This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Pointer moves from X:0x00025 to I:0x25

Greetings forum!

I am having trouble with a pointer that seems to be moving from what it should be pointing at in XDATA to an address in IDATA(Im assuming thats what I:0x000 is). uC is a c8051f020.

I initialized a global pointer with:

xdata ms_p MSG = NULL;


where ms_p is a pointer typedef to a message structure.

Depending on code, I set the address of what MSG is pointing to with:

...
MSG = &Msg2;


where Msg2 is a message structure in XData.

which works fine and points to the correct address of Msg2.
What baffles me is that after all this, in another function, MSG moves from where its pointing(*Msg2) to an equivalent address in the Idata area.

Where it breaks specifically:

...
case JS_string: case JS_object:
        cp = va_arg(ap, char *);
        jsm->child->vars.str.len = strlen(cp);
        jsm->child->vars.str.st = (char *)malloc(jsm->child->vars.str.len);
        strcpy(jsm->child->vars.str.st, cp);
//msg link breaks here

Any ideas would be appreciated.

  • You have forgotten to check the result of malloc(). And you allocate strlen() bytes but forgets that strcpy() will actually copy strlen()+1 bytes.

    Another thing - you are remembering the difference between a pointer stored in xdata, and a pointer pointing to data in xdata? Just something to think about and similar to the regular problem with const pointers and pointers to const data.

  • Thanks, I added a 1 to the malloc size and that did the trick.
    I know I forgot to check for a good malloc, just trying to get the pointer magic to work first.

    I thought the pointer would have to be explicitly defined within the memory type in order to successfully point to an address within a memory type, is that wrong?

  • A pointer is a variable whos value is is the address of something else.

    So a pointer have a storage class. And it has a type.

    It may have the storage class xdata, i.e. the value of the pointer should be stored in xdata.
    And it may have a type of xdata pointer to xx, i.e. that it pointers to an object stored in xdata.

    That was also why I mentioned the similarity with const pointers or pointers to const. A pointer that may not change value, i.e. you may not assign a new value or increment the pointer. Or a pointer that points to a variable that may not be changed.

    But back to your malloc().

    You should never, ever, ever, ever, try to get something else working first and then add in tests for the result of the call like adding the dots above an i. Always add the error handling instantly. That is the only way that you will know that you don't forget to revisit the code. Whenever you see a PC program fail, you have to remember that the probability was very high that a programmer did decide to "just fix this first" and forgot to either check return values or run his new code through a decent test bench that did exercise all code branches, i.e. full code coverage.