tags:

views:

64

answers:

6

Can someone please explain me this peculiar output:

#include <stdio.h>

typedef struct node
{
  int i;
  struct node *next;
}node;

main()
{
    node *p,*q;
    printf(" %u ",sizeof(node));              // 16
    p = (node *)malloc(sizeof ( node ) ) ;     
    printf(" %p ",p);                    // 0x1cea010
    q = (node *)malloc(sizeof ( node ) ) ; 
    printf("\n %p ",q);                    // 0x1cea030
}

I have a 64 bit processor. When the size is shown to be 16 byes, why is 32 byte allocated for the node?? I checked out a 32- bit machine. The addresses had a separation of 8 bytes. With no padding and stuff. So is the difference of 4 bytes solely cause of some padding issue of the 64 bit machine??

+7  A: 

Two malloc calls aren't necessarily going to return consecutive memory areas. A better way to do this test would be:

main()
{
    node *p;
    printf(" %u ",sizeof(node));
    p = (node*)malloc(2 * sizeof (node));     
    printf(" %p \n %p ", &p[0], &p[1]);
    free(p);
}

By allocating an array, you can be sure that they are back-to-back in memory.

Depending on your implementation of malloc, your system may be using the memory in between p and q to store bookkeeping information that is used by realloc, free, and friends.

bta
Thanx bta.. It works..
smartmuki
+1  A: 

When you allocate memory the allocator also needs to include some information about the chunk you just allocated. This is probably where the extra 16 bytes comes from. Also, the allocator might enforce a minimum chunk size to help prevent fragmentation. There are also alignment issues to take into account.

James
+2  A: 

The address malloc() returns is determined by the memory planning algorithm of the operating system. You are not guaranteed that two malloc calls following each other will get memory segments being after each other. This said, your code does not allocate 32 bytes for p, it allocates 16. Any writes/reads beyond the 16 bytes have undefined behavior and can crash your program. The same applies to q.

dark_charlie
A: 
node *p,q;

is the same as:

node *p;
node q;

So

q = (node *)malloc(sizeof ( node ) ) ;

Is an error because you are assigning a pointer value to a structure.

You are also not including either or , so your compiler doesn't know what the signature for malloc is, which may be why you aren't getting error or warning messages alerting you to the bad assignment.

If you are asking about the space between the two malloced memory areas, malloc does not have to (and usually does not) allocate the memory areas contiguously. There is usually a smallest sized chunk that it will allocate (smaller amounts are rounded up to this) and there may also be some bookkeeping data before the pointer that malloc returns.

nategoose
I am very sorry. I must have eaten up that star!!
smartmuki
A: 

Why do you conclude that malloc allocated 32 bytes for one node? They must not necessarily lie continuous. Btw, your implementation of malloc may need some extra space for storing book-keeping information.

swegi
A: 

malloc() doesn't guarantee consecutive memory chunks in consecutive calls. It may not even return two pieces of the same page.

It's generally a bad idea to assume anything about the memory dynamically requested and returned by malloc(). If anything is to be assumed, it's best to think that you were given the exact amount of bytes you asked for and that they're in some isolated location in memory.

That said, malloc() is much, much smarter than one would think. It's possible that it skipped those 16 bytes in case you allocated more structures of that same size, or decided to realloc that same one.

Santiago Lezica