Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

sizeof() operator for a structure

Posted on 2004-08-12
9
Medium Priority
?
720 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 600 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 600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

810 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