K
kimimaro
Is there anymore methods in exiting your program using pure C language
other than return 0?
other than return 0?
kimimaro said:Is there anymore methods in exiting your program using pure C language
other than return 0?
kimimaro said:Is there anymore methods in exiting your program using pure C language
other than return 0?
Nicolas Pavlidis said:There is a way:
exit(0);
This function, I think it is deklared in stdlib.h finishes your program,
but be aware of memoryleaks.
kimimaro said:Is there anymore methods in exiting your program using pure C language
other than return 0?
kimimaro said:Is there anymore methods in exiting your program using pure C language
other than return 0?
kimimaro said:Is there anymore methods in exiting your program using pure C language
other than return 0?
bd said:You can use the exit() or abort() functions.
Edmund said:And don't forget: assert(0);
Most* OS's free the memory when a program has exited so it's not really a
concern over memory leaks, but rather on programming style.
*All that I know of.
Michael Mair said:Note: The assert()-macro, if "called" with an expression evaluating
to zero, prints an error message and calls abort().
ranjeet said:NOTE : what I think is that when we call assert
And if it get the agrumnet as NULL then
Its display the message as we see in the case of the Poiters
when it gets the Zero as an argument. then it Returns the 0
(Exit the program)
Is my knowledge about assert is correct ????? help in correcting
If i am wrong.
NOTE : what I think is that when we call assert
And if it get the agrumnet as NULL then
Its display the message as we see in the case of the Poiters
when it gets the Zero as an argument. then it Returns the 0
(Exit the program)
assert is a macro, not a function. It's not called, properly speaking.
It's expanded with argument substitution.
[Correct]
And if it get the agrumnet as NULL then
assert evaluates its argument as an integer expression. NULL is a
macro that evaluates to a null pointer constant. ...
NULL is not the same thing as zero. NULL will evaluate to false, but
the argument to the assert macro need not be a pointer value.
In said:assert is a macro, not a function. It's not called, properly speaking.
It's expanded with argument substitution.
[Correct]
And if it get the agrumnet as NULL then
assert evaluates its argument as an integer expression. NULL is a
macro that evaluates to a null pointer constant. ...
The implication here -- which is also correct -- is that assert(NULL)
does not have to compile.
This is sort of a bug in C89, and is fixed in C99, where the argument
to assert() has type "bool" (from <stdbool.h>; really _Bool).
In C99, assert(NULL) is a valid invocation that -- if NDEBUG is not
defined -- emits a message and calls abort().
Nope, in C99 the argument to assert has scalar type.
You're contradicting yourself: if assert expected a bool argument, passing
it (void *)0 would be far from correct: undefined behaviour.
Chris Torek said:[Specific to C99]
Nope, in C99 the argument to assert has scalar type.
Since I still have not actually bought the final PDF, I will believe
you; but:
You're contradicting yourself: if assert expected a bool argument, passing
it (void *)0 would be far from correct: undefined behaviour.
I remember discussion (from before the C99 standard was final, I think)
about this. The claim was that:
a) assigning a bool variable from a pointer was allowed;
b) the result of such an assignment was "true" if the pointer
compares unequal to NULL and false if it compares equal -- e.g.:
char *p;
...
bool x = p; /* danger, maybe not actually legal C99 */
would always "mean the same thing" as:
bool x = (p != 0);
and therefore
c) by making assert "look like" a function taking a single "bool",
the rules for ordinary function parameter assignment would make:
p = malloc(N);
assert(p);
valid code (however ill-advised one might be to use assert() here).
If either (or both) of (a) or (b) is false in C99, the reasoning
would fall apart. (The pre-C99 draft I use as a quick searchable
text does not support either of the two claims, but I know that
this draft does not match the final standard, and that some of the
changes include details about "bool"s.)
In said:Chris Torek said:[Specific to C99]
... sort of a bug in C89, and is fixed in C99, where the argument
to assert() has type "bool" (from <stdbool.h>; really _Bool).
Nope, in C99 the argument to assert has scalar type.
Since I still have not actually bought the final PDF, I will believe
you; but:
In C99, assert(NULL) is a valid invocation that -- if NDEBUG is not
defined -- emits a message and calls abort().You're contradicting yourself: if assert expected a bool argument, passing
it (void *)0 would be far from correct: undefined behaviour.
I remember discussion (from before the C99 standard was final, I think)
about this. The claim was that:
a) assigning a bool variable from a pointer was allowed;
b) the result of such an assignment was "true" if the pointer
compares unequal to NULL and false if it compares equal -- e.g.:
char *p;
...
bool x = p; /* danger, maybe not actually legal C99 */
would always "mean the same thing" as:
bool x = (p != 0);
and therefore
c) by making assert "look like" a function taking a single "bool",
the rules for ordinary function parameter assignment would make:
p = malloc(N);
assert(p);
valid code (however ill-advised one might be to use assert() here).
If either (or both) of (a) or (b) is false in C99, the reasoning
would fall apart. (The pre-C99 draft I use as a quick searchable
text does not support either of the two claims, but I know that
this draft does not match the final standard, and that some of the
changes include details about "bool"s.)
C99 guarantees all of (a)-(c):
"6.3.1.2 Boolean type
When any scalar value is converted to _Bool, the result is 0 if the
value compares equal to 0; otherwise, the result is 1."
(Which is germane to the bool concept and you would expect or hope
it to hold also in C99.)
Still, assert takes a scalar argument also in C99 even though AFAICS it
could just as well have been redefined from C89 to take a bool.
In <[email protected]> (e-mail address removed) (Daniel Vallstrom) writes:
It doesn't make any difference, because assert is a *macro*, not a
function, so the fact that pointers are assignment compatible with bools
is irrelevant to an assert defined as taking a bool parameter.
It *was* redefined from C89, where it takes an int expression as its
argument. If you define it as taking a bool, then you *must* pass it a
bool argument and not anything that is assignment compatible with a bool.
In said:[email protected] (Dan Pop) wrote in message news: said:In <[email protected]> (e-mail address removed) (Daniel Vallstrom) writes:
[snip discussion about assert() having a bool argument]
It doesn't make any difference, because assert is a *macro*, not a
function, so the fact that pointers are assignment compatible with bools
is irrelevant to an assert defined as taking a bool parameter.
Fair enough. However, *if* the type of the argument to assert() was
to be changed to bool/_Bool, one could also imagine the addition to
the standard of a new overreaching clause saying that the argument
to macros is to be casted, where applicable, to the macro argument
type when used.
AFAICS that would work?
(I'm not suggesting it ought
to be done.) Anyway, it is not bool's fault that it isn't the type
of assert().
Ah! I obviously wasn't aware that C89 assert() only took int.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.