0

I've been trying to make a linked list with this code. It works fine when I run it using debug mode (and exits with exit code 0). But when I try to run it or when I Compile the code manually it exits with "Process finished with exit code -1073741819 (0xC0000005)".

num_t *temp = nums;

while (temp -> next){
    temp = temp -> next;
}

temp -> next = (num_t *) malloc(sizeof(num_t *));
temp = temp -> next;
temp -> location[0] = i;
temp -> location[1] = j;
temp -> next = NULL;

num_t is typedef for struct num. and struct num is an structure which has an array of 2 integers (array "location") and a pointer to struct num. nums is the first node of linked list and is initialized before (it's next node is NULL).

I read topics about "Process finished with exit code -1073741819 (0xC0000005)". none of them were useful.

yano
  • 4,827
  • 2
  • 23
  • 35
PicoNa
  • 3
  • 2
  • @PicoNa You need to check initially whether the pointer nums is a null pointer. – Vlad from Moscow Jan 25 '22 at 20:04
  • 3
    `malloc(sizeof(num_t *));` That should probably be `sizeof(num_t)` You only allocate enough memory to hold a pointer, not a pointer and 2 integers. – Gerhardh Jan 25 '22 at 20:06
  • @Yuumi yeah actually it doesn't seem to have any problems at all, and when I use debug mode nothing seems to be weird and even "location" values are correct. but when I'm running the code the problem exists until I delete the while loop at the start. – PicoNa Jan 25 '22 at 20:14
  • @Gerhardh that could be right, but temp -> next is only a pointer. I don't think it needs more than size of a pointer. btw I tried it. no success. – PicoNa Jan 25 '22 at 20:18
  • @Gerhardh wow that worked. The last time I just changed sizeof(num_t) for temp -> next. Now I did it for both temp-> next and "nums" and it just worked! Thanks man! one more thing, I was wondering I worked with linked lists before, and I was always using sizeof (pointer). Never had such a problem. How could that happen? And why it was working on debug mode? – PicoNa Jan 25 '22 at 20:26
  • 1
    "I don't think it needs more than size of a pointer." Incorrect. You need to allocate enough memory for whatever the pointer will point to. In this case, `->next` is a `num_t*` that will point to a `num_t`, so allocate size for a `num_t` (presumably, a [mre] would remove all doubt). Here's a [previous answer](https://stackoverflow.com/a/70073561/3476780) of mine that goes into more detail about best practices for allocated the correct amount of space. – yano Jan 25 '22 at 20:46
  • "And why it was working on debug mode?" Overwriting the amount of memory you request invokes [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior). Anything could happen, including working, or appearing to work only to blow up in your face some time later. As you've just learned, UB can behave can be different depending on compiler flags. – yano Jan 25 '22 at 20:49
  • You already have memory for the pointer. That is withing `*temp`. What is missing is the memory where your pointer shall point to. And that is a whole node, not a pointer. – Gerhardh Jan 26 '22 at 07:40

1 Answers1

0

Well, according to @Gerhardh 's comment the problem happened because I allocated insufficient memory for nodes. I was allocating memory only for a pointer, not a pointer and two integers. so changing malloc(sizeof(num_t *)) to malloc(sizeof(num_t)) solved the problem. and at the end I must thank @yano for their detailed explenation.

PicoNa
  • 3
  • 2