Sponsored Content
Full Discussion: sizeof
Top Forums Programming sizeof Post 86324 by Perderabo on Thursday 13th of October 2005 07:22:32 AM
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.
 

6 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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
OFFSETOF(3)						     Linux Programmer's Manual						       OFFSETOF(3)

NAME
offsetof - offset of a structure member SYNOPSIS
#include <stddef.h> size_t offsetof(type, member); DESCRIPTION
The macro offsetof() returns the offset of the field member from the start of the structure type. This macro is useful because the sizes of the fields that compose a structure can vary across implementations, and compilers may insert different numbers of padding bytes between fields. Consequently, an element's offset is not necessarily given by the sum of the sizes of the previous elements. A compiler error will result if member is not aligned to a byte boundary (i.e., it is a bit field). RETURN VALUE
offsetof() returns the offset of the given member within the given type, in units of bytes. CONFORMING TO
C89, C99, POSIX.1-2001. EXAMPLE
On a Linux/i386 system, when compiled using the default gcc(1) options, the program below produces the following output: $ ./a.out offsets: i=0; c=4; d=8 a=16 sizeof(struct s)=16 Program source #include <stddef.h> #include <stdio.h> #include <stdlib.h> int main(void) { struct s { int i; char c; double d; char a[]; }; /* Output is compiler dependent */ printf("offsets: i=%ld; c=%ld; d=%ld a=%ld ", (long) offsetof(struct s, i), (long) offsetof(struct s, c), (long) offsetof(struct s, d), (long) offsetof(struct s, a)); printf("sizeof(struct s)=%ld ", (long) sizeof(struct s)); exit(EXIT_SUCCESS); } COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2008-07-12 OFFSETOF(3)
All times are GMT -4. The time now is 12:47 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy