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;
}
3
  • duplicate of stackoverflow.com/questions/448844/variable-sized-arrays-in-c Commented Nov 24, 2010 at 17:40
  • Code looks OK, but the size is big. Are you sure your program has 18MB of stack to use? Commented Nov 24, 2010 at 17:49
  • @Steve. I was getting confused about how many items I want my array to hold and the actual size of it. Commented Nov 24, 2010 at 17:54

4 Answers 4

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. */
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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?
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);

Comments

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.

Comments

-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

4 Comments

"C does not allow a variable to be used when defining an array size" is what I did not know
@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?
Ah, my bad then. True since he was not getting a compile error
@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.

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.