Structures And Pointers by Anand George Inside C
Structures And Pointers by Anand George
Inside C Structures • Contiguous memory like array. • Name of the struct variable points to the beginning of the chunk of memory allocated to struct variable again like array. • Bit more complication in indexing elements as different member are of different size unlike array. • As elements are of different type compiler additional bytes called padding to make sure the alignment is correct.
Memory Alignment • A restriction from CPU which should be respected by the compiler. • Due to performance reasons mostly of the commercially available CPU mandates access to the memory to be some x byte aligned. • Normally x = 4 in a 32 bit OS. • Compiler can force the CPU not to follow alignment at the cost of performance.
Structure Padding • Additional memory byte compiler adds to the structures to correct the alignment. • Not specific to structures but local variables also have this problem. • More visible in the case of structures. • By the nature of language compiler cannot rearranged the members inside a struct such that padding will be less. So we have to take care of this fact when designing the structure.
Demo • Looking at memory of a struct variable in Visual Studio. • Look at the asm of the generated code. • Looking at size of struct • Looking at structure padding. • How to reduce padding. • Understanding padding in local or stack variable.
Access struct variable with struct pointer. • Arrow operator “->” mystruct mystr; mystruct* pmystr = &mystr; pmystr->element 1; pmystr->element 2;
Demo • Arrow operator ->
Finding offset of a member element in struct. • Structure has different types of elements. • More than that it has padding which is given by the compiler based on some setting etc which we cannot correctly predict all the time. • Now how to get the offset of a member element from the beginning of the structure?
So. . here things get a bit complicated and you will say “pointers are difficult” struct my. Test. Struct { int a; char b; short c; int d; }; void main() { int* theoffset = &(((my. Test. Struct*)0)->d); } • After execution of above code theoffset will contain 8 which is the offset of variable d from the start of the structure my. Test. Struct.
Why this is happening? • You are assuming some data at location 0 is of type my. Test. Struct by typecasting 0 to my. Test. Struct* (my. Test. Struct*)0 • So beginning of the structure variable which is the pointer to the beginning of the structure is 0. • Now all you are doing is asking compiler the address of member variable d. • Which is the offset of d from the beginning address of the structure as beginning is nothing but 0. &(((my. Test. Struct*)0)->d); • Unfortunately we cannot prove above argument by looking a disassembly as even when all optimization turned off all complier generate for above line of code is • mov dword ptr [offset], 8 Note: The code above is not portable although all the major (gcc, msvc)compliers are fine with it. It is buggy and against the standard as it appears to dereference a null pointer.
Why do I need offset first off all? • Complier handles most of the cases with arrow and dot operators. • So most cases you don’t have to care it. • But in some cases you may get addresses which point to the middle of the structure. • In that case you need to know the offset of the member and subtract it from the address of it to get into the beginning of structure. • CONTAINING_RECORD macro in windows header files use this a lot. • And the technique mentioned above is practically everywhere used in windows and Linux kernel. • We will discuss this in detail when we discuss self referencing structures.
Demo • Finding and using offset.
Some facts about structures. • Structure can contain structures, unions or array or any other data type for that matter. • We treat a once defined structure like another data type say int, char etc. • For example we can create array of structures, get address of it, get address of members of it, pass to other functions or anything like that.
Demo • Array of structures. • Structure inside structure.
Thank you
- Slides: 15