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->lockBuf();
		ring_buf->read((unsigned char*)buffer_out, buf_size);
		ring_buf->clearBuf();
		ring_buf->unlockBuf();
		pthread_mutex_lock(&play_mutex);
		pthread_cond_signal(&play_cond);
		pthread_mutex_unlock(&play_mutex);
		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!

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.

remove:
static int16_t buffer_out[buf_size];

change into:
int16_t buffer_out[buf_size];

.......he..he

>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

remove:
static int16_t buffer_out[buf_size];

change into:
int16_t buffer_out[buf_size];

.......he..he

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.