cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot access memory

cjaya.2
Associate II

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.

10 REPLIES 10

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 Venmo
Up vote any posts that you find helpful, it shows what's working..

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.  

TDK
Guru

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".

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 Venmo
Up vote any posts that you find helpful, it shows what's working..

I did debug and I found that 'command' value change from 0 to -1 even though I assigned at the begining as 0 for command. Somehow it changes its value for some reason. I even tried initializing static int command to volatile int command. It didnt make difference. I dont understand why its value changed to -1.

I've got zero visibility into anything you're doing.

The implied cause would be you're breaching some other memory buffer or the stack.

Behaviour change with optimization levels?

Got any static analysis tools, or a colleague who can write and review code?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi,

Thank you for the reply. I am using STM32CUBIDE to debug. Are there static analysis tools that I can use with the STM32CUBEIDE. Can you recomend. Why the behaviour change optimization level? is there way to check the optimization levels

 

https://pclintplus.com/

>>Why the behaviour change optimization level? is there way to check the optimization levels

It was posed as a question, do you see differences? Typically compilers will provide an option/setting as to what level to use. Look in the options and related tabs. Increase warning levels, and perhaps review what's currently flagging.

Do a basic code walk. Have a colleague do a code walk. Run LINT or MISRA tools against your code, enable them on your source code control if you can to check commits for rules violations.

Put checks in your code for the state of command, and bisect the problem to identify where you first see it occurring.

Your string code looks problematic, strtok() also isn't thread safe, perhaps use strsep() instead.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thank you for the reply. It was very useful

I tried to use strsep() but I am getting compiler warning  stating "passing argument 1 of strsep from incompatible pointer type"  here is the code,

for(char *str =strsep(&recdata, "\n\n"); str; str = strsep(NULL, "\n\n"))
{
 
}

 What am I doing wrong here..