3

I have found many 'pointer becomes NULL' questions, but struggling to apply the answers to my problem

I have 3 nested structs. Struct A should contain an array of struct B, which contains a single struct C which itself should contain an array of int:

typedef struct {
    int32_t *data;
    uint16_t nData;
    uint16_t dataOffset;        
} Window;

typdef struct {
    Window *window;
    const char *datasetPath;
} Dataset

typedef struct {
    char *id;
    uint8_t count;
    Dataset *datasets;
} DatasetGroup;

I have a new function for the structs:

int8_t dataset_new(Dataset *ds, const char *datasetPath, uint16_t winSize){
    ds->datasetPath = datasetPath;
    ds->window = malloc(sizeof(*(ds->window));
    ds->window->data = malloc(sizeof(int32_t) * (winSize));
    return 0;

int8_t datasetgroup_new(DatasetGroup *dg, char *id){
    dg->id = id;
    dg->count = 0;
    dg->datasets = malloc(sizeof(*(dg->datasets)) * 255);
}

And I have an add function to add a Dataset to the DatasetGroup:

int8_t datasetgroup_add(DatasetGroup *dg, const char *filePath, uint16_t winSize){
       // Create the dataset
       Dataset ds; 
       dataset_new(&ds, filePath, winSize);

       // Add the dataset to the dataset array
       dg->datasets[dg->count] = ds;
       dg->count++;
       return 0;
       }

I then iterate through the datasets in order to populate the data, doing stuff like:

     for (i = 0 ; i < datasetCount ; i++){
         Dataset *ds = &(dg->datasets[i])

Always on the 2nd interation, the data array becomes a null pointer: data = ds->window->data

I understand that I have done something wrong passing pointers around(?) but I'm not sure what precisely I have done wrong....

6
  • 2
    The posted code looks OK to me. Can you post an MCVE? Commented Apr 28, 2015 at 16:11
  • @RSahu I'll have to spend a bit of time cutting stuff out to try and get it to the minimum that still causes it.... (probably a good exercise any way). I'll work on it.... Commented Apr 28, 2015 at 16:16
  • The error is probably elsewhere. Get your program running in a debugger. Identify the address of the pointer that eventually becomes null, and set a data breakpoint on that address (i.e. something like &dg->datasets[1].window->data). Commented Apr 28, 2015 at 16:26
  • It looks like in datasetgroup_add() you are declaring ds on the stack, which can/will get overwritten once ds goes out of scope when you return from datasetgroup_add(). Try moving ds to the heap. Commented Apr 28, 2015 at 18:38
  • 1
    "losing" is relative, that address in memory still exists, but the addresses saved in ds.window and ds.datasetPath are getting overwritten, i.e. the memory location where you are declaring ds is being reused for other stuff. Try changing ds to a pointer that you then malloc and see if that fixes it. Commented Apr 28, 2015 at 20:09

1 Answer 1

1

This looks fine. So check another part of the code. You can debug code line by line so you can understand the segment which is creating this error.

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

1 Comment

You are right, thanks. Error was elsewhere..a simple mistake when allocating memory meant I wasn't allocating memory for more than 1 structure (I thought I was)

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.