Re: sizeof([ALLOCATED MEMORY])



"Howard Hinnant" <howard.hinnant@xxxxxxxxx> wrote in message news:howard.hinnant-EED144.10183104052006@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
No matter what the growth strategy for capacity, its existence is a
major motivation for finding out the amount of memory actually
allocated. create_array_short(N) may request only N*sizeof(short)
bytes, but may receive slightly more than that (for alignment purposes
or whatever). If create_array_short(N) had some way to find out how
much memory it actually received, then it makes perfect sense to set
capacity to size_received/sizeof(short) instead of to N. It makes for
higher performing code in the average case.

You're over-optimizing here. If malloc() returns more memory than you asked for, when you expand the array, you'll get access to it. On average, you'll perform the same number of realloc()s with or without this optimization unless you're expanding by a very small amount each time, in which case a large fraction of your realloc()s will be no-ops for the implementation.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin


*** Posted via a free Usenet account from http://www.teranews.com ***
.



Relevant Pages

  • Re: Linuxs approaching Achilles heal
    ... If you request a massive amount of memory that's probably because of a bug or malicious input, so it is unclear what to do in that situation anyway. ... xmallocsupposes that termination can be used as a last / first resort, ...
    (comp.lang.c)
  • Re: Linuxs approaching Achilles heal
    ... resort to doing something much worse. ... If you request a massive amount of memory that's probably because of a bug ...
    (comp.lang.c)
  • Re: [patch 04/22] fix deadlock in throttle_vm_writeout
    ... submitting a write request. ... It is easy to reproduce on a machine with not too much memory. ... The deadlock doesn't happen immediately, ... The patch limits the amount of memory which is under pageout writeout to be ...
    (Linux-Kernel)
  • Re: [patch 04/22] fix deadlock in throttle_vm_writeout
    ... submitting a write request. ... It is easy to reproduce on a machine with not too much memory. ... The deadlock doesn't happen immediately, ... The patch limits the amount of memory which is under pageout writeout to be ...
    (Linux-Kernel)
  • Re: size of huge memory
    ... > we expand the memory size to load huge amount of data? ...
    (comp.lang.c)