Struct array memory layout of the elements

In summary: The compiler will automatically multiply the index value by the sizeof(Person) and add that result to the pointer.Another thing to watch out for is the order of the fields in the structure. The compiler may add padding between fields, depending on the type of each field and the platform being used (x86, x64, PowerPC, etc.). You can use the sizeof operator to determine the size of a structure, but sometimes you have to pad a structure to ensure that the compiler doesn't add its own padding.I'm curious, did you get the correct answer? ;-)In summary, the conversation discussed the creation of a data structure and the need for help with understanding memory layout and addresses of its elements. The person asking the question also
  • #1
Sumaya
29
0
hi every one ,

i am new in data structure course

i need help in this :

Code:
Struct Person 
{
Char name[20];
int id;
float gpa;
};
Person student[5];

Compute the addresses of all the elements of array student and show a memory layout for the array if the address of the first element is 0x0012FF1A.

some what i did :
i did account that there is 28 bytes = 1c in hex
and i know that i have to add 1C to the address 0x0012FF1A and i think i'll get 0x0012FF36

my problem is if the first address 0x0012FF1A is the address for the first byte student[0]

so should i keep adding 1C to the previous address until i arrive to student[5]

just wondering and also how it is look like the memory layout . is there 28 bytes between every array like between student[0] and student[1]

thanx alot
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
First note that most compilers by default put each member on a word or long boundary, especially if you have speed optimization enabled. You can override that with certain compiler directives (like 'pack') or command line options. However it looks like each member in that struct is already a multiple of longs, so it should already be packed.

student[0] is a struct.
&student[0] is the address of the first struct.
student[4] is the last element (not student[5].

But in answer to your question, yes you can add 0x1C to iterate the elements but only if you know they are packed (and I think they are from the size of them) but sometimes compilers optimize in strange ways, so be careful. using sizeof should be useful.
 
  • #3
On top of what fleem said above, if you want to use absolute alignment (i.e. byte alignment and not word alignment), you need to tell the compiler, and that often means using a compiler directive.

For MSVC, I think it is a pragma macro (pack maybe?).

Also you should not use definitions like int or float directly. Create an include file that guarantee's data structures to be of a specific type. Use things like INT32 or UINT32 where that definition absolutely has a guarantee that the particular data type, no matter what platform is used is in fact a 32 bit signed or unsigned integer. When the platform is different, just add a compiler directive and then based on that write the appropriate typedef to force it to the right data type.
 
  • #4
thanx a lot for every one ...
 
  • #5
The syntax is not quite right you either want:

Code:
struct Person 
{
char name[20];
int id;
float gpa;
};

struct Person student[5];

or

Code:
typedef struct
{
char name[20];
int id;
float gpa;
}Person;

Person student[5];

Compute the addresses of all the elements of array student and show a memory layout for the array if the address of the first element is 0x0012FF1A.
I'm assuming you've completed this assignment. If you have access to a compiler you can check your answers, by declaring 0x0012FF1A to be a pointer to the structure, then printing out the addresses of the elements of an array of these structures. A pointer to an object can be indexed, in which case the compiler assumes the pointer is the address of the first object in an array of objects.

Code:
#include <stdio.h>

typedef struct
{
char name[20];
int id;
float gpa;
}Person;

int main()
{
int i;
    for(i = 0; i < 5; i++){
        printf("adr person[%d].name = %x\n", i, &((Person *)(0x0012FF1A))[i].name);
        printf("adr person[%d].id   = %x\n", i, &((Person *)(0x0012FF1A))[i].id);
        printf("adr person[%d].gpa  = %x\n", i, &((Person *)(0x0012FF1A))[i].gpa);
    }
    return(0);
}
 
Last edited:
  • #6
I just happened to re-read this thread and thought of something else that should be mentioned in case you are doing it but didn't mention it in your post. That something is how pointer arithmetic works in C/C++. When you add a value to a pointer, the compiler actually adds that many structure sizes to the pointer, instead, because it assumes you are working with an array of those structures. for example, if you have a pointer to a structure of size 7, then adding a 3 to it will really be like adding a 21 to it.
 
  • #7
fleem said:
... how pointer arithmetic works in C/C++. When you add a value to a pointer, the compiler actually adds that many structure sizes to the pointer, instead, because it assumes you are working with an array of those structures. for example, if you have a pointer to a structure of size 7, then adding a 3 to it will really be like adding a 21 to it.
You can also use an index with the pointer, as shown in the code fragment in my previous post where I delcare 0x0012FF1A to be a pointer to Person, then index that pointer ( ... ).
 
Last edited:

What is a struct array?

A struct array is a data structure that allows multiple elements of different data types to be stored in a sequential manner. It is similar to a regular array, but instead of storing a single data type, it stores a collection of data types grouped together as one unit.

How are the elements of a struct array stored in memory?

The elements of a struct array are stored in contiguous memory locations. This means that the elements are stored one after another in a linear manner, with no gaps or spaces between them.

What is the memory layout of a struct array?

The memory layout of a struct array is determined by the data types of its elements. Each element will take up a specific amount of memory, and the next element will be stored in the next available memory location.

Why is the memory layout of a struct array important?

The memory layout of a struct array is important because it determines the efficiency of accessing and manipulating the elements. If the elements are stored in a contiguous manner, it is easier and faster to access them compared to a non-contiguous layout.

Can the memory layout of a struct array be changed?

No, the memory layout of a struct array cannot be changed. Once the array is created, the elements will be stored in the same order in memory. However, the data in each element can be modified or rearranged as needed.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
5
Views
884
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
10
Views
6K
  • Programming and Computer Science
Replies
8
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
16K
Back
Top