3

What will be the output of the program on a 32-bit machine (using GCC)? Explain.

#include<stdio.h>

int main() {

     struct node {
         int data;
         struct node *link;
     };

     struct node *p, *q;
     p = (struct node *) malloc(sizeof(struct node));
     q = (struct node *) malloc(sizeof(struct node));
     printf("%d, %d\n", sizeof(p), sizeof(q));
     return 0;
}

The output shows

4, 4.

Is the above program related to structure member alignment padding and data packing?

1
  • Use %zu to print a size_t, and as pointed out by others, you are printing the size of a pointer, try: printf("%zu, %zu\n", sizeof(*p), sizeof(*q)); Commented Jul 31, 2015 at 7:55

4 Answers 4

3

No, you are just printing the size of the pointers. It's not related to the internal member layout of structures.

Sign up to request clarification or add additional context in comments.

1 Comment

Both sizeof(p) and sizeof(q) are equivalent to sizeof (struct node *) since p and q are both struct node *s.
2

On a 32-bit system the stored addresses are always 32 bits big. If you're printing the size of a pointer you're basically just printing the size of the address it points to (32 Bit -> 4 Byte).

If you want to know the size of the struct do something like this:

struct node p;
struct node q = {4, &p};

printf("%zu, %zu\n", sizeof(p), sizeof(q));

Comments

2
  • Point 1 Use %zu format specifier to print the output of sizeof of type size_t .

  • Point 2 Note the type of p, it is struct node *, same as q.

So, essentially, sizeof(p) and sizeof(q) are exactly same as sizeof(struct node *), which, on your platform are 32 bit wide. As you're not considering a variable here, so the alignment and padding for the structure and members are neither relevant nor involved in this case.

That said, please see why not to cast the return value of malloc() and family in C.

9 Comments

%zu will not necessarily work on every system, suggest OP the alternative to use %lu in that case.
@shekharsuman no, I won't. §7.21.6.1, paragraph 7 prevents me from doing that. :-)
Then I'd fluently award you a downvote. I've experienced this problem in Dev C++, Windows,32 bit. You should accept criticism, and happily apply that in your answer...
@shekharsuman you're most welcome. If i'm getting down-votes for advising the correct thing, i'm more than glad. Cheers !!
If you're not providing an alternative to OP for the special cases too, and if being advised by someone which you should, then you're pretty much rude guy. If you ask as a proof, then I can give you a proof too. BTW, I just asked you to make an edit revealing the exceptional cases too. It again, depends on your will to improve the answer.
|
0

No, it is not related to the structure member alignment padding and data packing.

Since it is a 32-bit machine the size of any pointer will be 4 bytes. Since p and q are of type struct node * i.e. pointer to struct node the result prints size of pointers p and q.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.