Hello everyone!
I am stuck on a strange problem -
code -

const int buf_size = ((aFormatCtx->bit_rate)*2);

	for (;;)
		static int16_t buffer_out[buf_size];
		ring_buf->read((unsigned char*)buffer_out, buf_size);
		ao_play(device, (char*)buffer_out, buf_size);

Now, I want the buffer_out array to be twice the size of stream bitrate. However g++ complains that "size of buffer_out is not constant". I need that buffer size. If I type some integer in it, it is ok. Please help!

Recommended Answers

All 6 Replies

Member Avatar for jencas

The size of the array must be const at compile time in C++. AFAIR dynamic arrays are only part of the C99 standard.
Try std::vector if you are using C++. Or boost::array.

>If I type some integer in it, it is ok.
The error is correct, but not as informative as it could be. Just because you declare a variable to be const doesn't mean that it's guaranteed to be a compile-time constant. In this case, aFormatCtx->bit_rate probably can't be resolved at compile time, so you can't use the resulting const value as the size of an array.

If any part of the size is calculated at runtime, you have to simulate arrays using pointers and dynamic memory (not recommended), or you have to use a container class that manages the memory for you, such as std::vector.

Thanks for that, I did not know that! I'll try to figure something out!

Thank you.

static int16_t buffer_out[buf_size];

change into:
int16_t buffer_out[buf_size];


>change into:
wrong. It's not constant because it has expressions that change. Thus you need dynamic memory (allocated with new ) or a type that's managed automatically (like Narue said, std::vector for example).

Right, Narue? *still needs to learn alot*

commented: Yes, that's it. +18

static int16_t buffer_out[buf_size];

change into:
int16_t buffer_out[buf_size];


he... he...
That has worked. Thanks very much. I guess it is because the array was static that it didn't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.