Dynamically Allocated Arrays

Hey all! I'm in the midst of writing a simple program that converts tabulated data from a CSV file and converts it to a WAV file. What I would like to know is if there's a way to dynamically add memory to an array of type "short * ". What i want to do is iterate through each line of the CSV file (containing a frequency, amplitude and period), generate the appropriate digital signal (using a sin function), and append each calculated datum to the array. I want to do this without having to predetermine the length of the file. So is there a way to dynamically add memory space to the end of a short * array, and fill it with data, and repeat through a for loop? Thanks!
What language are you using, C or C++?

For C checkout malloc, realloc, calloc, free.

For C++ new, delete, or perhaps one of the standard containers like std::vector.



Last edited on
Using C++. I'm not going to use vectors. This is strictly an exercise in dynamic memory allocation. What I'm asking is specifically whether you can "add" memory to an array without deleting what's already in the array, then filling the added memory space with data.
Arrays in C/C++ are of fixed sizes so you can't just change the size. However if you have allocated the array with new then you can "add" to the size of this array by creating a new larger dynamically allocated array, copying the original array contents to this new array and "swapping" pointers. But be sure you delete the original array before you do the swap. I would do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstring>
#include <iostream>

int* changeSize(int *old, size_t old_size, size_t new_size);

int main()
{
   size_t size = 10;
   size_t new_size = 20;
   int *array = new int[size];

   // Fill in this array.
   for(size_t i = 0; i < size; ++i)
      array[i] = (i + 1) * 10;

   // Print this array.
   for(size_t i = 0; i < size; ++i)
      std::cout << array[i] << " ";
   std::cout << std::endl;

   // Change the array size, add elements.
   array = changeSize(array, size, new_size);

   // Print the new array.
   for(size_t i = 0; i < new_size; ++i)
      std::cout << array[i] << " ";

   return(0);
}

int* changeSize(int *old, size_t old_size, size_t new_size)
{
   // Create a new larger array.
   int *new_array = new int[new_size];
   // Copy the old array to the new array.
   memcpy(new_array, old, old_size*sizeof(int));
   delete[] old;
   // Just populate the array for demonstration.
   for(size_t i = old_size; i < new_size; ++i)
      new_array[i] = (i+1) * 10;
   return(new_array);
}


I didn't do much error checking, you will probably want to insure that the new array was successfully created before you do the copy, check that the copy operation succeeded before you delete the old memory.

Edit: And don't forget to delete the array when you are finished with it.

Last edited on
That's essentially what I was trying to accomplish, but I forgot that I could switch pointers! Haha, thanks for your help!
Topic archived. No new replies allowed.