By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,173 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

size of structure variable whose structure contains no data types

P: 2
banfa just said that it is because so the compiler may not assign the same memory to two variables

but my question is WHY DOES COPILER ASSIGNMS MEMORY WHEN THERE IS NO NEED OF IT?

as the structure has no data types or member functions so why the memory needed to be assignem to structured variable with no data types? for what purpose?

Regards
Apr 5 '07 #1
Share this Question
Share on Google+
4 Replies


Ganon11
Expert 2.5K+
P: 3,652
Because if you explicitly create a struct, the computer cannot do nothing - something has to be created. But, since it is empty, is creates a something with the least space possible.
Apr 5 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
If the compiler allows this:

struct Data
{
};

Then it has to allow this:

Data var;

and this:

Data* ptr = &var

var has to exist in memory and that requires an allocation of 1 byte. Taking an address requires 1 byte at that address.
Apr 5 '07 #3

Banfa
Expert Mod 5K+
P: 8,916
If the compiler allows this:

struct Data
{
};

Then it has to allow this:

Data var;

and this:

Data* ptr = &var

var has to exist in memory and that requires an allocation of 1 byte. Taking an address requires 1 byte at that address.
The important point being that no 2 objects are allowed to have the same memory location so if you do this

Data array[10];

then array[0] must have a different memory location to array[1]. This is a requirement of the standard for a conforming implementation of a C/C++ compiler. The easiest way to achieve this is to give structures with no members an artificial size by assigning them 1 byte of memory so that objects of this type must exist at a different memory location to other objects.

And the reason they must have different memory locations is that if they didn't pointer arithmetic would not work correctly (or be very complicated to implement).
Apr 5 '07 #4

Banfa
Expert Mod 5K+
P: 8,916
banfa just said that it is because so the compiler may not assign the same memory to two variables
No I said the same memory address, which is different.
Apr 5 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.