Frank F
Frank F

Reputation: 37

Dynamic array allocated, but cannot use it

I would love to use this code as a dynamic array. Unfortunately, I cannot figure out why the program does not use the allocated memory. Is there something wrong with the parameters of the AddToArray function, maybe?

It is possible to copy and paste this code directly into the IDE and compile, to take a look at the output. The memory seems to be allocated but not used?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    float x;
    float y;
} DATA;

int AddToArray (DATA item, 
                DATA **the_array,
                int *num_elements,
                int *num_allocated)
{
  if(*num_elements == *num_allocated) 
  { // Are more refs required?

    // Feel free to change the initial number of refs
    // and the rate at which refs are allocated.
    if (*num_allocated == 0)
    {
      *num_allocated = 3; // Start off with 3 refs
    }
    else
    {
      *num_allocated *= 2;
    }// Double the number of refs allocated

    // Make the reallocation transactional by using a temporary variable first
    void *_tmp = realloc(*the_array, (*num_allocated * sizeof(DATA)));

    // If the reallocation didn't go so well, inform the user and bail out
    if (!_tmp)
    { 
      printf("ERROR: Couldn't realloc memory!\n");
      return(-1); 
    }
    // Things are looking good so far, so let's set the 
    *the_array = (DATA*)_tmp;   
  }

  (*the_array)[*num_elements] = item; 
  *num_elements++;

  return *num_elements;
}

int main()
{  
  DATA *the_array = NULL;
  int num_elements = 0; // To keep track of the number of elements used
  int num_allocated = 0; // This is essentially how large the array is

  // Some data that we can play with
  float numbers1[4] = {124.3,23423.4, 23.4, 5.3};
  float numbers2[4] = { 42, 33, 15, 74 };
  int i;    
  // Populate!
  for (i = 0; i < 4; i++)
  {
    DATA temp;
    temp.x = numbers1[i];
    temp.y = numbers2[i];
    if (AddToArray(temp, &the_array, &num_elements, &num_allocated) == -1)
    return 1;               // we'll want to bail out of the program.
  }

  for (i = 0; i < 4; i++)
  {
    printf("(x:%f,y:%f)\n", the_array[i].x, the_array[i].y);
  }
  printf("\n%d allocated, %d used\n", num_allocated, num_elements);
  // Deallocate!
  free(the_array);  
  // All done.
  return 0;
}

Upvotes: 0

Views: 91

Answers (3)

Hans
Hans

Reputation: 269

The bug in your code is that the pointer is incremented, not the value.

..
    (*the_array)[*num_elements] = item; 
    (*num_elements)++;
..

Is this some kind of programming assignment? The code can be improved in many areas here. There are lots of good algorithms for this kind of problem that are well written and optimized, I suggest that you some research in that area.

Upvotes: 1

sb9
sb9

Reputation: 1082

try

(*num_elements)++;

in function

AddToArray()

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

In your code, you need to change

*num_elements++;

to

(*num_elements)++;

because, without the explicit parenthesis, the ++ is having higher precedence over * operator. What you want is to increment the value stored in the address, not the other way around.

Check about operator precedence here.

Upvotes: 3

Related Questions