0

Critical error detected c0000374

#pragma once

typedef struct node
{
int value;
node* next;
node* before;
}   node;

void print_nodes(node* list) {
node *current = (node*)malloc(sizeof(node));
//current->value = 0;
current->next = list;

while (current->next != nullptr) {
    printf("%i\n", current->next->value); <-THROW an Exception in the Fist loop
    current->next = current->next->next;
}
free(current);
}

void add_node(node* list) {

}

inline void new_nodes(node* list, size_t anzahl) {
list[0].before = NULL;
for (int i = 0; i <= anzahl; i++) {
    list[i].value = i + 1;
    list[i].next = &list[i + 1];
    list[i + 1].next = &list[i - 1];
}
list[anzahl].next = NULL;
}

The printf statement Throws an Exception ... but only sometimes. My cpp calls the function new_nodes with the size_t = 10 so it cant be tooooo big.

Additional Info one time even the heap "broke".

Thanks for your Help.

  • Additional Info one time even the heap "broke" – Tim Weissenfels Dec 15 '18 at 15:24
  • This `current->next = current->next->next;` is too dangerous as what if `current->next->next` is `NULL` and next time in `while()` loop condition you are going to check `NULL->next`, it cause crash. Also I don't understand why you are doing malloc in `print_nodes()` function ? `add_nodes()` already creates the list, `print_nodes()` has to just print it. – Achal Dec 15 '18 at 15:33
  • The print_nodes() malloc is only for current it only allocate memory for one instance – Tim Weissenfels Dec 15 '18 at 15:36
  • "is too dangerous as what if current->next->next is NULL and next time in while() loop condition you are going to check NULL->next" What would be your suggestion – Tim Weissenfels Dec 15 '18 at 15:37
  • Add the extra info to the question, please, rather than in comments. The `malloc()` in the print code isn’t needed. You could write the code with a local pointer variable. – Jonathan Leffler Dec 15 '18 at 15:41
  • u mean like this? `void print_nodes(node* list) { node current; //current->value = 0; current.next = list; while (current.next != nullptr) { printf("%i\n", current.next->next->value); current.next = current.next->next; } }` – Tim Weissenfels Dec 15 '18 at 15:42
  • The simple check can be `list != nullptr` and anyhow you are just printing list in the `print_nodes()` function not modifying it, therefore use the same `list` argument to print it. For e.g `void print_nodes(node* list) { while (list != nullptr) { printf("%i\n", list->value); list = list->next; } }` – Achal Dec 15 '18 at 15:43
  • Tried it still throws an exception :( – Tim Weissenfels Dec 15 '18 at 15:45
  • 2
    Exception, casting the result of `malloc`, `nullptr`, "cpp" ... are you sure about the C tag? Maybe change it to C++ tag? – pmg Dec 15 '18 at 15:49
  • Yeah cheated a little with the nullptr keyword but vs is not able to let me use c keywords only so i thought why not – Tim Weissenfels Dec 15 '18 at 15:51
  • but i casted the result of malloc with the standart c-style cast so no problemo there – Tim Weissenfels Dec 15 '18 at 15:51
  • _no problemo there_ -- yes problemo. You didn't `#include `, did you? Meanwhile, show the code which calls `new_nodes`. – user58697 Dec 15 '18 at 18:51
  • Yes i did include stdlib.h – Tim Weissenfels Dec 15 '18 at 18:52
  • do NOT place function bodies in a header file, Move the function to a separate file and have the header file ONLY contain the prototype for the function. – user3629249 Dec 15 '18 at 23:05
  • regarding: `void add_node(node* list) { }` and `void add_node(node* list,node*) { }` overloading function names is NOT allowed in C. So this must be C++. Please correct the 'tag' from `c` to `c++` – user3629249 Dec 15 '18 at 23:09
  • The posted code is a mix of C and C++ code. Use only a single language, don't mix the languages – user3629249 Dec 15 '18 at 23:11
  • regarding: `new_nodes()` This is the function that needs to allocate a new node, fill in the fields of that new node, then link it into the `list` Exactly how to link the new node into the `list` will depend on some info not provided in the question, such as 1) is it a circular list 2) link at the beginning of `list` 3) link at the end of `list` 4) link at the ascending (or descending) ordered point in the `list` Therefore we cannot say exactly what is wrong with that function. – user3629249 Dec 15 '18 at 23:30

1 Answers1

2

this:

void print_nodes(node* list) 
{
    node *current = (node*)malloc(sizeof(node));
    //current->value = 0;
    current->next = list;

    while (current->next != nullptr) 
    {
        printf("%i\n", current->next->value); <-THROW an Exception in the Fist loop
        current->next = current->next->next;
    }
    free(current);
}

Needs to be heavily modified: Suggest:

modified (after clarification by the OP) to use a 'headless' linked list

void print_nodes(node* list) 
{ 
    node * current = list; 

    while (current) 
    { 
        printf("%i\n", current->value); 
        current = current->next; 
    } 
}
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • Good Suggestion modified it to `void print_nodes(node* list) { while (list->next != NULL) { printf("%i\n", list->value); list = list->next; } }` – Tim Weissenfels Dec 15 '18 at 23:25
  • I`m not quite sure if this maybe cause some trouble – Tim Weissenfels Dec 15 '18 at 23:26
  • This would be your "code" "implemented" and "working" `void print_nodes(node* list) { node * current = list; while (current->next != NULL) { printf("%i\n", current->value); current = current->next; } }` – Tim Weissenfels Dec 15 '18 at 23:31
  • @TimWeissenfels, Your suggested code assumes that there is no 'head' pointer for the list. I assumed that there would be a 'head' pointer that contains nothing but a pointer to the first entry in the list – user3629249 Dec 15 '18 at 23:33
  • Yeah you are right thats why i used your code and changed it a bit to fit my needs – Tim Weissenfels Dec 15 '18 at 23:36
  • @TimWeissenfels, If you like my answer, then please accept it. – user3629249 Dec 15 '18 at 23:39
  • Accepted it but change the code to my "correction" of it because your code would print out 1 element "further than wished" – Tim Weissenfels Dec 15 '18 at 23:42
  • 1
    @TimWeissenfels, Actually my version of the code would stop at the right time. However, since you have not implemented a 'head' pointer, it would skip the first entry. I'll edit the code to use your suggested version. – user3629249 Dec 15 '18 at 23:44
  • Yeah but my code is working in my particular version thats why. Bcs i mean ur code is the proper way to do it but i wouldent work in my case – Tim Weissenfels Dec 15 '18 at 23:47
  • Maybe u accept my edit and edit it into :working code with this code x: and :working code with this code y: or something like that – Tim Weissenfels Dec 15 '18 at 23:48