tags:

views:

160

answers:

5

I wrote a test program thinking that the address of p1 will be less than p2, but when I compiled the code, the address of p2 turned out to be lower (p1 is 8 units larger than p2). I also heard rumors that 2 adjacent memory blocks will combine themselves automatically. Does that play any part in the following code?

void main(){
    char *p1, *p2;
    p1=malloc(4);
    p2=malloc(5);
    p1="yah";
    p2="goog";
    printf(" p1 = %d, and p2 = %d \n", p1, p2);
}
+7  A: 

malloc can give you a pointer from anywhere in the heap; it's entirely up to the implementation as to how it allocates memory.

The pointer values are limited to your address space range (so, in a 32-bit address space, you won't get a pointer value greater than 2^32 - 1).

James McNellis
does that mean p2 is sometimes larger than p1 and p1 is sometimes larger than p2? Thanks
Yes. Memory could be anywhere.
GMan
It's quite possible, yes. Though, as Martin points out, you are not copying the string correctly, and you are printing the pointers to the static string literals.
James McNellis
It would be fairly unconventional when two subsequent blocks allocated in main have decreasing addresses; it actually doesn't happen so in the OP's program.
Martin v. Löwis
I agree that that would be unusual. Technically, we don't know what pointers malloc has returned to him since, as you pointed out, he's not printing the pointers to the malloc'd blocks.
James McNellis
+11  A: 

You are not printing the addresses returned from malloc, but the addresses of the string literals (which are statically allocated). If you want to fill the blocks, use strcpy; doing so is not necessary to print the addresses, though.

Also, use %p to print pointers.

Martin v. Löwis
Yes, you've actually thrown away the addresses returned by `malloc`, replacing them with other addresses.
caf
You meant to use `strcpy(char *dst, const char *src)` to copy into the buffers returned from `malloc()`.
hughdbrown
+1  A: 

The OS will determine how to meet the malloc demands, as your program has allocations that went beyond just what you explicitly asked for, and there is no guarantee where it will be allocated at, as this is not physical memory but pointers to some virtual memory that will map to physical memory.

Also, two adjacent blocks won't be merged, as it would then be difficult to determine how to free them, which of the two pointers would free be called with?

James Black
Contiguous memory spaces are merged together once they are freed and ownership is passed once again to the OS.
klynch
True, but not when they are malloced, hence the problem would be how to free them.
James Black
+4  A: 

malloc(3) obtains memory from the OS, so the OS, and the way your program is linked, are what determine the addresses that malloc(3) can play with. Because malloc recycles memory, you may be reusing an older allocation, or it may simply decide to hand out the higher addresses first.

However, in your program, you overwrite the malloc address references with references to your string literals. These are allocated in static memory in your image; malloc is not involved.

DigitalRoss
+1  A: 

"p1 will be less than p2" falls in the realm of "undefined". Something like p1 < p2 is defined only if p1 and p2 point to elements within the same array or struct.

sigjuice