Thread: C++ list Strange Behavior

  1. #16
    Registered User
    Join Date
    Jun 2006
    Location
    Singapore
    Posts
    18
    Thanks guys,
    I have replaced all my char array with string but the problem still persist.
    As pointed out by Daved, I also removed the for() loop and change that part to:
    Code:
            //id format is like this "crescent_0_1"
            char num;
            num = id->at(id->find_last_of('_')+1);
    	vertex = atoi(&num);
    	
    	num = id->at(id->find_first_of('_')+1);
    	segmentId = atoi(&num);
    	roadname = id->substr(0, id->find_first_of('_'));
    In this case, calling the default constructor of the list also doesnt solve my problem.

  2. #17
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
            char num;
            num = id->at(id->find_last_of('_')+1);
    	vertex = atoi(&num);
    this won't work. atoi expects a zero terminated string and you pass it a pointer to a single character.
    Kurt

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char like '6' can be converted to Int just by
    int vertex = charNum - '0';
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    Registered User
    Join Date
    Jun 2006
    Location
    Singapore
    Posts
    18
    thanks vart, ur method works wonderfully. I did not know the conversion can be done this way.
    Regarding the memory issue, I really have no idea on how to overcome it. After inserting 117 items, only 63 entries in the list. Hence, I redesign my program and eliminate the list usage in my function call. However, my other parts of program which uses list works successfully, perhaps they store fewer items.
    Finally, thanks guys for offering ur valuable advice.

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Regarding the memory issue, I really have no idea on how to overcome it.
    Start using some of the tools I referred to in a previous post.

    Your code has a hidden bug, which despite "I redesign my program and eliminate the list usage in my function call" is still there. No doubt at some future date, when you've added some other feature, the bug will reappear, and you'll be back asking basically the same question.

    Picking language constructs which avoid the bug won't work. Sooner or later, you will need to add a feature, and you won't find a construct which avoids the bug.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM