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; I m using list in my window mobile app but experienced strange behavior. For example, Code: for(int k=0; k<50; k++) ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Location
    Singapore
    Posts
    18

    C++ list Strange Behavior

    I m using list in my window mobile app but experienced strange behavior.
    For example,
    Code:
    for(int k=0; k<50; k++)
    	list1.push_back(100);
    The list1 only contains the first 31 entries of 100, the remaining 19 entries are fill with '?'
    One of the possible reason maybe due to insufficient memory, however, when i test with
    Code:
    for(int k=0; k<200; k++)
    	list1.push_back(100);
    The list1 contains the first 127 entries of 100 and the rest are fill with '?'

    What could be the possible reasons? thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,403
    Strange. What happens if you use:
    Code:
    list1.insert(list1.begin(), 50, 100);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    Yes, that is strange. Is it possible that the problem is with your outputting of the values rather than your insertion?

  4. #4
    Registered User
    Join Date
    Jun 2006
    Location
    Singapore
    Posts
    18
    Hi laserlight,

    I tried the code you provided and the list1 contains value of 100 for the first 31 entries, and the rest are '?'

    I observed the values from the debugger window, and this is my first time encounter such problems. In my other projects, the list usage was fine.

    However, this is my first attempt in Window Mobile App, so I m wondering if the problem is due to memory. But the 2nd test seems not to be the case.

    I really have no clue to the problem now. Hope someone can give some hint to solve it.
    Many Thanks

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Have you tested this in an isolated program?

    Because if this is just part of a much larger program, then memory mis-use elsewhere in the program could be showing up here, even though there is nothing wrong here.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Location
    Singapore
    Posts
    18
    In isolated program, the list works fine.
    You are correct, this is a part of a larger program.
    I have tried to eliminate possible dangling reference and memory leak by following the guide from http://www.linuxdevcenter.com/pub/a/...-1.html?page=3
    but the problem still exist. My app uses splay-tree, r-tree and lists, which are prone to errors if not carefully implemented.
    Regardless of some additional mistakes that I removed, the returned list entries only stored up to 63 entries after I inserted 117 items. I notice that those items after 63th overwrite the 1st entry of the list, while the previously 63th entry is replaced by 62th item. In other words,
    Any item beyond 63 entry becomes '?'

    P.S. All items insert at the front.

    Thanks for the help.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Location
    Singapore
    Posts
    18
    After googling in the web, I found that the problem maybe caused by buffer overflow, where the new items overwrite the existing entries. But I still need to figure out how to overcome such problem.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > I m using list in my window mobile app
    Does it also work as a regular windows desktop application as well?

    I would certainly suggest you create a PC version for your core algorithms and data structures for some initial testing, simply for the convenience of testing and debugging.

    Finding random memory overruns elsewhere is hard work without some tools to help you.
    http://en.wikipedia.org/wiki/Memory_debugger

    You talk about memory overruns. Does this mean you're using horrible C-style char* arrays for strings for example?
    I mean, if you're using std::list, are you using std::everything else for all the other primitive data structures (eg vectors in place of arrays).
    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.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Location
    Singapore
    Posts
    18
    Hi Salem,

    I am building the app for embedded platform, using the PDA emulator of Visual Studio .Net2005. As I need to do some Graphics rendering, all my testing was on the emulator.
    You are rite, finding random memory overruns is a tedious process, hence I have been stuck in this problem for a week.
    Indeed, my string representation is in char* array. I had implemented my own string class just to overload the comparison operators. Will this cause an adverse effect?
    Thanks.

  10. #10
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    You'd probably get the best results from just sticking with a std::string.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  11. #11
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    I was wondering if we could see the code that occurrs right before the loop where you insert values into the list, and also the code right after the loop where you insert values.

    Much of the time, the code right before and right after has some definite impact on the way that small snippet of code will function....although at other times the error could be at some obscure place in a completely different file of your code.

    A few weeks ago I had an error which boggled me, because I was also receiving some ? values in my lists while using the std::list data structure.

    It turned out that it was in the constructor of the class I was working on. The list was a member of that class, and I had not initialized it properly. I had thought that C++ should call the default constructor on the list automatically...but for some reason the default constructor never got called...when I called it myself everything suddenly became okay. It was a weird one.

    Anyways, show us a bit more of the code and it might reveal some light on the subject.
    My Website

    "Circular logic is good because it is."

  12. #12
    Registered User
    Join Date
    Jun 2006
    Location
    Singapore
    Posts
    18
    Here's the callback function that I does insertion into list
    Code:
    bool MySearchCallback(String* id, void* arg) 
    {
    	Road *aRoad;
    	String roadname;
    	int segmentId, startVertex;
    	int success;	
    	DbRead *wrapper;
    	
    	wrapper = reinterpret_cast<DbRead*>(arg);
    	startVertex = -1;
    	for(int i=id->stringLength(); i>0; i--)
    	{
    		if( *(id->getChar(i)) == '_')
    		{
    			if(startVertex == -1)
    			{
    				startVertex = atoi(id->getChar(i+1));
    			}
    			else
    			{
    				segmentId = atoi(id->getChar(i+1));
    				roadname.setString(id->substr(0,i));
    				break;
    			}
    		}
    	}
    	aRoad = NULL;		
    	success = wrapper->splayTree.retrieve(roadname,aRoad);	
    	if(success)
    		wrapper->routeList.push_back(aRoad->getVertex(segmentId, startVertex));
      return true; // keep going
    }
    There are suppose to be 28 items but only 14 entries are correct. The rest are '?'
    DavidP, my list is a member of the DbRead class, which may experience the same problem as yours. How do I initialized it properly? Thanks.

  13. #13
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Well, I am not saying that initializing your list properly is your problem, it just happened to be my problem when I was having trouble with std::list.

    I just simply called the default constructor to my list in the constructor of my class:

    Code:
    MY_CLASS::MY_CLASS ( ) : mylist() { /* constructor body */ }
    My Website

    "Circular logic is good because it is."

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The for loop parameters are off. Valid indexes go from 0 to size-1 (or backwards from size-1 to 0). Your loop goes from size to 1. In addition, inside your loop you are calling getChar(i+1), so on your first iteration of the loop you will be accessing memory past the string's bounds.

    I don't know if this is causing your current problem, but it is certainly something that should be fixed.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > Indeed, my string representation is in char* array.
    Oh let me count the ways of either missing off the \0, or forgetting to count it in memory size calculations.

    You'd be better off using std::string to begin with, just so you know that it works.
    When you know that it is, then you can measure the performance and then decide whether the problems of using raw char arrays outweighs the improvements you might get.
    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 1 of 2 12 LastLast
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, 11:30 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM

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