Solved

sizeof() operator for a structure

Posted on 2004-08-12
9
705 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
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 46

Assisted Solution

by:Sjef Bosman
Sjef Bosman earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
sorry, sjef_bosman
I didn't see your answer
0
 
LVL 46

Expert Comment

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

Sjef
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
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
Comment Utility
#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
Comment Utility
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
Comment Utility
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
Comment Utility
Oh.....Sorry Guys........I didn't notice the TA...I thought I was answering C++ question

Amit
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now