10

I'm trying to make a variable sized array in c.

The array keeps on coming back as having a value of -1.

What I want to do is to make an array of size size and then incrementally add values to it. What am I doing wrong?

int size = 4546548;

UInt32 ar[size];
//soundStructArray[audioFile].audioData = (UInt32 *)malloc(sizeof(UInt32) * totalFramesInFile);
//ar=(UInt32 *)malloc(sizeof(UInt32) * totalFramesInFile);
for (int b = 0; b < size; b++)
{
    UInt32 l = soundStructArray[audioFile].audioDataLeft[b];
    UInt32 r = soundStructArray[audioFile].audioDataRight[b];
    UInt32 t = l+r;
    ar[b] = t;
}
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
dubbeat
  • 7,706
  • 18
  • 70
  • 122

4 Answers4

9

What you need is a dynamic array. One that you can allocate an initial size, then use realloc to increase the size of it by some factor when appropriate.

I.e.,

UInt32* ar = malloc(sizeof(*ar) * totalFramesInFile);
/* Do your stuff here that uses it. Be sure to check if you have enough space
   to add to ar and if not, call grow_ar_to() defined below. */

Use this function to grow it:

UInt32* grow_ar_to(UInt32* ar, size_t new_bytes)
{
    UInt32* tmp = realloc(ar, new_bytes);
    if(tmp != NULL)
    {
        ar = tmp;
        return ar;
    }
    else
    {
        /* Do something with the error. */
    }
}
jer
  • 20,094
  • 5
  • 45
  • 69
  • just on a note. if totalframesinfile is 50 does that mean that there are 50 spaces in the array and each space is of size UNint32? – dubbeat Nov 24 '10 at 17:55
4

You should probably allocate (and subsequently free) the array dynamically, like so:

int *ar = malloc(sizeof(int) * size);
for (int b = 0; b < size; b++)
{
    ...
}

// do something with ar

free(ar);
Wyatt Anderson
  • 9,612
  • 1
  • 22
  • 25
1

if you make size a const int that should work. Also, if your array is inside a function and size is an argument of said function, that should work too.

Hervé
  • 275
  • 4
  • 5
-1

C does not allow a variable to be used when defining an array size, what you'd need to do is use malloc, this should give you an idea:

UInt32* ar;
ar = (UInt32*) malloc(size * sizeof(UInt32));

Don't forget to free it up afterwards

Argote
  • 2,155
  • 1
  • 15
  • 20
  • "C does not allow a variable to be used when defining an array size" is what I did not know – dubbeat Nov 24 '10 at 17:51
  • 5
    @dubbeat: C (the current version, C99) does allow it. C89 doesn't. Compilers that don't support it should give an error message at compile-time ("Constant expression expected", or similar), not compile but give you an unexpected value of -1. What compiler are you using? – Steve Jessop Nov 24 '10 at 17:51
  • Ah, my bad then. True since he was not getting a compile error – Argote Nov 24 '10 at 17:55
  • @dubbeat: in that case VLAs are supported (with -std=c99), and I'm pretty sure the problem here is either that you're running out of stack and something undefined happens (I'd expect a segfault+coredump, but you never know), or else there's some unrelated bug in the code to fill the array, which is causing the unexpected -1 value. Variable length arrays (VLAs) are quite hard to use safely, personally I think they're a bit pointless. – Steve Jessop Nov 24 '10 at 18:04