Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array of variable length structs (!) URGENT PROBLEM

Status
Not open for further replies.

vlakas1981

Programmer
Dec 11, 2002
40
GR
hi all,

this is my really urgent question:

I have an array of 256 elements, each element being the following struct:
struct coded_letter{
int size;
int *elem;
};

elem is a variable length int array.

i populate it, write it to a (binary) file, along with other data (an integer, and afterwards a series of bytes), and then try to read it, using the command

fread(l_table,sizeof(l_table),1,fps);

(l_table is again an array of 256 elements of the same struct)

when reading the values of this array, i get correct values for the l_table.size element, but not for the .elem element.
What is the problem???
Thanks in advance
 
sizeof will not report the size of arrays
using your method.

A method that is recommended for tracking array sizes is this:
int asz;
asz = sizeof I_table / sizeof I_table[0];


Good Luck.
 
[tt]elem[/tt] is NOT a variable length integer array! It is a pointer to an int which has a fixed length. When you write this array of structures, you are not writing what each structure's [tt]elem[/tt] is pointing to!

Also, the value that's in [tt]elem[/tt] is a memory address of the integer array that hopefully you've malloc'd somewhere else. This memory address will be different from run to run, so you just can't write it to a file and expect it to be correct when you read it back in. So, when you read the array back in, the size is correct, but the pointers are meaningless because you are running in different memory locations (a pointer is just a memory address).
 
Excuse me, I should have said number of elements, not size of
in the array, if that is what you would like to know.
For the elem element you will need to use a query like:
asz = sizeof I_table.elem;
for number of elements in the elem array:
aelements = sizeof I_table.elem / sizeof I_table.elem[0];

HTH
 
excuse me marsd, i didn't clarify what l_table.size is. It counts the number of elems each struct in the array has. So, to be totally clear, if i have l_table[k].size=3 then
l_table[k].elem[0], l_table[k].elem[1] and
l_table[k].elem[2] are the valid elems for the l_table[k]. I also don't need to count the size of the array, i know it. it is 256. My problem is with the value of each elem, which i can't get right.
Thanks
 
this is for sambones,

thanks for the reply first of all. i did indeed malloc each elem as i should. as for the pointer, you're right, but as i'm using it as a table (l_table[k].elem[d]) i used this expression. when i'm reading the values, as with when i'm writing them, i'm not reading elem, i'm reading l_table[k].elem[0], l_table[k].elem[1] etc. (pls look previous post for more details). I don't get any errors or warnings from the compiler.
Any other ideas??
 
The structure contains a pointer. If you write the structure out, all it will write out is the number of elements and the pointer: not what the pointer is pointing to.

The writing needs to be done in 2 stages:

1) write the number of elements
Code:
for (i = 0; i < 256; i++)
{
    fwrite (&l_table[i].size,sizeof(l_table[i].size),1,fps);
2) Write out the data
Code:
    fwrite (l_table[i].elem,sizeof (*l_table[i].elem),l_table[i].size, fps);
}

The reading has to be done in a similar fashion. Since it is a pointer, it cannot be read in: the space has to be allocated first
Code:
for (i = 0; i < 256; i++)
{
    fread (&l_table[i].size, sizeof (l_table[i].size), 1, fps);
    l_table[i].elem = malloc (l_table[i].size) * sizeof (*l_table[i].elem));
    fread (l_table[i].elem, sizeof (*l_table[i].elem), l_table[i].size, fps);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top