1

I've been searching all day but I can't find the one that can do what I'm looking for. I'm trying to find a way to create an array of pointers.

Like the Tree data structure, but instead of just left and right pointers I want to create a expandable array that I can store many pointers. Is there a way to do this?

struct Test{
    char label[100]; 
    float fear;
    float anger;
    float disgust;
    float sad;
    float happy;
    float surprise;
    struct Test *connect[];
};

I included my struct above, so what I want is the connect to be able to expand anytime I need it and at the same time store pointers to other Tests.


I tried using malloc but it seems like I'm not doing it right, here's my attempt:

typedef struct Test test;

In the function (*tst)->connect = malloc(sizeof(test));

1
  • 1
    It doesn't appear you need a flexible array member. I think just a size_t n_connect; and struct Test **connect; members would be sufficient. Obviously some calloc and realloc management would be in order to maintain those members. Commented Aug 15, 2015 at 19:50

4 Answers 4

1

You already defined what you need:)

struct Test{
    char label[100]; 
    float fear;
    float anger;
    float disgust;
    float sad;
    float happy;
    float surprise;
    struct Test *connect[];
};

So all you need is to allocate enough memory for an object of type Test.

For example

struct Test *objext = malloc( sizeof( struct Test ) + 10 * sizeof( struct Test * ) );

Data member connect is called a flexible array member.

Only it is desurable that you would add one more data member that will contain the number of the elements in the array.

Sign up to request clarification or add additional context in comments.

4 Comments

Don't understand what he will do with + 10 * sizeof( struct Test * ) memory? He has to do second allocation e.g.: objext->connect = malloc ( 10 * sizeof( struct Test * ) ).
@i486 He will store there his pointers to other objects of type Test.
Ok, I understood the idea. I assumed that connect is double pointer. If the author wants dynamically expandable connect member, it must be defined as struct Test **connect; and the size of Test object will be fixed.
@i486 There are two approaches: either to use a flexible array member or to make two allocations one for the structure and another for the array.
0

You need to look into malloc() and memory allocations. What you are essentially looking for is

dynamic memory allocation - allocating memory on the fly.

instead of static memory - allocating memory during compile time

9 Comments

I tried malloc but it seems I can't figure out the correct code for it. I tried this: (*Tst)->connect = malloc(sizeof(test)); Where 'test' is typedef struct Test test;
@Jeremy put your attempt in your question as an addendum
malloc stands for memory allocation and will create a memory location of size 1024 (for example) if you surpass that, you'll need to realloc() a bigger chunk or malloc() more memory and keep track of it. How you want to do this is all dependent on you but reallac() is much easier for memory management.
So if I want to add one pointer on each I need to realloc it like: ((sizeof(connect)/ sizeof(test)) + 1) * sizeof(test) Something like that?
more along the lines of current_size + sizeof(struct)
|
0

You can use realloc() to increase connect size. Also use struct Test ** type for it

1 Comment

By itself, the connect member can't be re-allocated using realloc since it's an FAM. The type struct Test** would allow it, although it has different semantics.
0

When you have an FAM in a struct, it is allocated inline with the struct. You need to realloc the entire struct to resize connect.

To expand a bit, in your struct

struct Test{
    char label[100]; 
    float fear;
    float anger;
    float disgust;
    float sad;
    float happy;
    float surprise;
    struct Test *connect[];
};

the member connect is an FAM. FAMs allows you to allocate a variable sized array inline with the structure. This is generally necessary when the number of pointer dereferences needs to be minimized. However, it's slightly more complicated than keeping a simple pointer.

To realloc a structure with an FAM with additional reserved memory, call realloc as follows...

test = realloc(test, sizeof(struct Test) + fam_cur_size + fam_add_size);

The function realloc preserves existing content, and will even re-use the same memory chunk when possible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.