The code snippet is : Code: struct tree { int data; char name; float salary; }*HEAD; int main() { printf("size of tree : %d",sizeof(struct tree)); getch(); return 0; } when i execute using DEV C++ compiler , the output is : 12 , but it should be 9 {4,1,4}. since int is 4 bytes , char 1 byte , float is 4 byte according to Dev c++. when i run in TURBO C , it's coming correctly as 7. I need to know wat's happening inside. please help me out if anybody have an idea, :undecided:undecided
Do you really mean "char name"? Names are usually more than one character long. But anyway, you will get different results from different compilers depending on how they pack fields into memory; there is no "should" value. 12 is just as good as 9 or 7 (although I can't see where 7 comes from, this implies sizeof(int)=sizeof(float)=3). If you need this data to take up a specific number of bytes you will have to code that part yourself, e.g. Code: struct tree { char data[9]; }; then you could use [0-3] for data, [4] for name and [5-8] for salary.
Thanks for your reply :charming:. i too know the memory allocation for data types in each compiler will be different. but, Actually i tried of giving a code snippet like this : struct tree { int data; char name[10]; float salary; }*HEAD; if i type this & am getting an o/p like : 16 !!! then hw the split up of memory will be? if i give character member alone in a structure just to find out the size. then am getting 1 byte.!!! if i impose any int or float along with that char inside a structure, it's displaying an unpredictable answer!! as if char is taking 4 bytes long!!! in the above code : if i replace char name[10] as char name[100]; that char array is exactly taking 1 byte each.!! but if it is char name[10] , it's displaying an different answer. am totally confused !!! :nonod::nonod::disappoin:worried: please help me out . Thank you.
>> then hw the split up of memory will be Simply take the address of each element and display it on the screen. malloc a new HEAD then printf HEAD as a pointer, then &data, name(which is already an address) and &salary. Then you can see exactly how the memory is used. Also you might want to printf the sizeof() each element, and maybe the HEAD. >> if i give character member alone in a structure just to find out the size. then am getting 1 byte.!!! Yes, of course. sizeof(char) is 1 byte. You cannot store a multibyte string in a single character. >> as if char is taking 4 bytes long!!! It may seem that way, for example if name=&data+4 and &salary=name+4 but in reality the compiler is probably word-aligning the elements for more efficient memory access by the CPU. In name's 4 bytes you'll be able to store char name, char name[2], char name[3] and char name[4] without any change of HEAD size. >> if i replace char name[10] as char name[100]; ..etc Don't really understand. Do what I suggested above, i.e. display the addresses and sizeof each element. That should clarify matters. Or are you confused by char[10] and char[100] being different sizes? You shouldn't be, because they ARE different sizes - char[10] is 10 chars, and char[100] is 100 chars, which will take different amounts of memory.
OK... thank you, i agree & i will display and see Wat have u mentioned, but about the last query , the code snippet is : struct tree { char name[10]; int data; }*HEAD; when i see the size of (struct tree) it's displaying : 16. ok -> O/P 1 but the above o/p should be 14 bytes only (10bytes + 4bytes) according to DEV c++ compiler when i increase my size to 100 (ie) char name[100] , the o/p is 104. - > O/P 2 since am increasing my array size 10 times more , according to o/p 1 it must display as 160 , & hw is it displaying correctly as 104 (100bytes + 4bytes) ??? That's only is my question boss:embarasse:embarasse!!!!!
Again you're making the mistake that it "should be" some value. It varies by implementation. Re-read that until you get it. 16 is equally valid if the compiler is doing WORD ALIGNMENT - that is, aligning addresses to 4-byte boundaries. So char name[10] is 10 bytes, but 10 isn't an exact multiple of 4, so it rounds it up to 12. 12+4=16, which is the result you get. 100 *is* an exact multiple of 4 (25*4) so there is no need to move data to 101, 102 or anywhere else. 100+4=104, as you have observed. 160 is completely wrong, you are not making the correct computation. If you defined name as char[102] then this would take up 104 bytes and the result would be 108. Try it! Define name as char[1], then char[2], then char[3] and so on. Or start somewhere else and increase the size by 1 each time. You will see then that word alignment will result in data moving up memory 4 bytes at a time.