4 Replies - 255 Views - Last Post: 29 September 2012 - 02:19 PM Rate Topic: -----

#1 Sheppard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 25-December 11

Why is my program crashing?

Posted 29 September 2012 - 01:39 PM

Hi there. I am trying to learn how to use linked lists and have wrote up some code but it crashes and I can't seem to figure out why :/

So after my program enters the for loop it asks me for the head nodes data, then returns back into the loop and crashes?
Here is my code

main.cpp:
#include "main.h"

void print_list(Node*);
void get_data(Node*&);

int main()
{
    Node *head = NULL;
    Node *temp, *last;

    unsigned int numberOfFriends;

    std::cout << "Number of friends: ";
    std::cin >> numberOfFriends;

    for(unsigned int i = 0; i < numberOfFriends; i++)
    {
        if(head == NULL)
        {
            head = new Node;

            get_data(head);

            last = head;
        }
        else
        {
            temp = new Node;

            get_data(temp);

            last->next_node = temp;
            last = temp;
        }
    }

    print_list(head);

    return 0;
}

void print_list(Node* head)
{
    Node * ptr = head;

    while(ptr != NULL) //While the ptr iterator hasn't reached the end of the list, proceed
    {
        //Output node contents to console
        std::cout << ptr->SFriend.name << std::endl;
        std::cout << ptr->SFriend.email << std::endl;

        ptr = ptr->next_node; //next pointer in the linked list
    }

    //Exit function when ptr has reached NULL
}

void get_data(Node*& node)
{
    std::cout << "Name: ";
    std::cin >> node->SFriend.name;

    std::cout << "Email: ";
    std::cin >> node->SFriend.email;
}



main.h:

#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED

#include <iostream>
#include <windows.h>
#include <vector>

#include "main.h"

struct friends
{
    char *name;
    char *email;
};

struct Node
{
    friends SFriend;
    Node * next_node;
};

#endif // MAIN_H_INCLUDED



Is This A Good Question/Topic? 0
  • +

Replies To: Why is my program crashing?

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: Why is my program crashing?

Posted 29 September 2012 - 01:44 PM

It's customary to allocate space for the strings where you are going to input data into. You don't seem to be doing that for the name or email.
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Why is my program crashing?

Posted 29 September 2012 - 02:01 PM

Given that this is C++, consider std::string instead of char*.

It's not just the added work of using C strings. Its also about the robustness of your own code. For example, you need to comply with the rule of three, which you probably will violate if your code looks like this already.

And one more point. You do understand that when you dynamically allocate, you are responsible for deallocating too, right?

This post has been edited by Oler1s: 29 September 2012 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#4 Sheppard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 25-December 11

Re: Why is my program crashing?

Posted 29 September 2012 - 02:10 PM

View PostSkydiver, on 29 September 2012 - 01:44 PM, said:

It's customary to allocate space for the strings where you are going to input data into. You don't seem to be doing that for the name or email.


Ah, simple mistake. Thank you!

View PostOler1s, on 29 September 2012 - 02:01 PM, said:

Given that this is C++, consider std::string instead of char*.

It's not just the added work of using C strings. Its also about the robustness of your own code. For example, you need to comply with the rule of three, which you probably will violate if your code looks like this already.

And one more point. You do understand that when you dynamically allocate, you are responsible for deallocating too, right?


Yeah, that's what I did. I'm using the data type string instead of char* now.
And yes, I do understand, I just tend to forget sometimes <_< lol
Thanks for the replies
Was This Post Helpful? 1
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Why is my program crashing?

Posted 29 September 2012 - 02:19 PM

struct friends
{
    char *name;
    char *email;
};



name and email are only pointers, they can only point to somewhere in memory. When you create the pointer it points to some garbage value in memory, most likely an address that your program does not own. when you try writing to this memory location, you will most likely (if your lucky) get a segmentation fault. If you are going to use pointers for your strings, then you need to allocate memory for "your program" on the run time heap using "new", or alternatively (and a better way) would to pre-allocate enough memory as char array for the names.

example
struct friends
{
    char name[50];
    char email[50];
};



However, since you are using C++ use can use a std::string. This will take care of all the memory allocation, and reallocation if your string needs to resize in length.

Edit: looks like you already found your solution, good job :)

This post has been edited by jjl: 29 September 2012 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1