C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
cut memory segment Posted by luzhell on 8 Apr 2005 at 12:46 AM
hi
does anyone know how i can decrease the size of allocated memory? for example if i have an array and i want to cut it after 400 entries and there should no memory be wasted
Report
Re: cut memory segment Posted by stober on 8 Apr 2005 at 6:23 AM
: hi
: does anyone know how i can decrease the size of allocated memory? for example if i have an array and i want to cut it after 400 entries and there should no memory be wasted
:

allocate the array dynamically, use malloc() for initial allocation, then use realloc() when you want to reduce or expand it. you can't change the size of a statically allocated array.
Report
Re: cut memory segment Posted by luzhell on 8 Apr 2005 at 6:45 AM
: : hi
: : does anyone know how i can decrease the size of allocated memory? for example if i have an array and i want to cut it after 400 entries and there should no memory be wasted
: :
:
: allocate the array dynamically, use malloc() for initial allocation, then use realloc() when you want to reduce or expand it. you can't change the size of a statically allocated array.
:
i thought i can use realloc only with sizes larger than the actual size. is this wrong?
would be nice...
Report
Re: cut memory segment Posted by stober on 8 Apr 2005 at 7:19 AM
This message was edited by stober at 2005-4-8 7:21:26

: :
: i thought i can use realloc only with sizes larger than the actual size. is this wrong?
: would be nice...
:

Yes, that is wrong.
http://www.opengroup.org/onlinepubs/007908799/xsh/realloc.html

realloc() can be used to shrink the allocation too, but that doesn't mean the program will gain more memory. It only means that the difference is now available to allocation by something else. malloc() and realloc() only work with contiguous memory locations, and most operating systems do not attempt to defragment memory unless there are two more adjacent free memory blocks.

So reducing the size of allocated arrays may, or may not, give your program more memory. There's a lot of ifs, ands and buts.


Report
Re: cut memory segment Posted by luzhell on 8 Apr 2005 at 7:48 AM
thx for your help man :)
very appreciated...



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.