Solved

sizeof() operator for a structure

Posted on 2004-08-12
9
710 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
C Programming - If Statement 8 82
windows 10 pro lost profile. 10 45
Is there a lightweight USB-C to USB 3 hub that would work on a MacBook Pro? 3 27
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

831 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