Thread: Is this right

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > X = new Test;
    This is the only thing you allocated, so the only thing you can delete is X
      delete X;

    > int *ptr;
    Whilst this is a pointer, which you initialise with
    > ptr = &a;
    You didn't initialise it by calling new, so its wrong to call
      delete ptr;
    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.

  2. #17
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ok quick look at dynamic memory....

    You ask the operating system for a chunk of memory on the heap by using the new keyword. This returns a pointer to the given memory or either throws std::bad_alloc or returns 0 if unsuccessful (depends on compiler mostly).Dont lose this pointer because it is the key to freeing up the memory when you have finished with it.

    Code:
    #include<iostream>
    using namespace std;
    
    class A
    {
    private: int x;
    public : A() : x(100) {}
                print() {cout<<x;}
    };
    
    int main()
    {
    // lets dynamically create an instance of A
    A* pointer_to_A = new A;
    if(pointer_to_A)
    {
    pointer_to_A ->print();
    //ok finished with memory so now we free it like this
    delete pointer_to_A;
    //but hang on pointer_to_A still points to our freed memory so for safety
    pointer_to_A=NULL;
    }
    // ok now a dynamic array of A objects
    A* pointer_to_array = new A [5];
    if (pointer_to_array)
    {
    for(int i=0;i<5;i++)
    {
    pointer_to_array[i] -> print();
    cout<<endl;
    }
    // now we are finished with it we free it like this
    delete [] pointer_to_array;
    pointer_to_array=NULL;
    }
    return 0;
    }
    anything there you dont follow?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    Stoned_Coder thanks mate that works a treat


    But i still don't know what happens to this pointer
    does it need to be freed


    Code:
    
    ptr = &a

    Do i just leave ptr or should i do this


    ptr = NULL;


    Thanks guys i hope this is my last post on this one



    Marky_Mark

  4. #19
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    there are 3 ways to get memory dynamically and these all need their own freeing mechanism....

    1) malloc() ...... you have to use free()
    2) new ...... you have to use delete
    3) new[] ...... you have to use delete[]

    Any pointer that doesn't point to dynamically allocated memory does not have to be freed, but it is still good practice to set it to null when you are through using it. (unless the pointer goes out of scope right away).
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed