-1

I'm attempting to try and use a string input from a method and set that to a variable of a structure, which i then place in a linked list. I didn't include, all of code but I did post constructor and all that good stuff. Now the code is breaking at the lines

node->title = newTitle;
node->isbn = newISBN;

So newTitle is the string input from the method that I'm trying to set to the title variable of the Book structure of the variable node. Now, I'm assuming this has to do with a issue with pointers and trying to set data to them, but I can't figure out a fix/alternative. Also, I tried using

strcpy(node->title, newTitle)

But that had an issue with converting the string into a list of chars because strcpy only uses a list of characters. Also tried a few other things, but none seemed to pan out, help with an explanation would be appreciated.

struct Book
{
   string title;
   string isbn;
   struct Book * next;
};

//class LinkedList will contains a linked list of books
class LinkedList
{
 private:
     Book * head;

 public:
     LinkedList();
     ~LinkedList();
     bool addElement(string title, string isbn);
     bool removeElement(string isbn);
     void printList();
 };

 //Constructor
 //It sets head to be NULL to create an empty linked list
 LinkedList::LinkedList()
 {
     head = NULL;
 }

 //Description: Adds an element to the link in alphabetical order, unless book with 
    same title then discards
 // Returns true if added, false otherwise
 bool LinkedList::addElement(string newTitle, string newISBN)
 {

struct Book *temp;
struct Book *lastEntry = NULL;
temp = head;
if (temp==NULL) //If the list is empty, sets data to first entry
{
    struct Book *node;
    node = (Book*) malloc(sizeof(Book));
    node->title = newTitle;
    node->isbn = newISBN;
    head = node;
}
while (temp!=NULL)
{
    ... //Rest of Code
  • Where are you learning from? It looks like this is "C with strings", not C++. For instance, you don't need to preface a pointer declaration to a struct with the struct keyword, it's just `Book* temp;` (preferably with an initialization, as well). There's also pretty much never a reason to use `malloc` over `new`. – Coda17 Aug 29 '14 at 19:39
  • I didn't have all that great of a teacher for C and C++ (he literally told us the code he put up on the board wouldn't run on a compiler correctly) and I don't know many of "correct" or newer declarations for c++ – Robert Kebert Aug 29 '14 at 19:56

2 Answers2

0

Note that your Book struct is already a linked list implementation, so you don't need the LinkedList class at all, or alternatively you don't need the 'next' element of the struct.

But there's no reason from the last (long) code snippet you pasted to have an error at the lines you indicated. node->title = newTitle should copy the string in newTitle to the title field of the struct. The string object is fixed size so it's not possible to overwrite any buffer and cause a seg fault.

However, there may be memory corruption from something you do further up the code, which doesn't cause an error until later on. The thing to look for is any arrays, including char[], that you might be overfilling. Another idea is you mention you save method parameters. If you copy, it's ok, but if you do something like

char* f() {
    char str[20];
    strcpy(str, "hello");
    return str;
}

...then you've got a problem. (Because str is allocated on the stack and you return only the pointer to a location that won't be valid after the function returns.) Method parameters are local variables.

snoopy
  • 399
  • 1
  • 4
  • 11
0

The answer you seek can be found here.

In short: the memory malloc returns does not contain a properly constructed object, so you can't use it as such. Try using new / delete instead.

Community
  • 1
  • 1
Jonas R.
  • 56
  • 1
  • 3