Virtual Memory Errors using new w/destructors and static array

This is a discussion on Virtual Memory Errors using new w/destructors and static array within the C++ Programming forums, part of the General Programming Boards category; This class-based program uses virtual memory in the driver routine and implementation routines. If I add a static char array ...

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    Virtual Memory Errors using new w/destructors and static array

    This class-based program uses virtual memory in the driver routine and implementation routines. If I add a static char array by the instructors request, I keep getting execution errors, but it does compile okay.

    I am just trying to get the name to print and continue before moving on. It does print the name as requested but crashes once the destructors are called. Commenting out the destructors seems to work but that is not a solution.

    If I remove the static char array and just print a last name, it works fine with no errors but something with the strcat function and the destructors is possibly causing a memory problem.

    Error:
    Unhandled exception at 0x77f767cd in program3.exe: User breakpoint.


    Code:
    //*************** Person.h (Class Header)
    class Person {
    
    public:
       Person ();
       ~Person();
       Person (char*, char*, char*, char*, char*, char*);
    
       const char* GetFullName () const;
    
       virtual void Print ();
    
    protected:
       static char* fullName;
       char* firstName;
       char* LastName;
       (other code omitted)
    
    }  // end of class header
    
    // ************ Person.cpp (Class Implementation)
    
    Person::~Person()
    {
    // works if fullName function removed and fullName function works if this removed?
    
       delete [] FirstName;
       delete [] LastName;
    }
    
    
    char* Person::fullName = 0;  //Initialize static char array
    
    Person::Person (char* first, char* last.......)
    { SetName (first, last); }
    
    void Person::SetName (char* first, char* last)
       FirstName = new char [strlen (first) + 1];
       LastName = new char [strlen (last) + 1];
    
       strcpy (FirstName, first);
       strcpy (LastName, last);
    
    // If these are removed, it works but then I have no way of retrieving the full name.
       fullName = strcat (FirstName, " ");
       fullName = strcat (fullName, LastName);
    }
    
    const char* Person::GetFullName () const {return fullName; }
    
    void Person::Print () { cout << GetfullName(); }
    
    // End of Person.cpp
    
    
    // ************** School.cpp (Driver)
    
    int main ()
    {
       Person *p2 = new Person ("Dave", "Walters", "1000 Street", "City", State", "Zip");
    
       p2->Print ();
    
    // works if fullName function removed and fullName function works if this removed?
       delete p2;
       p2 = 0;
    
    } // end of driver main

  2. #2
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    there is nothing at the other end of *fullName

    you must make a place for your fullname
    .sect signature

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 06:05 PM
  4. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 06:24 PM
  5. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM

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