1

I'm having trouble in adding integers to a dynamic array reader, so the second function in the code below. Why do the added numbers in the output look weird? I'm guessing there's a memory problem, I'm somehow allocating new memory incorrectly.

Output

int *create_dyn_array(unsigned int n)
{
    
    int *array = malloc(n * sizeof(*array));

    for (size_t i = 0; i < n; i++) {
        scanf("%d", &array[i]);
    }

    return array;
}

int *add_dyn_array(int *arr, unsigned int num, int newval)
{

    int *temp = NULL;

    temp = realloc(arr, (num + 1)*sizeof(int));
    arr = temp;

    int *newarray = arr;
    while(*arr) {
        arr++;
    }

    int testarray[1];
    int *ptr = testarray;
    int j = 1;
    while (j > 0) {
        *ptr = newval;
        ptr++;
        j--;
    }
    ptr = testarray;

    while(*ptr) {
        *arr++ = *ptr++;
    }

    return newarray;
}

void printarray(const int *array, int size) {
    printf("{ ");
    for (int i = 0; i < size; ++i) {
        printf("%d, ", array[i]);
    }
    printf(" }\n");
}

int main()
{
    int *array = create_dyn_array(5);
    printarray(array, 5);
    
    array = add_dyn_array(array, 5, 10);
    printarray(array, 6);
    
    array = add_dyn_array(array, 6, 100);
    printarray(array, 7);
    
    array = add_dyn_array(array, 7, 1000);
    printarray(array, 8);
    
    return 0;
}

What is wrong in the realloc logic?

3
  • 1
    What is that ritual supposed to do? The add should have been just realloc(...);temp[num]=newval; return temp;, nothing more should be needed. That is if you're trying to add newval to the end of the array. If that was supposed to do something else, explain that in the question. Commented Feb 17, 2021 at 13:21
  • If you're trying to add multiple elements and fill them with newval, you need to know the previous size and the new size. You cannot assume that the extra elements added by realloc will have any specific value initially. Commented Feb 17, 2021 at 13:26
  • Please do not add pictures of text. Add it as text to your question instead. Commented Feb 17, 2021 at 13:28

1 Answer 1

3

This is how realloc works:

The contents of the new object shall be the same as that of the old object prior to deallocation, up to the lesser of the new and old sizes.

No need to copy anything manually. The whole function can be reduced to this:

int *add_dyn_array(int *arr, unsigned int num, int newval)
{
  int *temp = realloc(arr, (num + 1)*sizeof(int));
  if(temp == NULL)
  { 
    /* optionally handle errors in some way */
    exit(EXIT_FAILURE);
  }
  temp[num] = newval;

  return temp;
}

Please note however that reallocing one single item at a time is very inefficient.

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

7 Comments

The efficiency depends on the implementation. On my GNU/Linux x84-64 system, a simple loop from 1 to 1000000 reallocating the same block to the length specified by the loop index counter only changed the returned pointer 20 times.
@IanAbbott sure, but hope for the best and be prepared for the worst.
@IanAbbott Did you write to the memory also? If not, then the OS will probably optimize the actual allocation and refrain from doing it until the memory is actually needed.
@Lundin I neglected to write to the memory, but after a slight tweak to write the last byte of the block after the realloc, there is no change to the number of times the block is reallocated (although sometimes it is reallocated 20 times and sometimes it is reallocated 24 times).
Thanks for the help. Next time I'll just delete 3/4 of the code and perhaps it works right away, heh.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.