Thread: run time error-pointer assign

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    run time error-pointer assign

    Hi, Can anyone explain me why this code causes run time error:
    SimpleCat *pRags=new SimpleCat;
    pRags=&Frisky; //if I put this then run-time error
    cout<<"Frisky is "<<pRags->itsAge<<"age"<<endl;
    delete pRags;

    I have class SimpleCat, and if class is some like new data type why I can't use analogy
    int x=2;
    int *p=new int;
    p=&x
    ??????

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > pRags=&Frisky; //if I put this then run-time error
    Because you can only delete what you got back from new
    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.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    That is ok, but what if I create two or more objects, then how can I declare pointer to point to specific object I want
    for example:
    SimpleCat First,Second;
    SimpleCat *p=new SimpleCat;
    p.itsAge=2;
    to which of these two objects is value of member variable itsAge is set to 2????

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I meant p->itsAge not p.itsAge

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Do this (remove SimpleCat First,Second;)

    SimpleCat *p=new SimpleCat[2];

    Now you have two cats, and can do this...

    p[0].age = 2;
    p[1].age = 4;

    When you're done, tidy up with

    delete [] p;
    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.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    You don't need to allocate memory to a pointer if you're just using it to point to already constructed objects.
    Code:
    SimpleCat First,Second;
    SimpleCat *p=new SimpleCat;
    There are four objects here. Three are of type SimpleCat and one is of type pointer-to-SimpleCat. The expression
    new SimpleCat allocates an instance of SimpleCat on the heap and returns a pointer to it.

    If you just want to point to your objects, you can do this:
    Code:
    {
      SimpleCat First, Second; 
      SimpleCat *p;
      p = &First; // now p points to first
      int firstAge = p->itsAge;
      p = &Second; // now p points to second
      int secondAge = p->itsAge;
    } // end of scope.. no need for delete because no objects
      // were allocated with new

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Thanks guys!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Run time error?
    By shwetha_siddu in forum C Programming
    Replies: 6
    Last Post: 04-21-2009, 01:37 PM
  2. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  3. Displaying Function Run Time
    By pf732k3 in forum C Programming
    Replies: 1
    Last Post: 03-21-2008, 12:36 PM
  4. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 07:11 AM
  5. Linked list question....again
    By dP munky in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2003, 05:59 PM