Question:
Structures vs. Arrays, which is better?
?
2009-12-07 16:17:17 UTC
Let's say I want to define person within a program I am writing using the following traits: gender, height, weight, age

So I see 2 ways to tackle this, I can either write a structure or an array... which one should I do?

I understand that these would normally be two different data types (integer and string) but what if I said that a male is given 0 and female is given 1. Now I could have all the same data types, so I could do either of the following:

ARRAY.....
int person(4) = {0, 72, 175, 30};

- this creates a 30 year-old, 6 foot tall (72 inches) male who weighs 175 pounds

STRUCTURE.....
struct person {
int gender;
int height;
int weight;
int age;
};

person woman;

woman.gender = 1;
woman.height = 60;
woman.weight = 110;
woman.age = 22;

- this creates a 22 year-old, 5 foot tall female who weighs 110 pounds.



So both of these should work, but which one is better? or are they the same? Could someone please explain when should you choose to create a structure, and when should you choose to create an array?

Another related question: I know an array is a contiguous block of memory, is a structure as well?

Thanks! - I will choose best answer!
Four answers:
Cuong L
2009-12-07 16:37:00 UTC
When you work with program that deals with data structure as this one, structure is the best data type to use because when ever you want to access any data about the person, you can just do person.gender, person.height, etc... While array you have to remember which cell has data about gender, weight, etc.



Besides, in the future, if you need to keep more info. for more people, you can create array of structure while the array used to keep data about a person, you will have very hard time to keep track of data for each person.



every time you create variable of structure data type, OS will try to give you a contiguous block of memory. Same thing for array. There is no guarantee that your array can be contiguous block of memory.
Ratchetr
2009-12-07 16:50:38 UTC
Cuong is spot on.

Let me just add 1 more thing...

You forgot that people have names. And telephone numbers. And a home page URL.

Now you have a problem if you used an array: Your used an array of ints. While you could bodge a string of characters into an int array to store the name, things would get really ugly.



So structures...unless your language supports classes, in which case a class is even better.



On the contiguous question: A structure is contiguous, BUT...the compiler might add some padding bytes between members. A structure containing a 2 byte short, and 1 byte char and a 4 byte int might get laid out in memory like this:

0-1 short variable

2-3 padding

4 char variable

5 - padding

6-9 int variable.



So yes, 10 bytes to store 7 bytes of data. The compiler usually provides a way to disable this. (It can be a space vs. performance issue.)
anonymous
2009-12-07 16:43:34 UTC
Structures allow you to better type the variables, which means that your code is better documented and compilers can check more easily for errors - eg comparing gender with age, which would throw an error. In the array example, the only difference would be the index, which could only really be checked visually. The understandability of code gets even worse with arrays of arrays vs arrays of structures.



Less important these days, but still useful, is that the different types may be different sizes in a structure - very messy to do with arrays. A compiler can rearrange the order of a structure to put the different sizes of elements onto appropriate memory boundaries for maximum efficiency or maximum speed; if it's an array, you have to do it.



One thing old-school C programmers used to do is put string variables into an array. This meant you could pre-reserve space, but it was considered dangerous coding, as it was very easy to go outside the limits of the array, particularly as the sematics to get the address of the start were easy to get wrong - especially if you wanted to get the type of a "pointer to character" rather than "pointer to array". If that sounds messy, it was, and is exactly the sort of error that leads to security problems in code.



At the machine code level, the two may look identical once compiled, but why bother? It's safer, quicker and easier to code with the structure version.
Sean Grant
2009-12-07 16:20:02 UTC
ARRAY!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...