sizeof


 
Thread Tools Search this Thread
Top Forums Programming sizeof
# 1  
Old 10-12-2005
sizeof

we know that sizeof never returns zero when used with structure

then why in this case it is returning zero


struct foo
{
char c[];
};

void main()
{
struct foo f;
cout<<sizeof(f);
}

i am working on solaris 5.8
isn't the above function should return the size of empty structure
# 2  
Old 10-12-2005
How is your program even compiling? I tried to complie the following program on HP-UX 11.11 and SunOS 5.8:
Code:
#include<stdio.h>
#include<unistd.h>

struct s {
        char sarr[];
};

int main() {
        int a;
        char c;
        struct s b;

        fprintf(stdout,"size of int: %d\n",sizeof(a));
        fprintf(stdout,"size of char: %d\n",sizeof(c));
        fprintf(stdout,"size of char arr: %d\n",sizeof(b));
}

And I am getting the following errors:
On HP:
cc: "t3.c", line 5: error 1578: Size of struct or union member is unknown.
cc: "t3.c", line 4: error 1613: Zero-sized struct.
cc: "t3.c", line 15: warning 504: The sizeof operator applied to a zero-sized object.
Note the errors in lines 4 and 5 above.

On Sun:
t3.c:5: error: flexible array member in otherwise empty struct
# 3  
Old 10-12-2005
but i am getting the result as zero

my program is like this


struct foo
{
char c[];

};


void main()
{
struct foo s;
cout<<sizeof(s);
}



when i compile this and run on solaris 5.8 it gives the output as 0
i dont know why

there is no error in my compilation
# 4  
Old 10-12-2005
I think this has to do with the fact that you are using C++. Cant test that now. Sorry.
# 5  
Old 10-12-2005
PHP Garbage in, garbage out

All C and C++ compilers reject this code. Even g++ would not compile it until I added:
#include <iostream>
at that point I had to run it with:
./bogus ; sleep 10
to see the output. To fix that, I made this change:
cout << sizeof(s) << "\n" ;
Now that I can easily view the output, yes it is zero. I am a liitle surprised that it compiles at all. Using:
g++ -ansi -pedantic bogus.C -o bogus
I get a warning:
bogus.C:4 warning: ANSI C++ forbids zero-size array `c'

How many characters did you expect c[] to be sized for? 5? 17? 80? Is zero really that unexpected? I really cannot imagine what result you were hoping for... Smilie
# 6  
Old 10-13-2005
man u havent under stand my problem

as you said that it return zero
the same result i too am getting

but i have read that sizeof can never ever return zero even if the class or structure is empty

if you remove the char from the structure the value of sizeof is 1
but when include the char[] then its value is zero
why?

thats was my question
# 7  
Old 10-13-2005
My guess is that sizeof cannot work out the size of the struture and is passing back 0, representing a null or undetermined value or an error condition.

What does your documentation for sizeof say about error conditions/values from this function?
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