Re: xcode-users digest, Vol 1 #493 - 15 msgs
Re: xcode-users digest, Vol 1 #493 - 15 msgs
- Subject: Re: xcode-users digest, Vol 1 #493 - 15 msgs
- From: Tobias Sargeant <email@hidden>
- Date: Wed, 7 Jul 2004 10:55:21 +1000
Message: 15
Cc: XCode XCode <email@hidden>, Pavol Markovic
<email@hidden>, "<email@hidden>"
<email@hidden>, Markus Hitter <email@hidden>
From: Markian Hlynka <email@hidden>
Subject: Re: Static data?
Date: Tue, 6 Jul 2004 16:26:35 -0600
To: Allan Odgaard <email@hidden>
remember, allocation on the stack happens when a function is called
too. in this case for sure, and _probably when an app launches,
int foo[1024][1024][1024];
should be faster than using malloc or new. malloc/new need to look for
memory in the heap and return it. Allocating on the stack is just a
matter of incrementing the stack pointer.
Setting aside for the moment that that array is 4G, which you have
no hope of allocating by any means...
Uninitialised data like this is recorded in the BSS section of the
executable. When the dynamic loader loads an executable, it maps
enough pages for the BSS section, and zeros them. This memory will
come from directly under the start of the heap.
malloc/new, by contrast generally does this:
1) compute log(2) of requested size, and use this to look in the free'd
memory bin, which is sorted by size, for a block that it can recycle.
if it can't, call brk() to get more memory.
2) mark out a bit more memory than you asked for, fill in a couple of
headers and link it into the malloc pool.
3) return a pointer to the allocated memory.
brk() doesn't zero memory, and doesn't have to find physical pages
immediately.
There's no rational reason to care about optimising a single set of
memory allocations that occur once at startup, and even if there was,
the immediate page allocation and zeroing of data that occurs when
your BSS is mapped is going to be slower than malloc in any case.
If you're doing something like this on the stack, all bets are off.
Yes, allocating on the stack does only decrement a stack pointer
(in fact, this happens anyway as part of the call frame setup, so
it's actually free), but the OS still has to map pages for you,
which will eventually take some (non trivial) CPU cycles (although
this is not done immediately, because they aren't cleared).
The amount of space that you can allocate on the stack is frequently
very limited, and relying on being able to allocate multi-megabyte
structures in this way is not sensible; this isn't what a stack is
meant for. If there isn't enough space on the stack, bad things will
happen without an error message.
Toby.
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.