![]() |
| | #1 |
| Registered User Join Date: Nov 2005
Posts: 9
| A noob question - how to reduce the size of an array? 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 | |
| | #2 |
| Devil's Advocate 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 | |
| | #3 |
| Registered User 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |