• C

Getting rid of set sized arrays in struct

Hi *,

Currently I have structs as in the first attached example. I do not want to have to preassign the size of arrays, so need to change to the second attached example.

What is the best way to do this so that code using these structs will not be changed.

Currently, I am changing the struct to as the second example, and then in my "constuctors" calloc'ing appropriate memory. But I am getting segmentation faults all over the place. For example, I will first change x so it is a pointer *x. This works, only if it is at the end of the struct, and not anywhere else. This doesn't seem to make sense. I am calloc'ing memory, so it should matter where in the struct, right?

Cheers,
James
typedef struct {
    int x[10];
    int y[10][20][3000];
    float z[10][10];
} name;

typedef struct {
    int *x;
    int *y;
    float *z
} name;

Open in new window

LVL 4
James_h1023Asked:
Who is Participating?
 
HooKooDooKuCommented:
Syntactically, your change is fine (hence the reason you are not getting compile time errors).  

But there really isn't any substitute for reviewing the code that uses this structure.

There are two main issues I see that could occur.
1. A subroutine attempts to access an element of one of these arrays, and the memory location is beyond the memory you've allocated.  For example, you malloc x to be size 5, and some piece of code tries to access the 6th through 10th elements expecting there to be at least 10.  (If your malloc sizes the arrays such that the hard coded values are the minimum size, and the malloc is ALWAYS valid before anything that ever executes against the struct, then this is not an issue).
2. Code has been written to do something based on the sizeof as it relates to this structure.  For example, if the structure defined a record layout in a file, someone might be reading the file with something like...
   f.Read( &MyStruct, sizeof( MyStruct) )
... to read an entire record in one command.  But if you've changed the structure from a fixed size array to simple pointers, you've GREATLY changed the results of the sizeof().
0
 
James_h1023Author Commented:
Thanks for a reply.

I think it might be 1. Although, I am not changing any functions or subroutines, when I change from a static array to a pointer, and I am definately assigning enough memory.

Am I correct in the following:
A static array inside a struct becomes part of the struct memory, whereas a pointer (array) is allocated from the heap?

Attached is an example of strange behaviour. For example, the second struct would not work, and the first will. More specifically, in the first example, I make my struct, and malloc *f. Then if I change some values in x, anything to *f will then segment, however, if I change *f before changing x it wouldn't segment! I don't see how then can interfere. However, if I use the second example I can use *f even after changing x as one would expect.

Cheers,
James
typedef struct {
    int *f;
    int x[10][10];
} name;

typedef struct {
    int x[10][10];
    int *f;
} name;

Open in new window

0
 
HooKooDooKuCommented:
It shouldn't matter where the memory is located (heap or program memory or something else) everything should still work the same.

However, with the second struct, the location of the f pointer is immediately after the x array.  So if you executed a statement like...
  x[10][0] = 0;
that begins to update memory PAST the end of your array, you risk modifying the value of the f pointer.  Doing so basically means the f pointer no longer points to your malloc memory, but to some random memory address that you might not have legal access to (causing an exception when you attempt to access the memory f is pointing to).

But in the 1st structure, since the f pointer is in memory BEFORE the x array, accessing memory beyond the array doesn't impact the value of the f pointer.
0
 
James_h1023Author Commented:
Brilliant, thanks that's what I though. I must be writing/reading somewhere i'm not allowed to do.

Cheers,
James
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.