Why did my program crash when I tried the perror function?

Cause int* p contains a random/garbage value.

Up vote 0 down vote favorite share g+ share fb share tw.

I'm testing the perror function in C, and according to this page it prints a default message when a null pointer is passed: int main(void) { int *p; perror(p); //crashes } c error-message link|improve this question edited Feb 16 at 17:48Muggen2,5601517 asked Feb 16 at 17:46Adel8781418 100% accept rate.

Cause int* p contains a random/garbage value. It is not an NULLpointer. You need to explicitly initialize it with p = NULL;.

Using an uninitialised variable is Undefined behaviour. Main() also needs to return 0;.

Because p isn't initialized to 0 automatically in C (this isn't Java). Int *p = 0.

Passing an invalid pointer to perror is undefined behavior. (C99, 7.1.4p1) "Each of the following statements applies unless explicitly stated otherwise in the detailed descriptions that follow: If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined.

Thanks! – Adel Feb 16 at 17:54 How about: 7.19.10.4-2: if s is not a null pointer and the character pointed to by s is not the null character. – cnicutar Feb 16 at 17:57 @cnicutar I would quote 4.

P2 that says "Undefined behavior is otherwise indicated in this International Standard by the words "undefined behavior" or by the omission of any explicit definition of behavior. ". – ouah Feb 16 at 18:00 @Adel because I read the standard more than one time:) – ouah Feb 16 at 18:01 1 Well you could be right.

However the standard would usually make it clear "if so on so, the result is not defined". Why did they mention s not being NULL without any mention of UB? – cnicutar Feb 16 at 18:02.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions