Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
are array members successively located in memory? is the following code right?
C++
int A[5] = {1, 2, 5, 7, 9};
file.write((char*)a, 5*sizeof(int));

what about structures?
Posted

You can do it this way if you are sure that your 'size' value is correct; structures can also be written the same way, assuming they are simple structures of elemental items. However, complications can set in when structures or arrays contain pointers to objects. It is much better to use structures or classes that serialise themselves so that you do not need to change your code when the structure or class changes.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Nov-11 20:04pm    
Wise advice, a 5.
--SA
Richard MacCutchan 3-Nov-11 3:48am    
Thanks!
Joseph Marzbani 3-Nov-11 0:37am    
but I don't have the answer to the first part of my question yet :-( are array members successively located in memory? (sorry for my bad English)
Richard MacCutchan 3-Nov-11 3:48am    
Yes, it would be a very strange compiler that did not locate them in this way. However I still believe it is better practice to save elements individually rather than as a 'block', as you will then get used to following the same process when dealing with classes and structures.
Your code is fine (almost - see below). For structures, basically the same. Use sizeof() always, since the compiler might not generate the structure size you might think.

C
typedef struct xyz {
int a[5];
char b[7];
double c[2];
struct xyz *next;
char z;
} sxyz;

sxyz arr[8];

...

file.write(arr, sizeof(arr));  // right
file.write(&arr[0], 8 * sizeof(sxyz));  // wrong

Note I've done two things in two different ways:
1. arr in this context (as a pointer) means the same as &arr[0] - the address of the first element of the array. Both versions are correct in this regard.
2. (No of structs) * (size of struct) is not necessarily equal to size of array of structs

As an exercise, try and work out in your head the size of sxyz above. Compare that with what your compiler says.
[optional advanced exercise]Find the compiler option that changes the size of sxyz without breaking your program. What does that do to the size of arr?

Cheers,
Peter
 
Share this answer
 
Comments
Richard MacCutchan 2-Nov-11 19:03pm    
A great example of what I warned against in my post. The xyz structure contains a pointer within it and you have no way of knowing whether that points to another item in the sxyz array or some piece of memory that was allocated elsewhere, by new or malloc()! The chances are that writing this in the way you describe will lose what may well be vital data.
Peter_in_2780 2-Nov-11 20:01pm    
OK. Silly of me to chuck a pointer in. Didn't really mean to unleash the serialisation can o'worms. I was just answering OP about structs and making a point about quirks of size.
Joseph Marzbani 3-Nov-11 0:36am    
but I don't have the answer to the first part of my question yet :-( are array members successively located in memory? (sorry for my bad English)
Peter_in_2780 3-Nov-11 1:33am    
The answer is yes. Array members are stored in the natural order in memory.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900