LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2004, 02:07 AM   #1
joeyBig
LQ Newbie
 
Registered: Jul 2004
Posts: 23

Rep: Reputation: 15
when does sizeof ( ) operator return zero !!


hi

When does the sizeof () operator return the value zero.

regards,
joe
 
Old 10-01-2004, 03:58 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
I guess you mean the C sizeof() operator (as opposed to, say, the PHP one).

The sizeof operator returns the size of its operand, in memory units (not necessarily bytes).
sizeof() a pointer will return the size of the thing the pointer points to; apparantly, it returns 1 for a pointer to a pointer (this makes sense, as you can ++ a pointer to make it point to the next element in a contiguous array)

I guess passing an empty struct or union (or C++ class) would return zero on some implementations but I wouldn't rely on it.
 
Old 10-01-2004, 05:54 AM   #3
joeyBig
LQ Newbie
 
Registered: Jul 2004
Posts: 23

Original Poster
Rep: Reputation: 15
hi rjlee,
thanks for u'r information.
what u said is correct.
an empty structure or union is the answer.
empty class returns 1.

the doubt i got is,
in C, the size of an empty structure is 0 while in C++, it is 1.

why does this difference occurs.

regards,
joey
 
Old 10-01-2004, 06:43 AM   #4
WhiteChedda
Member
 
Registered: Aug 2003
Location: Florida
Distribution: Mandrake 9.1 for now
Posts: 205

Rep: Reputation: 30
Re: when does sizeof ( ) operator return zero !!

Quote:
Originally posted by joeyBig
hi

When does the sizeof () operator return the value zero.

regards,
joe

Make sure you tell the compiler to do strict ANSI C compiling. You SHOULD get a warning for C, as this is undefined behavior if I am remembering correctly and hence compiler dependent. Some compilers may have extentions to handle this in a non-standard fashion, and this might be what you are seeing.

As to C++ last time I used it, there was no standard, so I'm really not sure about that.

Last edited by WhiteChedda; 10-01-2004 at 09:06 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sizeof operator implementation vinods Programming 1 10-26-2004 09:57 AM
How Sizeof Operator Implemented? hiren Linux - General 4 09-29-2004 05:12 AM
Sizeof Operator Implementation hiren Programming 4 09-28-2004 10:32 AM
sizeof() return wrong size of structure?? Hammo Programming 7 02-21-2004 04:08 PM
sizeof() and C/C++ boku Programming 2 09-01-2002 07:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration