C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-24-2005, 12:01 AM   #1
Registered User
 
Join Date: Nov 2005
Posts: 9
A noob question - how to reduce the size of an array?

Hi,

i have declared an array of class point.

point buffer[100];

but somewhere during my program, it is known that the size is smaller, says size 50.

how do i reduce the size of the buffer from there?

Thanks in advance!!
itachi is offline   Reply With Quote
Old 11-24-2005, 12:05 AM   #2
Devil's Advocate
 
SlyMaelstrom's Avatar
 
Join Date: May 2004
Location: Out of scope
Posts: 3,778
C doesn't allow you to do that. Nor does C++. What you could do is dynamically allocate an array, put what you want in it, then when you get the size you want, transfer that data to a newly allocated array of the appropriate size and delete the old one.
__________________
Terms of Service
By quoting or replying directly to this post, you consent to the fact that all of the information in the post above is completely accurate and highly intelligent and no comments will be made towards its validity, thoughtlessness, and/or grammatical structure.

Violators will be prosecuted to the fullest extent of the law.
SlyMaelstrom is offline   Reply With Quote
Old 11-24-2005, 01:30 AM   #3
Registered User
 
fischerandom's Avatar
 
Join Date: Aug 2005
Location: Stockholm
Posts: 71
SlyMaelstrom is right, that's the way to do it. But be aware that the technique he describes may fragment the heap and by doing so you might in practice end up with smaller free memory blocks in your heap which may have the effect that you can not allocate a memory block of a given size larger than the largest free block, even though the total amount of free memory is more than the block you try to allocate. That is if you don't use techniques such as handles (for example a char**) and use memory related functions for compacting the heap. Maby you should consider trying to do what SlyMaelstrom suggested only when you are out-of-memory.
fischerandom is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fixing my program Mcwaffle C Programming 5 11-05-2008 03:55 AM
Array size tigrfire C Programming 5 11-14-2005 08:45 PM
Class Template Trouble pliang C++ Programming 4 04-21-2005 04:15 AM
Something I'd like to share with all of you: simple frustum culling Silvercord Game Programming 11 03-25-2003 05:13 PM
Class member array size with constuctor initalizer rafe C++ Programming 2 10-14-2002 10:09 AM


All times are GMT -6. The time now is 08:04 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22