C++ list Strange Behavior

This is a discussion on C++ list Strange Behavior within the C++ Programming forums, part of the General Programming Boards category; Thanks guys, I have replaced all my char array with string but the problem still persist. As pointed out by ...

  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
    ZuK
    ZuK is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    char like '6' can be converted to Int just by
    int vertex = charNum - '0';
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    32,823
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21