User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,609 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,615 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 240 | Replies: 6
Reply
Join Date: Jul 2007
Posts: 37
Reputation: linux0id is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
linux0id's Avatar
linux0id linux0id is offline Offline
Light Poster

How to make an integer constant

  #1  
Jul 8th, 2008
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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Location: Germany
Posts: 33
Reputation: jencas is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
jencas jencas is offline Offline
Light Poster

Re: How to make an integer constant

  #2  
Jul 8th, 2008
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.
Last edited by jencas : Jul 8th, 2008 at 2:52 pm.
If you are forced to reinvent the wheel at least try to invent a better one!
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: How to make an integer constant

  #3  
Jul 8th, 2008
>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.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Jul 2007
Posts: 37
Reputation: linux0id is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
linux0id's Avatar
linux0id linux0id is offline Offline
Light Poster

Re: How to make an integer constant

  #4  
Jul 8th, 2008
Thanks for that, I did not know that! I'll try to figure something out!

Thank you.
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: cikara21 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
cikara21's Avatar
cikara21 cikara21 is offline Offline
Newbie Poster

Re: How to make an integer constant

  #5  
Jul 9th, 2008
remove:
static int16_t buffer_out[buf_size];

change into:
int16_t buffer_out[buf_size];

.......he..he
.:-cikara21-:.
Reply With Quote  
Join Date: Aug 2007
Posts: 128
Reputation: hacker9801 is on a distinguished road 
Rep Power: 2
Solved Threads: 13
hacker9801 hacker9801 is offline Offline
Junior Poster

Re: How to make an integer constant

  #6  
Jul 9th, 2008
>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*
Last edited by hacker9801 : Jul 9th, 2008 at 5:13 am.
Don't yell at me if I'm wrong - I'm thirteen. :)
Reply With Quote  
Join Date: Jul 2007
Posts: 37
Reputation: linux0id is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
linux0id's Avatar
linux0id linux0id is offline Offline
Light Poster

Re: How to make an integer constant

  #7  
Jul 10th, 2008
Originally Posted by cikara21 View Post
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC