Thread: questions about new and delete

  1. #1
    Unregistered
    Guest

    Smile questions about new and delete

    Hi,
    I have some questions about operator new and delete .


    class record
    {
    public:
    record(int,string);
    record();
    ~record();
    int number;
    string name;
    function...

    };

    //constructor
    record::record(int m ,string n)
    {
    number=m;
    n=name;
    if (......) //some condition is checked
    {exit1(); //if condition is true terminate the program }
    }

    //deconstructor
    record::~record() { }


    main()
    {
    p=new record (1,"Papoo");
    delete p;
    }

    The questions I have in mind are:
    1) If I am trying to create an object p of type record dynamically by using new and in the constructor there is some condition to be checked and let's assume if that condition is true the program terminates.
    Since the program is terminated is the new object p being created?Based on the fact that the condition is true, following questions arise.
    2) If the object p is being created using new, is it being deleted from the memory?
    3) Since the (delete p ) is in main() and its not even being called from the main(), is the memory free or there are going to be memory leaks associated?

    The program that I have may not have meaning, but actually I have some other program which is much complicated and I cannot put it on board, so I am just trying to clear some of my doubts through this program.

    Thanks,
    papoo.

  2. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Since 'new' returns a pointer to an object created on the free store, if it is unsucessful it will return zero. Instead of exiting a program you might want to consider throwing an exception, although I'm sure that exiting would kill the program an free all memory.
    I compile code with:
    Visual Studio.NET beta2

  3. #3
    Unregistered
    Guest
    Well actually once the dynamic memory is created on the heap the program must delete it. Simply exiting the program will not remove the memory allocation from the operating system. You should either create the constructor in such a way that the dynamic memory is allocated only if the test are true or use the autopoint function in STL.

  4. #4
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    The allocation would fail. The memory is not allocated until the pointer to the memory is returned, in this case nothing would be returned, however I do question the need to make any tests within a constructor. I don't think it makes much sense. The purpose of the constructor is to initialize the instance of the type.
    I compile code with:
    Visual Studio.NET beta2

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> if it is unsucessful it will return zero.

    Actually, this is not ANSI standard behaviour. To correctly follow the standard, a call to new which fails should throw a bad_alloc exception and the return is undefined. To return NULL on failure one should use the...

    x = new(nothrow) something;

    ... construction. It is a "feature" of several compilers, including MS VC++ that this is incorrectly implemented.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    I see what you are saying. This is an excerpt from VS.NET, not the whole help page.

    The new keyword allocates memory for an object or array of objects of type-name from the free store and returns a suitably typed, nonzero pointer to the object. If unsuccessful, by default new returns zero. You can change this default behavior by writing a custom exception-handling routine and calling the _set_new_handler run-time library function with your function name as its argument....
    I compile code with:
    Visual Studio.NET beta2

  7. #7
    Unregistered
    Guest

    Question

    Hi Everyone,

    I read all the opinions and I would like to thank all of you for responding. But I am a bit confused as to how solve the problem.

    First of all if the object is being created and the constructor is checking some condition. The object p is being created using new and in the constructor we have those variables being assigned to the public variables of class record. i.e. number=m and n=name. Then using the variable number some condition is being tested and if the condition is true the program should exit() or abort().

    //constructor
    record::record(int m ,string n)
    {
    number=m;
    n=name;
    if (number ......) //some condition is checked
    {exit1() or abort(); //if condition is true terminate the program }
    }


    All this time I was thinking that if the condition is true then the abort() or exit() is called and hence the object is not being created at all. But I was wrong when I tested my program again without the abort or exit command it seemed like the object was created and I was unaware of it.

    As Witch_king said "new returns a pointer to an object on free store,if it is unsuccessful it will return zero". I guess its
    unsuccessful if the memory cannot be allocated but since in my case the constructor is just checking some condition does that mean if the conditions are true and the program is aborted the memory is not being allocated and the new is unsuccessful?(i.e. the pointer to the memory is not returned)

    But what I think if I understand correctly is that the pointer to the memory is returned and it's the condition which I have to test that is making things look different.

    My program should be such that if I create a object using a constructor and if in the constructor I check some condition and if the condition is true I should abort the program.
    And since it looks like the object was being created inspite of the conditions being true, how can I delete the object ? i.e. I should be able to use the abort() command as well as I should delete the object if created.

    I hope whatever I am trying to say is clear.

  8. #8
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you want to ensure that all dynamically allocated memory is deleted before termination then you have a design flaw (most o/s's will clean up for you but it's not guaranteed). It's not just the memory for allocating the instance of the class that wont get deleted but any memory that you've allocated for any other purpose in your program.

    If you want to explicitly delete the dynamically allocated memory then you may be better off setting a flag in your class and then checking that flag from main() after the constructor has been called(not good OOP though). Alternatively you could call a global function to cleanup the memory from your constructor before exit, however if you de-allocate the memory during your constructor then your destructor wont get called.

Popular pages Recent additions subscribe to a feed