-4

I am facing one problem on the memory allocation of a local variable. Here's the link for the code https://ideone.com/3O9522 when the Data arrays (data1, dat2, data3, data4) are declared local.

unsigned char *PutData1[2];  
unsigned char *PutData2[2];  
void call_func1(unsigned char x, unsigned char *Test1, unsigned char *Test2)
{  
PutData1[x] = Test1;   
PutData2[x] = Test2;  
if (x== 0)  
{  
printf("Init PutData1: %02x %02x %02x %02x  \n", PutData1[0][0], 
PutData1[0][1], PutData2[0][0], PutData2[0][1]);
}  
if (x== 1)  
{  
printf("PutData2: %02x %02x %02x %02x  \n", PutData1[1][0], PutData1[1]
[1],PutData2[1][0], PutData2[1][1] );  
printf("Afer PutData1: %02x %02x %02x %02x  \n", PutData1[0][0], PutData1[0]
[1], PutData2[0][0], PutData2[0][1]);  
}  
}  

void Func1(void)  
{  
unsigned char Data1[2] = { 0xAB, 0xCD };  
unsigned char Data2[2] = { 0xDE, 0xAE };  
call_func1(0, &Data1[0], &Data2[0]);  
}  

void Func2(void)  
{  
unsigned char Data3[2] = { 0x44, 0x33 };  
unsigned char Data4[2] = { 0x11, 0x55 };  
call_func1(1,&Data3[0], &Data4[0]);  

}  

int main(void) {  
Func1 ();  
Func2 ();  
return 0;  
}  

After calling call_func1 (1, &Data3[0], &Data4[0]), values at Data1 and Data2 arrays are overwritten

Here's the link for the code https://ideone.com/I00OZj

when the Data arrays are declared global. After calling call_func1 (1, &Data3[0], &Data4[0]), values at Data1 and Data2 arrays are retained. Can anyone explain for such behaviour.

  • I didnt get any memory error. It was compiling though I didnt look on warnings also in the compiler. The only thing which I observed was that the data buffers were over written. – Akshara Prasad Jan 13 '18 at 18:34
  • 2
    One obvious flaw is that you're declaring `call_func1()` to take three arguments, but you're only supplying two for each call. What do you think the value of `Test2` is going to be? – Dave Tweed Jan 13 '18 at 18:36
  • But given that none of the code should be modifying the contents of either `Data1[]` or `Data2[]`, there must be some subtlety with regard to their status as local (automatic) variables. A peek at the generated code would tell us a lot. In the meantime, try declaring them `static`. – Dave Tweed Jan 13 '18 at 18:44
  • Declaring static or global, it works.But didnt get why it didnt work when declared locally. and PutData2[1] was assigned the same address as PutData2[0]. – Akshara Prasad Jan 13 '18 at 18:51
  • @dave : Edited the code. Please check – Akshara Prasad Jan 13 '18 at 18:52
  • 1
    Compile your code with warnings enabled (`-Wall` for several compilers). Then heed the warnings. – marcelm Jan 13 '18 at 18:52
  • What did your compiler tell you about the types of the arguments you pass to `call_func1()`? – EOF Jan 13 '18 at 18:52
  • 1
    Please post a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). There's no output from the code you posted, so the problem could easily be in the way you check/view the output. Also, it's clear from the edit history that you aren't posting your real code. That's just wasting everybody's time. Copy/paste the real code into the question. – user3386109 Jan 13 '18 at 19:01
  • Your issue is `call_func1 (0, &Data1, &Data2);`, the & is incorrect for the arguments that `call_func1` expects. This works: https://ideone.com/su3iUc That said, I do get the correct results with your exact code on ideone as well. I get the expected warnings here, but the output is still correct: http://coliru.stacked-crooked.com/a/61b06ffdecc3f640 What compiler are you using? I would expect a recent compiler to complain about the implicit int return on main, but maybe that is still okay for C code. – Retired Ninja Jan 13 '18 at 19:08
  • Here's the link for the code https://ideone.com/3O9522 – Akshara Prasad Jan 13 '18 at 19:49
  • @Akshara Prasad: But the code at the link is completely different from the code you posted in the question. Why did you post wrong code in the question? If you have a question about the code at the link - post that code in the question. Don't post fake code. – AnT stands with Russia Jan 13 '18 at 19:53
  • I have updated the correct one now – Akshara Prasad Jan 13 '18 at 20:06

1 Answers1

1

Your code stores pointers to local arrays Data1 and Data2 declared in Func1. It stores these pointers in PutData1[0] and PutData2[0]. When Func1 completes, these local arrays Data1 and Data2 are destroyed. The PutData1[0] and PutData2[0] pointers become dangling. Any attempts to access anything through these dangling pointers will lead to undefined behavior.

In call to call_func1 made from Func2 you attempt to access data through those dangling PutData1[0] and PutData2[0] pointers. The behavior is undefined. That's all there is to it.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Ihave updated the code. [link](https://ideone.com/3O9522) . Here's the code in which i am facing the problem. – Akshara Prasad Jan 13 '18 at 19:47
  • @Akshara Prasad: But your updated code is completely different from the code you posted in the question. Why did you post wrong code in the question? – AnT stands with Russia Jan 13 '18 at 19:52
  • And if I declare the array as global, it works as expected. https://ideone.com/I00OZj – Akshara Prasad Jan 13 '18 at 19:53
  • @Akshara Prasad: Exactly as it should be. The code with locals is nonsensical and produces undefined behavior. Which is exactly what you observe. This all has nothing to do with the code you posted in the question though. – AnT stands with Russia Jan 13 '18 at 19:54