sizeof


 
Thread Tools Search this Thread
Top Forums Programming sizeof
# 8  
Old 10-13-2005
c[] is not legal. Reasonable compilers refuse to produce an executable from your source code. That is the behavior I would prefer to see and I think it's unfortunate that g++ compiles it. But the standard does not require g++ to reject illegal code. So g++ is free to do anything it wants with your code. The resulting program can output 1 or 0. Or 8,732. Or it could whistle dixie. Or it could dump core. No behavior is specified for a non-c++ program by the c++ standard.

As for what you call "empty" structures, "Objects with an incomplete structure or union type may be mentioned in contexts where their size is not needed". And the sizeof operator needs the size. That too is illegal. gcc allows sizeof to operate on a incomplete structure and for some odd reason decided it should return 1.

Microsoft allows a single unsized array as the last member of a structure. gcc's behavior with unsized arrays is a superset of Microsoft's extention. The allows gcc to compile some Microsoft code. Maybe that is why gcc behaves as it does with unsized arrays. Microsoft says sizeof return the size of the structure minus the size of the array. The array has a non-zero size but the size is not known at compile time. Apparently, you obtain the size of the array somehow and add it in later. Miicrosoft unsized arrays in structures Note that you are abusing Microsoft's screwy extention. You are not supposed to allocate it as you have done.

Incomplete structures are in the same boat. The members are unknown, not absent. gcc is out of its mind in allowing you allocate such a thing. Each object is supposed to have a unique address, so if you allocate two of them, I guess that they consume a byte.

So nonstandard extentions to the language by two different organizations have clashed with one another. Righteous karma, if you ask me. Try writing in C or C++ for a change. Then none of this will affect you.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Programming

Compiler/Runtime uses of sizeof

Ignoring other considerations for a moment and in general ... Would there be a difference in result (dot oh or execution) of: A. strncpy( a, b, sizeof(a) ); vs. B. c = sizeof(a); strncpy( a, b, c ); My general understanding is (at least I think my understanding is) that... (10 Replies)
Discussion started by: GSalisbury
10 Replies

2. Shell Programming and Scripting

Sizeof a file from directory path in perl

Hai how to find size of a file?? ex : /home/kiran/pdk/sample/calibre this is a path In that I have to find size of a files in side a calibre(it is the folder) like .results or .summary (1 Reply)
Discussion started by: kiran425
1 Replies

3. Programming

sizeof(object) in C++

Hi, I have defined the class and call the sizeof(object to class) to get the size. # include <iostream> # include <iomanip> using namespace std; class sample { private: int i; float j; char k; public: sample() { } (2 Replies)
Discussion started by: ramkrix
2 Replies

4. Programming

Doubts regarding sizeof() operator

Hi, There are some bewildering sizeof() questions I have in my mind. Could anyone shed some light on this? int main() { printf("%d\n", sizeof(main)); // Ans: 1 } That is, the sizeof() a function identifier though it is treated internally as a pointer gives 1 byte always, why? ... (5 Replies)
Discussion started by: royalibrahim
5 Replies

5. Programming

How to get the sizeof char pointer

The below code throws the error, since the size of x = 19 is not passed to the cstrCopy function. using namespace std; static void cstrCopy(char *x, const char*y); int main () { char x; const string y = "UNIX FORUM"; cstrCopy(x,y.c_str()); return 0; } void cstrCopy(char *x,... (3 Replies)
Discussion started by: SamRoj
3 Replies

6. Programming

sizeof an array of structure without using 'sizeof' operator

Hi All, is it possible to find out the size of an array of structures ( without using 'sizeof' operator). The condition is we have the array of structure instant but we are not aware of the elements inside the structure. Can someone help me out? Thanks in advance. (18 Replies)
Discussion started by: rvan
18 Replies
Login or Register to Ask a Question