Thread: Can't figure out this subtle error

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    71

    Can't figure out this subtle error

    Code:
    struct node{
        string location;
        int gval;
        int fval;
        node *next;
    };
    Code:
            struct node *hp;
            for(int i = 0; i<graph.size();i++)
    	{
    		cout << graph[i].location <<"=="<<graph[0].location<<"?\n";
    		if((graph[i].location).compare(start)==0)
    		{ 
    			index = i;
    			break;
    		}
    	}
        cout<<"Found "<< graph[index].location;
        //make starting nodes
        hp = (struct node*)(malloc(sizeof(struct node)));
        hp->location = graph[index].location;
    For some reason the cout produces output fine with the string value of graph[index].location but when I debug my program, it crashes right at
    Code:
     hp->location = graph[index].location;
    Also graph is defined as
    Code:
    vector<vertice> graph = initializeGraph();
    Code:
    struct Edge
    {
        string destination;
        int weight;
    };
    struct vertice
    {
        string location;
        vector<Edge> directions;
    };
    Graph is a vector of vertice. Also when I am in the debugger, and I try to track the value of graph[i].location in the for loop, I get
    Code:
    graph[i].location    CXX0058: Error: overloaded operator not found
    Last edited by workisnotfun; 03-03-2013 at 10:35 PM.

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    Dam I think it might be something with my syntax because I changed
    Code:
    hp = (structnode*)(malloc(sizeof(structnode)));
    hp->location = graph[index].location;
    to
    Code:
    node head;    
    head.location = graph[index].location;
        hp=&head;
    and it gets through this part perfectly fine.

    Can someone explain what's going on? I actually came from a C point of view and trying to use C++, and it seems I don't even have to malloc, is that right? What are some of the things I am misunderstanding because it seems like the '->' use is different in C++ than in C? :/
    Last edited by workisnotfun; 03-03-2013 at 10:52 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    A couple of comments.
    Code:
            struct node *hp;
            for(int i = 0; i<graph.size();i++)
    	{
    		cout << graph[i].location <<"=="<<graph[0].location<<"?\n";
    		if((graph[i].location).compare(start)==0)
    		{ 
    			index = i;
    			break;
    		}
    	}
        cout<<"Found "<< graph[index].location;
        //make starting nodes
        hp = (struct node*)(malloc(sizeof(struct node)));
        hp->location = graph[index].location;
    1. What guarantees that something will always be found. Or put another way, what garbage is in index if nothing is found.

    2. Don't use malloc in a C++ program, use new.
    hp = new node;

    It might be an idea to add a constructor as well, which will at least make sure you have some plausible data.
    Code:
    struct node{
        string location;
        int gval;
        int fval;
        node *next;
        node ( ) : next(0) {}
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    As mentioned by Salem you should be using new and not malloc here. The later does not construct the std::string data member location, it simply allocates sizeof(std::string) space for that member but does not actually build it. Using new will properly construct that piece of the struct. This is most certainly the reason for your crash.

    If your struct did not contain any objects needing to be constructed then using malloc would be allowable here but you'd still want to prefer new instead.
    Last edited by hk_mp5kpdw; 03-04-2013 at 08:59 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you writing some sort of graph? In that case, you should probably look at smart pointers. Otherwise I have to question why you use new (or malloc) in the first place.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot figure out error
    By fenway81 in forum C Programming
    Replies: 16
    Last Post: 11-12-2012, 07:51 PM
  2. Odd bus error, can't figure it out.
    By Subsonics in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 11:44 AM
  3. subtle segmentation fault
    By CodeMonkey in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2009, 02:12 PM
  4. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  5. subtle issue that i cant find
    By keira in forum C++ Programming
    Replies: 6
    Last Post: 02-27-2008, 08:10 AM