2

While writing c code I noticed that when I change the value associated with memory location pointed to by pointer x, it results in a change of value of the data pointed to by pointer y.

When I checked again I found that malloc is allocating overlapping regions of memory for to 2 different pointers. Why is this happening??

I have quite a few dynamically allocated variables in my code. So is it because there is a limit to the maximum amount of memory that can allocated by malloc?

The following is an output from my code. From the output you can see that malloc allocates overlapping memory regions to x and y.

size x:32 y:144 //total size allocated to x and y by malloc

//the memory locations allocated to each of the pointers

location x:0x7fb552c04d20 y:0x7fb552c04c70 

location x:0x7fb552c04d24 y:0x7fb552c04c8c

location x:0x7fb552c04d28 y:0x7fb552c04ca8

location x:0x7fb552c04d2c y:0x7fb552c04cc4

location x:0x7fb552c04d30 y:0x7fb552c04ce0

location x:**0x7fb552c04d34** y:0x7fb552c04cfc

location x:0x7fb552c04d38 y:0x7fb552c04d18

location x:0x7fb552c04d3c y:**0x7fb552c04d34**

The code i used for allocating memory is

int *x = (int *)malloc((DG_SIZE+1)*sizeof(int));
int *y = (int *)malloc(4*(DG_SIZE+2)*sizeof(int));

printf("\n size x:%d y:%d\n", (DG_SIZE+1)*sizeof(int), 4*(DG_SIZE+2)*sizeof(int));

int a = 0;
for(a = 0; a <= DG_SIZE; a++){
   printf("\n location x:%p y:%p\n",(x + a), (y + a*DG_SIZE + 0));
}
Johns Paul
  • 633
  • 6
  • 22
  • 1
    Post the code where you make the call to malloc – samgak Jun 02 '15 at 03:41
  • int *x = (int *)malloc((DG_SIZE+1)*sizeof(int)); int *y = (int *)malloc(4*(DG_SIZE+2)*sizeof(int)); – Johns Paul Jun 02 '15 at 03:44
  • And how about `x`? Paste the whole snippet that leads to the output. – timrau Jun 02 '15 at 03:46
  • No `free()`, no loop? – timrau Jun 02 '15 at 03:47
  • 2
    @JohnsPaul Also, don't cast the return of malloc. – Sid Shukla Jun 02 '15 at 03:47
  • Put that in the question. And show all your code. For example you still haven't even shown how `x` is allocated and how you are printing the output. You may be leaving out code which you think isn't relevant but the chances are that it really is (`malloc` is very very very unlikely to be doing the wrong thing). – kaylum Jun 02 '15 at 03:48
  • How are you printing it? – user253751 Jun 02 '15 at 03:48
  • Have you considered using `valgrind`? – autistic Jun 02 '15 at 03:48
  • @JohnsPaul Paste the entire snippet, with allocation, printing, and freeing of memory. – Sid Shukla Jun 02 '15 at 03:50
  • See [Do I cast the result of `malloc`](http://stackoverflow.com/q/605845/4228131) – WedaPashi Jun 02 '15 at 03:51
  • Post the code where you are printing the address. Generally, on windows `0x7f` is the location where system dlls are loaded. Although I am not sure on this. Can somebody confirm this ? – Abhineet Jun 02 '15 at 03:52
  • "when i change the value associated with memory location pointed to by pointer x" - How awesome it would be if *any* of the posted code actually *did that*. [Post an **MCVE**](https://stackoverflow.com/help/mcve). – WhozCraig Jun 02 '15 at 03:54
  • //the addresses were displayed using the following code : int a = 0; for(a = 0; a <= DG_SIZE; a++){ printf("\n location x:%p y:%p\n",(x + a), (y + a*DG_SIZE + 0)); } – Johns Paul Jun 02 '15 at 03:55
  • And show the code where you are writing using the pointers and how you are checking the value through the other pointer (my bet is that you are overflowing a buffer somewhere). – kaylum Jun 02 '15 at 03:56
  • Hi, I am really sorry for posting only small portions of the code. But I am working on a proprietary code and that is why i cant post all my code here. Also i tried the code without the cast and that returned the same output. – Johns Paul Jun 02 '15 at 03:58
  • @AlanAu do i need to post the code that overwrites the the pointer? From the output it is clear that pointer x needs to have 32 bytes and and y needs 144 bytes. Also its can be seen that these 32 byte and 144 byte chunks have overlapping memory regions – Johns Paul Jun 02 '15 at 04:03

1 Answers1

9

The y block is wholly before the x block in memory. There's no overlap.

However your loop is printing out addresses from beyond the end of the y block, into the y column.

Specifically:

int *y = (int *)malloc(4*(DG_SIZE+2)*sizeof(int));

has allocated 36 ints, because DG_SIZE is 7 (based on your output).

But then you loop a from 0 to 7 and output (y + a*DG_SIZE + 0). When a == 6, this gives y + 42 which is beyond the end of the 36 ints allocated.

I guess you meant to output y + a*4, rather than y + a*DG_SIZE.

M.M
  • 138,810
  • 21
  • 208
  • 365