Solved

sizeof() operator for a structure

Posted on 2004-08-12
9
714 Views
Last Modified: 2008-01-16
there r 2 ques:
1.what will be the size of an empty structure?
structure test
{
};
main()
{
    struct test *p;
    p = (struct test *)malloc(sizeof(struct test));
   printf("%d",sizeof(struct test));
}

It prints 1.But wat does that mean.

2.and if i print like printf("%d"); gives me -1? why is this so.

Where can i get answers for questions like this.

thanks
0
Comment
Question by:abi_a
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 46

Assisted Solution

by:Sjef Bosman
Sjef Bosman earned 200 total points
ID: 11781287
Those are 3 questions ;)

1. your program as above won't compile; on my Linux-system the output is 0

2. printf("%d") expects a number on the stack, so it prints whatever is there; if you don't have parameters, pushed on the stack, then it just prints an int from the stack; could be a local variable

3. in EE
0
 
LVL 5

Accepted Solution

by:
migoEX earned 200 total points
ID: 11781303
1) Fro MSDN: "When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment", so probably the size of 1 is for that padding

2) The value (-1) was some orbitary value stored in th memory, and each time you can get different values. What happens is following:
(a) when you call some function, you put all arguments on the stack one after the other (in some cases part of them is stored directly in registers)
(b) the printf function has dynamic number of parameters, which means that it starts from the first "format" parameter, and each time sees reference to a variable, takes it from nect position on the stack. In your case, after reading "%d" it assumes the next value is integer, and reads it. As you didn't really fill this value with a number, some trash from the memory will be returned.
0
 
LVL 5

Expert Comment

by:migoEX
ID: 11781311
sorry, sjef_bosman
I didn't see your answer
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11781327
Happens in EE all the time, I'm used to it :) Sometimes good, somewhat reassuring to the asker.

Sjef
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 11781345
struct Data {
} ;

int main () {
     printf ( "%d", sizeof ( struct Data ) ) l
}

would print 1....because of the following

Consider before printf, u declare an array of the struct Data

struct Data arr[10] ;

As u know, array elemnets are stored in contiguous memrory locations...so each elemnt should have a distinct address
Thus in our case, if the size would have been 0, then each elemnet would not have had a unique addr

Hence a non zero size

Amit

0
 
LVL 11

Expert Comment

by:avizit
ID: 11781404
#include <stdio.h>
 struct Data {
} ;

int main () {
     printf ( "%d", sizeof ( struct Data ) ) ;
}


=========
the above prints 0 on my machine. ( linux /gcc)

0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11781465
I even modified the code to
    struct Data {
    } data;

    int main () {
         printf ( "%d\n", sizeof ( struct Data ) );
         printf ( "%d\n", sizeof ( data ) );
    }

and Linux prints two lines with zeroes... The outcome is not defined in the C language, and hence compiler-dependent.
0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 100 total points
ID: 11781575
The disucssion here is relevant

http://tinyurl.com/6fngc

to sumarise it here
such structs are illegal in C ..

/abhijit/


0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 11781607
Oh.....Sorry Guys........I didn't notice the TA...I thought I was answering C++ question

Amit
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question