Skip to main content
Visitor II
February 15, 2024
Question

Cannot access memory

  • February 15, 2024
  • 2 replies
  • 4101 views

Hi, I am having problem with string copy strncpy(). I am getting error message "Cannot access memory at address 0x31203a6e"

static char *DataBuffer[30] = {0};

for(char *str =strtok((char*)recdata, "\n\n"); str; str = strtok(NULL, "\n\n"))
{
 strncpy(DataBuffer, str, strlen(str)+2);
 strcat(DataBuffer,"\n");
 strncpy(DataBuffer, str, strlen(str)+2);
}

Is it memory addressing access faults. Is the chip failing memory handling. How do I debug it to find out.

    This topic has been closed for replies.

    2 replies

    Graduate II
    February 15, 2024

    The pointer starts or becomes invalid as not in usable memory

    Suggest you instrument so you can watch where it fails.

    strlen + 2 ?? Why?

    Are these properly NUL terminated strings?

    cjaya.2Author
    Visitor II
    February 15, 2024

    I have found the cause basically I have a else if condition,

    else if(command)
     {
     		
     break;
     }

     its in the while loop when the command is 1 it breaks up the loop from the while loop. and it will come out from the while loop. But when I debug command showing as -1 and goes inside the if condition and breaks from the while loop. Why is that?. if condition is to check if it is true not as -1.  

    Graduate II
    February 15, 2024

    Wouldn't any non-zero value for command be expected to enter that condition, if it gets there, and break?

     

    Super User
    February 15, 2024

    static char *DataBuffer[30] = {0};

    This is an array of pointers to char. Probably not what you want. Should be producing errors or warning upon trying to compile. You probably want and array of chars like this:

     

    static char DataBuffer[30] = {0};

     

    > strlen(str)+2

    Calculate this before you use it and ensure you're not overstepping the bounds of DataBuffer.