
pointer to int array question
Tong Zhai a crit dans le message ...
Quote:
> arr = (int *) malloc (5*sizeof (int));
No cast need, malloc() returns a void *.
Quote:
> assert (arr != NULL);
assert() supplies a wonderful tool for debugging, because it forces you to
correct the code that is not running. But assert() should be disabled at
validation and release time (by activating the NDEBUG macro). The abort()
function (that is called by the assert() macro) shoul be used in extrem
emergency only. Good protective test code has to be written to prevent the
use of say a NULL pointer by example. The protection will prevent illegal
code to be executed, and will gently warn the user that he is doing
something wrong, or that no more ressource like memory is available.
Don't forget that there is no guarantee that an abort() will return the
allocated memory to the system or close the open files. It can have
catastrophic results. (I can tell you that my MS-DOS window open under Win
98 has been suddenly closed several times a day during the debug of by
current project)
I have a lot a assert() that are active in the code that I write and test on
my PC, but when the code is downloaded to some embedded system (sort of
networking electronic device to be specific), it is first recompiled with
the NDEBUG macro defined, and the assert() macros are even not compiled at
all. (I have no stderr on my electronic card !)
--
HS
C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html
About gets() and fflush(stdin) :
- "Even Buffy cannot slay the resulting RhinoDaemons."
Martin Ambuhl clc.