I am trying to create a linked list that will store words and how many times they occurred in the .txt file. Before reading I'm trying to create a linked list to see if it is okay. While testing, it is crashing.
#include <iostream>
#include <string>
struct n {
std::string word;
int occurance;
n* next;
};
typedef n node;
int main() {
node* root;
root = (node*)malloc(sizeof(node*));
root->word = "test";
root->occurance = 5;
std::cout << root->word
<< root->occurance << std::endl;
}
typedef n node;-- Totally unnecessary, and error-prone. Why did you need to do this? Second, what C++ book introduces usingmallocto create a dynamically allocated object? That is wrong in this contextmallocin C++ when you havenew?root = (node*)malloc(sizeof(node*));Your sizeof is a pointer. You wantsizeof(node)root = (node*)malloc(sizeof(node*));-- This does not constructnodeobjects, but your code assumes it did.mallocat all, because it doesn't initialize anything.