|
From: Jonathan W. <jon...@gm...> - 2007-07-17 22:03:14
|
Ok Adam, I get it now. "...that the system will allocate a char str[10] without checking to see if it can do it with a malloc first." sounded like he should check whether allocating on the stack would work by mallocing it first. Indeed for any large amount of memory, it is safer to allocate on the heap with the malloc family, and then of course, 'large' will depend on the target architecture. Sure thing I wouldn't like satellites hailing down on top of us :) I would usually set 'large' to 100bytes (personal preference), but if this is a recursuve function, even that much should probably be on the heap instead. It seems I'm a bit snappy at the moment, please accept my apologies if I'm being too harsh. Good evening (or whichever it is where you are) Jonathan On 7/17/07, Adam Jones <aj...@i-...> wrote: > > >>Jonathan Wrote: > >>However, Adam you are wrong about the allocation of char str[10]: this > data will be allocated on the stack, along with other local variables. You > can't check whether it failed or >>not: if there's anough room, ok, if > not, > the program will have already crashed (stack overflow) before you can do > anything (this is why we use malloc() for large amounts of >>memory, which > indeed needs checking) > > When you declare Char str[10], you do create it on the stack. But I was > suggesting that he create it on the heap with the Malloc() function. > Please > reread what I posted, it is listed below again: > > >>Adam Wrote: > >>You also assume which is not very good > >>that the system will allocate a char str[10] without checking to see if > it > >>can do it with a malloc first. On memory allocations you need to check > >>first, and if it fails exit your program gracefully. > > Maybe bad wording, but the fact remains that if you are going to allocate > memory, to be safe do it on the heap (using malloc()) instead of the > stack. > That is what I was suggesting. > > I hope this clears up any miscommunications on my part..... > > > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > -- <Morpheus> linux, c'est une question de VI ou de MORE |