Solved

Getting rid of set sized arrays in struct

Posted on 2011-02-23
4
328 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:James_h1023
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 500 total points
ID: 34963817
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
 
LVL 4

Author Comment

by:James_h1023
ID: 34964996
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
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 34966214
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
 
LVL 4

Author Comment

by:James_h1023
ID: 34972295
Brilliant, thanks that's what I though. I must be writing/reading somewhere i'm not allowed to do.

Cheers,
James
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now