1

I am trying to set up an array of structs within a struct in C. The size of the array is not known until runtime, so I have tried to define the array as dynamic. I have reduced my problem to the code shown below (there are a few more members in each struct, but don't pose a problem).

//struct definitions
struct intStream {
int test;
};

struct io {
struct inStream **inputStream;
};

// then proceed to main
int numDevices = 4; //(derived from number of devices found at runtime)
struct io *devices;
devices->inputStream = malloc(sizeof(struct inStream) * numDevices)

// Fails on
devices->inputStream[0]->test = 1;

I am still relatively new to C style programming, so my understanding of memory is not complete, but through some searching I have not been able to find alternate solutions to my problem besides the struct hack which aesthetically I do not like, as well as wanting the option to include more arrays in future.

5
  • 1
    You have an unitialised variable devices. What do you think it is pointing at? Commented Nov 22, 2016 at 10:56
  • 2
    Please do not add answer to the question, thanks. :) Commented Nov 22, 2016 at 11:22
  • @n.m. I have been shown the error of my ways ;) not too sure what I expected looking back! Commented Nov 22, 2016 at 11:27
  • @SouravGhosh Sure, I just wanted to include a "fuller" version of the answer, thanks. Commented Nov 22, 2016 at 11:27
  • There is no array of structs in your code. Nor is there an array of pointers to struct in your struct. If you need a flexible array, use a flexible array member. Commented Nov 22, 2016 at 11:31

1 Answer 1

5

In this code, devices is uninitialized. Using uninitialized memory invokes undefined behavior.

You need to make devices point to some valid memory before you can dererefence that pointer.

That said,

 devices->inputStream = malloc(sizeof(struct inStream) * numDevices)

also looks wrong. What you may want is

 devices->inputStream = malloc(sizeof(struct inStream *) * numDevices);

and then, you need to allocate memory to each devices->inputStream[i], also.


[Modified code by OP, removed from question and added into the answer, just for reference]

struct io *devices;
devices = malloc(sizeof(*devices));  

devices->inputStream = malloc(sizeof(struct inStream *) * numDevices);

for(int i = 0; i < 4; i++) {  
    devices->inputStream[i] = malloc(sizeof(struct inStream));  
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the solution, makes perfect sense in the way you explained. I tried to put in a full form answer in this comment but it didnt work, so I have updated the question with your advice/solution too.
@user2142136 I have corrected and added your code in my answer itself, hope that's ok. :)

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.