RecursiveGamer
RecursiveGamer

Reputation: 3

Trying to make a Linked List that contains strings C++

I am currently working on a linked list that will have strings that contain strings of information. I am using a struct that looks like this:

struct symbolTable
{
    string lexeme;
    string kind;
    string type;
    int offSet;
    symbolTable *nextSymbol;
    symbolTable *nextTable;
};

The insert function looks a bit like this:

void MPParser::insertToSymbolTable(string identifier, string type, string kind)
{
    tempOffset++;
    symbolTable *tempNode;
    tempNode = (symbolTable*)malloc(sizeof(symbolTable));
    tempNode->kind = kind; //Run Time error Here..
    tempNode->type = type;
    tempNode->lexeme = identifier;
    tempNode->offSet = tempOffset;
    tempNode->nextTable = NULL;
    tempNode->nextSymbol = root;
    root = tempNode;
}

The program compiles and then when I try to run and insert into the linked list I get this error:

Unhandled exception at 0x5A6810D0 (msvcr110d.dll) in mpcompiler.exe: 0xC0000005: Access   violation writing location 0xCDCDCDCD.

What is the correct way to assign a string to another one in a pointer? Or am I doing something completely wrong? Any help would be appreciated!

Thanks!

Upvotes: 0

Views: 5757

Answers (3)

taocp
taocp

Reputation: 23654

I did a very simple test under gcc 4.5.3:

#include <iostream>
#include <string>

struct A
{
  std::string a;
};

int main()
{
   A* ptr = new A;
   ptr->a = "hello";
   std::cout << ptr->a << std::endl;

   //A aStruct;
   A* ptr2 = (A*)malloc(sizeof(A));
   //ptr2 = &aStruct;
   ptr2->a = "hello again";   //simulate what you have done in your code
   std::cout << ptr2->a << std::endl;
   std::cin.get();
};

This will result in core dump since ptr2 trying to access raw memory. However, if I uncomment:

//A aStruct;
//ptr2 = &aStruct;

It then works as expected. Therefore, you should use new instead of malloc. The reason is that new will call the constructor of the class to initialize the memory block allocated, however, malloc will not do that.

Upvotes: 1

bash.d
bash.d

Reputation: 13207

Try to replace your code with

void MPParser::insertToSymbolTable(string identifier, string type, string kind)
{
    tempOffset++;
    symbolTable *tempNode;
    tempNode = new symbolTable;
    tempNode->kind = kind; //Run Time error Here..
    tempNode->type = type;
    tempNode->lexeme = identifier;
    tempNode->offSet = tempOffset;
    tempNode->nextTable = NULL;
    tempNode->nextSymbol = root;
    root = tempNode;
}

The Access Violation means that you are writing to non-assigned memory. And you must never use malloc in C++ as it does not call constructors, always use new to create dynamic objects and delete to free them.

Upvotes: 2

Remy Lebeau
Remy Lebeau

Reputation: 597166

Use new instead of malloc() so the string objects are constructed properly:

tempNode = new symbolTable;

And then use delete when you need to free the node later:

delete node;

Upvotes: 2

Related Questions