Thread: Dereferencing Class Pointer List

  1. #16
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103

    Red face

    I have provided the definitions, although I don't think the constructors or Add() functions are the problem. I runs fine in Release Mode, however when i try to access a container through the pointer list class, it gives an error.... Pointers are confusing..
    Be easy on me...only 14

  2. #17
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Whoa, you definitely have code that should be throwing warnings and/or errors. If your compiler isn't rejecting this then I'm very surprised.

    Let's take a look at your main function:
    Code:
    int main(int argc, char *argv[])
    {
       //mynewList->Add();
       cout<<"BEFORE: "<<endl;
       ShowAll(*mynewList->GetList());
    
       // There is no need to make this a pointer!  It's ridiculous.
       medList::iterator *it = &mynewList->GetList()->begin();
    
       while( (*it) != mynewList->GetList()->end())
       {
          Results->push_back(&(**it));   
          (*it)++;
       }
       system("PAUSE");  // You really should #include <cstdlib> if you're going to use this...
    
       medSearchResultList::iterator iterate = Results->begin();
       while(iterate != Results->end())
       {
          // Whoa!  Why are you using 'it' here!?
          // Furthermore, you are trying to dereference the return value
          // from a function that returns void!!!
          *(*it)->SetName("FROM POINTER!");
    
          // Oh jeez, you forgot to increment your iterator...
          // Can you say infinite loop?
       }
       cout<<"MODIFIED THROUGH POINTER LIST CLASS"<<endl;
       ShowAll(*mynewList->GetList());
    
       system("PAUSE");
       return(0);
    }
    Here's a slightly fixed up version:
    Code:
    int main(int argc, char *argv[])
    {
       //mynewList->Add();
       cout<<"BEFORE: "<<endl;
       ShowAll(*mynewList->GetList());
       medList::iterator it = mynewList->GetList()->begin();
    
       while (it != mynewList->GetList()->end())
       {								
          Results->push_back(&*it);
          it++;
       }
       system("PAUSE");
    
       medSearchResultList::iterator iterate = Results->begin();
       while(iterate != Results->end())
       {
          (*iterate)->SetName("FROM POINTER!");
          iterate++;
       }
       cout<<"MODIFIED THROUGH POINTER LIST CLASS"<<endl;
       ShowAll(*mynewList->GetList());
    
       system("PAUSE");
       return(0);
    }
    Your compiler is hopefully also throwing numerous warnings about how you are passing const char pointers into your constructors/functions and assigning them to non-const char pointers. This is very dangerous. You might be tempted to try modifying those internal strings at some point, at which time you will most likely experience crashes.

    I understand that you're trying to learn about pointers, but your usage of them is pretty ridiculous at times, and in several places you would be better off not using pointers. I find this code very hard to follow.

  3. #18
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Crap! Did not know why I was doing that! That was an error generated right after fixing the errors I previously asked. It all works now! Although when I compile it in DEBUG mode it still forces that break point. It only works if I compile it in release mode. Can anyone explain why the error happens in debug mode?

    And BTW, I was adding to this code around 3:45 AM, so that could probably explain that stupid stunt...
    Last edited by toonlover; 08-15-2008 at 07:46 PM.
    Be easy on me...only 14

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Well, when you compile in debug mode, your compiler is setting compile time preprocessor flags for you. This means that additional checks are being compiled into many of the standard library classes.

    Why are you getting that particular error? I have no idea. However, as easy as it would be to point the finger at the compiler writers and say they screwed up somewhere, they most likely didn't.

    You're probably doing something wrong somewhere... (how's that for a helpful error message? )

    [edit]
    By the way, which compiler are you using? I just compiled the exact code you posted, plus the fixes I suggested, using both the MinGW compiler and Visual Studio 2008, and neither of them gave me any compilation/runtime errors.
    [/edit]
    Last edited by arpsmack; 08-15-2008 at 07:47 PM.

  5. #20
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Well that's odd. I originally began to code this with Code::blocks, and then I migrated to Visual Studio 2005 w/out any service packs installed ( might be the reason? ). I tried to recompile the code once again in Code::blocks after the progress done with Visual Studio 2005 and compiles it flawlessly. Perhaps it is one of the Preprocessor flags included in the DEBUG mode, and I believe it is _HAS_ITERATOR_DEBUGGING.
    Last edited by toonlover; 08-15-2008 at 08:18 PM.
    Be easy on me...only 14

  6. #21
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    My small experience with Visual Studio is that whenever you're in debug mode, the error comes up when you're trying to de-reference a NULL pointer or delete an object of a foo pointer.

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you get runtime errors in debug builds then your code is still wrong, and may yet fall over unexpectedly later.
    Besides, you probably can't get by with a non-working debug build.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Pointers are confusing..
    Yep, and you're having a hard time with them. You're also using them in many places where it is not necessary, and now you have a program full of pointer errors. Unfortunately you've got a lot more to fix up besides the mismatched iterators.

    The first thing I see is that you don't have a copy constructor or copy assignment operator for your SingleListClass even though you have a member variable allocated with dynamic memory. The rule of three says that if you need a destructor, copy assignment operator or copy constructor, you probably need all three. Here you have a destructor and you need the other two. A better solution is to not use a pointer here. There's no benefit and if you don't use dynamic memory you won't need the destructor, copy assignment operator or copy constructor.

    The second thing is that you leak memory in SingleListClass::Add(). You allocate a temp _medContainer dynamically, then you push_back a copy of the object. You never delete the temp one (the copy will be cleaned up automatically by the list).

    There's probably a lot more errors lying around, I haven't looked closely. I'd suggest using pointers only where they're necessary and learning how to use them correctly that way first.

    As to your original problem, did you try the main() program example I posted? Did it work or fail? In Release mode it might work because the extra checks done by the compiler to help you are skipped, you want to make sure it works in debug mode and release mode.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-22-2009, 05:03 AM
  2. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 05:50 PM
  3. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  4. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM