Skip to main content
cjaya.2
Associate III
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.

2 replies

Tesla DeLorean
Guru
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?

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
cjaya.2
cjaya.2Author
Associate III
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.  

Tesla DeLorean
Guru
February 15, 2024

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

 

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
TDK
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.

"If you feel a post has answered your question, please click ""Accept as Solution""."