-1

I'm trying to define Node into NodeList class, And store it.

Whay I've tried is:

In Try() function, I defined a node like Node *node = malloc... This works fine. But if I use the node that I defined in class like node = malloc... this line gives runtime error. I don't understand what is the difference between these two.

Here are classes:

Node.hpp

#ifndef NODE_HPP
#define NODE_HPP

class Node{
public:
    int data;
};

#endif

Node.cpp

#include <iostream>
#include "Node.hpp"

using namespace std;

NodeList.hpp

#ifndef NODELIST_HPP
#define NODELIST_HPP

#include "Node.hpp"

class NodeList{
public:
    Node *node;
    void Try();
};

#endif

NodeList.cpp

#include <iostream>
#include "NodeList.hpp"
#include "Node.hpp"

using namespace std;

void NodeList::Try(){
    //This works (doesn't give error):
    //Node *node = (Node*)malloc(sizeof(Node));

    //But I use the node I defined in class here and this line gives runtime error:
    node = (Node*)malloc(sizeof(Node));
}

Main.cpp

#include <iostream>
#include "NodeList.hpp"

using namespace std;

int main()
{
    NodeList *node = NULL;
    node->Try();
    system("PAUSE");
    return 0;
}
  • ***it gives error recipe for target failed*** It should give a more detailed error message than that. – drescherjm Nov 25 '20 at 18:46
  • 1
    `node = (Node*)malloc(sizeof(Node));` note that malloc does not call the constructors. – drescherjm Nov 25 '20 at 18:47
  • OP is invoking UB as far as i can see, by invoking Create from a null pointer – WARhead Nov 25 '20 at 18:51
  • @drescherjm It's ok. I don't have any code in constructor now. – oburhesapbanyedi Nov 25 '20 at 18:53
  • Related to the use of malloc in a `c++` program: [https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-and-or-new](https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-and-or-new) – drescherjm Nov 25 '20 at 18:55
  • @WARhead As I said above, in Try function I defined a Node and it was null. Used it to call Create and it worked. So I think it's not because it's null – oburhesapbanyedi Nov 25 '20 at 18:55
  • @oburhesapbanyedi So, as far as I can see, it seems compiler allows call to functions that does not access any member variables, like it allows calls to static functions? ( which seems to be a contested topic https://stackoverflow.com/questions/28482809/c-access-static-members-using-null-pointer ) – WARhead Nov 25 '20 at 19:01
  • https://stackoverflow.com/a/28483256/6459731 this answer goes on to explain that the specific case given by is allowed in some compilers, so implementation defined? – WARhead Nov 25 '20 at 19:04
  • @WARhead You say it's contested but I have to call it like this. Because I'm trying to store Node into NodeList. So I define the Node in NodeList. And it's null at first. – oburhesapbanyedi Nov 25 '20 at 19:14
  • You could make Create() a static member function `static Node *Create(Node*);` and avoid the issue completely by `Node* node = Node::Create();` – drescherjm Nov 25 '20 at 19:19
  • With this said I am not sure any of this is related to the error message, however I am still waiting for the exact text of the message as ***recipe for target failed*** provides no help in debugging or identifying the compile error. – drescherjm Nov 25 '20 at 19:24
  • @drescherjm yes if I define like `Node* node = NULL` and call Create, it works. But it doesn't store. I want to use Node I defined in class NodeList: `class NodeList{ Node *node}` I don't understand what is the difference between these two? – oburhesapbanyedi Nov 25 '20 at 19:27
  • Then `node = Node::Create();` in `NodeList` after you make it a static member function. – drescherjm Nov 25 '20 at 19:28
  • `class NodeList{ Node *node}` node has no value. – drescherjm Nov 25 '20 at 19:29
  • @drescherjm whole error is: `makefile:7: recipe for target 'run' failed mingw32-make: *** [run] Error -1073741819` – oburhesapbanyedi Nov 25 '20 at 19:29
  • `-1073741819` is `0xC0000005` which is access violation. Meaning the program crashed when it ran. It's not a build error. I assume you are using Visual Studio code and it is hiding this detail (that the problem was at runtime and not build time) from you. – drescherjm Nov 25 '20 at 19:29
  • actually it's NotePad++ – oburhesapbanyedi Nov 25 '20 at 19:32

1 Answers1

2

You code has many problems:

  1. In Main.cpp you are dereferencing a NULL pointer: node->DosyaOku();, but node is NULL. This is an undefined behavior.
  2. You have the same problem in NodeList.cpp
  3. You are using a malloc in Node.cpp and you should probably want to use a new instead (read here), and you should think how to free/delete that pointer.
  4. The Create in Node.cpp has a parameter that is overwritten immediately, that looks like an error.
ocirocir
  • 3,543
  • 2
  • 24
  • 34
  • Thanks for the answer. In main.cpp, it works well. But in `Try()` function I use the node I defined in class and it doesn't work. I don't understand the difference between these two. – oburhesapbanyedi Nov 25 '20 at 22:20
  • **You have the same problem in NodeList.cpp** this gives error. But in `main.cpp` it doesnt. – oburhesapbanyedi Nov 25 '20 at 22:23
  • 1
    It's an undefined behavior in both cases. If "it seems to work" in main.cpp it doesn't mean that it is correct and would not crash the next line you add. – ocirocir Nov 25 '20 at 23:30
  • Unfortunately sometimes with Undefined Behavior broken code can appear to work. – drescherjm Nov 26 '20 at 00:23
  • When I looked at this code earlier I did not think about the passed node* to Create. You are correct about point #4 that it does not make sense if you pass by value then don't use and immediately set it to a different value. – drescherjm Nov 26 '20 at 00:25
  • @drescherjm I edited the question and not using nullptr to call function now. But still I cant use node as I want. – oburhesapbanyedi Nov 26 '20 at 12:11
  • It's ok. I changed null definition to malloc in main.cpp and it works now. Thank you all – oburhesapbanyedi Nov 26 '20 at 12:16