0

I have this part of my program:

ProcessItem *new_process = (ProcessItem *)malloc(sizeof(ProcessItem));
printf("%p, %d\n", new_process, errno);
if(NULL == new_process) {
  printf("%p, %d\n", new_process, errno);
  panic("SS: ProcessItem malloc failed.\n");
}

Where ProcessItem is:

typedef struct ProcessItem{
struct ProcessItem *next_item;
endpoint_t ep;
SensitivityItem *process_sensitivities; /*iterate sensitivities for proc*/
} ProcessItem;

Now this is the odd part: The output for the program looks like this: program output

Does anyone know why the new_process pointer is NULL all of a sudden?

Mlezi
  • 105
  • 1
  • 13
  • 3
    Did you do a copy-paste of the code you show, or did you rewrite it for the question? If you rewrote it, look at the actual code and make sure you don't use assignment `=` instead. – Some programmer dude Jan 13 '19 at 11:06
  • 3
    Otherwise if you want more help, then you have to create a [mcve] to show us. Also please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Jan 13 '19 at 11:07
  • 1
    I did a copy paste. I am definitely using ==. Notice the NULL is in front so the compiler would complain about lvalue assignments if it were only one '='. – Mlezi Jan 13 '19 at 11:09
  • Nevertheless, MCVE please. – Yunnosch Jan 13 '19 at 11:10
  • 1
    Then we really need a [mcve]. Also please [don't cast the result of `malloc` in C](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). – Some programmer dude Jan 13 '19 at 11:15
  • I can't make it MCVE my self. This happens in latest minix OS. When I try it in GDB on a linux dist it is not reproducible – Mlezi Jan 13 '19 at 11:55
  • Also, other mallocs in the same program in exactly the same style succeed. – Mlezi Jan 13 '19 at 11:57
  • A null pointer is not necessarily the address value `0`. Maybe minix is one of these systems. – Jens Gustedt Jan 13 '19 at 14:06
  • The bug is definitely somewhere else, likely in the context in which the code is executed (signal handler? ISR? too deep in stack?). Try an experiment, and remove the first `printf` call. Most likely the test for `NULL` will pass. – user58697 Jan 13 '19 at 19:57
  • This code runs in a system call handler for a custom Minix server. Removing the first printf doesn't help, the problems occured before the printf was there (it's why I put the printf there in the first place). I moved the malloc call a stack level down. The NULL check succeeded, but then a segfault is given later on when trying to use the malloced memory... – Mlezi Jan 13 '19 at 21:04
  • OT: strongly suggest changing: `if(NULL == new_process) {` to `if( !new_process ) {` – user3629249 Jan 13 '19 at 22:04
  • in general, error messages should be output to `stderr`, not `stdout`. Suggest either `fprintf( stderr, "your error message\n" )` or if wanting to know about `errno` then use: `perror( "your error message" )` – user3629249 Jan 13 '19 at 22:06
  • OT: regarding: `printf("%p, %d\n", new_process, errno);` the '%p' format specifier expects a `void*` and the compiler should have complained, Suggest: `printf("%p, %d\n", (void*)new_process, errno);` also, `the value in `errno` is meaningless by itself. Suggest: `printf("%p, %s\n", (void*)new_process, strerror( errno ));` – user3629249 Jan 13 '19 at 22:13

0 Answers0