0

I am working with dynamic arrays, consider two scenarios - Scenario 1:

typedef struct 
{
int *array;
int dataPtr;
} A;
static A dynaArray;
//Call the function as
dynaAdd(&dynaArray, <pointer to data block>, <length> );

static bool dynaAdd(A *data, int *dataCopy, int len)
{
    int newlen = (data->dataPtr + len); 
    data->array  = (int *) realloc(data->array, newlen * sizeof(int));
    if (!data->array)
    {
        return FALSE;
    }

    memcpy(&data->array[data->dataPtr], dataCopy,  len* sizeof(int));

   data->dataPtr += len; // update data ptr

 
    return TRUE;
}

I create a static struct A data (say) and pass to a function as pointer which reallocates length for A->array everytime and adds data to it, maintaining a dataPtr for index. This works perfectly fine where I can create an A->array of say length 100, use the data and free the pointer and null.

On the other scenario 2 -

static int *dynArray;
//Call the function as
dynaAdd(dynaArray, <pointer to data block>, <len>, <dataIdx> );
    
static bool dynaAdd(int *data, int *dataCopy, int len, int dataIdx)
{
    int newlen = (dataIdx + len); 
    data  = (int *) realloc(data, newlen * sizeof(int));
    if (!data)
    {
        return FALSE;
    }

    memcpy(&data[dataIdx], dataCopy,  len* sizeof(int));

   dataIdx += len ;

 
    return TRUE;
}

I just use int *array instead of struct, maintaining a static int dataPtr(say) to keep track of the next index, pass the array pointer to the function and dynamically grow the array and add contents to it. However, the program crashes after creating some x length array (which keeps varying). Can someone help understand the difference between the two approaches ? In both scenarios, the goal is to create a dynamic array and keep adding contents to it until the certain data index and then free the array. Thank you.

2
  • 2
    In order to know what you might be doing wrong, we need to see your code. Commented Mar 31, 2021 at 23:00
  • Edited the question to add more details Commented Mar 31, 2021 at 23:30

1 Answer 1

1

In the second example, you're modifying data which is local to the function. Such a change is not reflected in the calling function, so dynArray doesn't change. And since you reallocated memory, if the memory moved then this pointer is now invalid and attempting to dereference it triggers undefined behavior.

You need to change the function to accept the address of a int *, i.e. an int **, for the first argument and make the corresponding changes. You'll also want to make dataIdx a int * so changes to that are also propagated back

So your function would now look like this:

static bool dynaAdd(int **data, int *dataCopy, int len, int *dataIdx)
{
    int newlen = (*dataIdx + len); 
    *data  = realloc(*data, newlen * sizeof(int));
    if (!*data)
    {
        return FALSE;
    }

    memcpy(&(*data)[*dataIdx], dataCopy,  len* sizeof(int));

    *dataIdx += len ;

 
    return TRUE;
}
Sign up to request clarification or add additional context in comments.

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.