[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 724
  • Last Modified:

sizeof() operator for a structure

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
abi_a
Asked:
abi_a
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
Sjef BosmanGroupware ConsultantCommented:
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
 
migoEXCommented:
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
 
migoEXCommented:
sorry, sjef_bosman
I didn't see your answer
0
Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

 
Sjef BosmanGroupware ConsultantCommented:
Happens in EE all the time, I'm used to it :) Sometimes good, somewhat reassuring to the asker.

Sjef
0
 
Sys_ProgCommented:
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
 
avizitCommented:
#include <stdio.h>
 struct Data {
} ;

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


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

0
 
Sjef BosmanGroupware ConsultantCommented:
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
 
avizitCommented:
The disucssion here is relevant

http://tinyurl.com/6fngc

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

/abhijit/


0
 
Sys_ProgCommented:
Oh.....Sorry Guys........I didn't notice the TA...I thought I was answering C++ question

Amit
0

Featured Post

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now