473,322 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

About the size of structure

typedef struct AA {
} A;

when use gcc compiles, sizeof(A) is 0
when use g++ compiles, sizeof(A) is 1

So I can known the length of this pointer is 1

but when I add one member to the structure,
typedef struct AA {
int i;
} A;

Now if use gcc, sizeof(A) is 4
if use g++, sizeof(A) is still 4.

where is this pointer?

Thanks
Jul 22 '05 #1
4 2341
Albert Yan Qing Ge wrote in news:3F***************@lucent.com:
typedef struct AA {
} A;

when use gcc compiles, sizeof(A) is 0
when use g++ compiles, sizeof(A) is 1
Yup C++ insists every type has a minimum size of 1, I don't know
about C so whether gcc output is correct I can't say.

So I can known the length of this pointer is 1
What pointer ?. There is non *special* pointer stored in every C++
user defined type.

but when I add one member to the structure,
typedef struct AA {
int i;
} A;

Now if use gcc, sizeof(A) is 4
if use g++, sizeof(A) is still 4.

The extra byte in the first example was just padding (or a filler byte
if you like), it'e not some special value (i.e. 'this').

where is this pointer?


Again what pointer !

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
On Tue, 09 Dec 2003 10:42:13 +0800, Albert Yan Qing Ge
<ay**@lucent.com> wrote in comp.lang.c++:
typedef struct AA {
} A;

when use gcc compiles, sizeof(A) is 0
GCC is a C compiler. The structure definition is illegal in C, where
structures with no members are not allowed. So you are not using GCC
as a conforming C compiler, consult your documentation or a gcc
newsgroup for how to do this.
when use g++ compiles, sizeof(A) is 1
Structs or classes with no members are allowed in C++, but 0 length
objects are forbidden. So a class or struct with no members must have
sizeof of at least 1. Otherwise it would be impossible to have an
array of such objects, as each array element must have a unique
address.
So I can known the length of this pointer is 1
Do you mean the "this" pointer that is passed to non-static member
functions? The sizeof value of a class never includes room for that,
it is not a member of the class or struct.
but when I add one member to the structure,
typedef struct AA {
int i;
} A;

Now if use gcc, sizeof(A) is 4
if use g++, sizeof(A) is still 4.

where is this pointer?
The "this" pointer is not EVER in the class or struct. It has no
effect on the size of a class or struct.
Thanks


You need to get a good book on C++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 22 '05 #3
Try #pragma pack(1)
before the structure definition... then check your sizeof(...)
"Albert Yan Qing Ge" <ay**@lucent.com> wrote in message
news:3F***************@lucent.com...
typedef struct AA {
} A;

when use gcc compiles, sizeof(A) is 0
when use g++ compiles, sizeof(A) is 1

So I can known the length of this pointer is 1

but when I add one member to the structure,
typedef struct AA {
int i;
} A;

Now if use gcc, sizeof(A) is 4
if use g++, sizeof(A) is still 4.

where is this pointer?

Thanks

Jul 22 '05 #4

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message news:Xn**********************************@195.129. 110.204...
Albert Yan Qing Ge wrote in news:3F***************@lucent.com:
typedef struct AA {
} A;

when use gcc compiles, sizeof(A) is 0
when use g++ compiles, sizeof(A) is 1


Yup C++ insists every type has a minimum size of 1, I don't know
about C so whether gcc output is correct I can't say.

The above struct is illegal in C. You aren't allowed to define
empty structs. GCC is wrong.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sims | last post by:
Hi, I have some small questions that have never been any problems, (for my compiler?), but always make me curious. So here goes... what does the standard sday about the 'if' statement? for...
5
by: John | last post by:
Hi all, Can a linked list be a member of a structure? If so, when I add or remove an element from the linked list, the size of the structure will change. Will it cause any problem? Thanks a...
8
by: Bryan Feeney | last post by:
This structure is, according to sizeof, 3 bytes long, which makes sense struct test { char text; }; This structure is, according to sizeof, 4 bytes long, which also makes sense struct...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
11
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
7
by: Lars Netzel | last post by:
Hi How do I get information (read) about the phonenumber in the default Dial Up Connection? Best Regards/ Lars Netzel
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
12
by: Sam Kong | last post by:
Hi, JavaScript hides its memory structure. I know that numbers, booleans, null and undefined are value types (value is directed saved in a variable). I want to know: - How JavaScript...
15
by: kris | last post by:
Hi I am writing a small program where I need to obtain the actual size of a structure. The programm is as follows struct abc { int j; char k; int i; }*a;
6
by: parag_paul | last post by:
Suppose I have a struct typedef struct atype{ int a; int b; } at; main(){ static at* j= 0;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.