dynamic allocate practice program

im practicing to allocated memory dynamically
should i really need to check if the dynamic memory is successfully allocated everytime i allocate a new dynamic memory?
i havent learned exceptions yet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
int main() {
std::cout << "Enter your name: " << std::endl;
std::string *name = new std::string;
if ( name == NULL ) {
    std::cout << "ERROR! TRY CONSULT THE ADMINISTRATOR" << std::endl;
return 0;
}
std::cin >> *name;
std::cout << "Your name is: " << *name << std::endl;
std::cout << "[1]Delete [2]Save" << std::endl;
int *select = new int;
if ( select == NULL ) {
           std::cout << "ERROR! TRY CONSULT THE ADMINISTRATOR" << std::endl;
return 0;
}
std::cin >> *select;
if ( *select == 1 ) {
    delete name;
   if ( name == NULL ) {
std::cout << "Your account has been deleted! " <<std::endl;
std::cout << "Empty account "<< std::endl;
}
}
else {   
        delete select; // should i delete the select yet? even i will use this select on the //upcoming codes?
        std::cout << "Your account has been save" << std::endl;
}

}
Last edited on
if your going to check your pointer against NULL then you need to mark new with nothrow. If you don't then an exception will be thrown and your check will never execute. new (std::nothrow) . Also prefer nullptr over NULL.

should i really need to check if the dynamic memory is successfully allocated everytime i allocate a new dynamic memory?

Well its probably a good idea but if all your going to do is exit the program. Then you can just let new throw an exception an not catch it because the default behavior is to close the program when an exception is not caught
Last edited on
new will throw an exception if it fails, and because you don't handle the exception the code that follows will not run, which means your null checks are unnecessary and will never be used.

If you want new to return null on failure you could use std::nothrow.
 
std::string *name = new(std::nothrow) std::string;

I recommend you learn exceptions because that's why they are used, so that we don't have to have error checks all over the place.
Last edited on
You should not practice mere allocation. Learn to deallocate too. Your program, however, has so many possible paths that proper manual deallocation is daunting.

Therefore, learn to use the automatic option:
1
2
3
4
5
6
7
8
9
#include <string>
#include <memory>

int main () {
  using std::string;
  std::unique_ptr<string> name( new string );
  // use name
  return 0;
}
> learn to use the automatic option:
std::string name;


1
2
delete name;
if ( name == NULL ) {
deleting a pointer does not nullify it.

> should i delete the select yet? even i will use this select on the //upcoming codes?
delete it when you are done with it.


Also, learn to indent.
> learn to use the automatic option:
std::string name;

Good point. Using OP's example was not very useful.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <memory>

class Foo {
 // details
};

class Bar : public Foo {
  // details
};

int main ( int argc, char * argv[] )
{
  std::unique_ptr<Foo> gaz;
  if ( 1 < argc ) {
    gaz.reset( new Bar );
  } else {
    gaz.reset( new Foo );
  }
    
  // use gaz

  return 0;
}

Polymorphism is a use for pointers, but are we there yet?
@Yanson
Well its probably a good idea but if all your going to do is exit the program. 

isnt it good if i exit the program immediately if the allocating fails? because it will be a problem on the next codes.
@keskiverto
 learn to use the automatic option:

what do you mean by automatic option?
Polymorphism is a use for pointers, but are we there yet? 

im not there yet. just started learning oop access specifiers and classes
Last edited on
@ne555
deleting a pointer does not nullify it.

so the pointer only becomes free from heap and available to others that will use it. right?
what should i write to check if the pointer is successfully deleted?
Last edited on
what do you mean by automatic option?

The std::unique_ptr. Check out what it does.

to check if the pointer is successfully deleted?

You don't delete the pointer. A pointer is a local variable, just like int x;
The pointer's value is an address. A number. A location.

The delete keyword first calls destructor of the object at the location and then deallocates the memory that is at that location. The deletion does not change the value of the pointer. The pointer still has the same number, but now that address does not contain properly allocated memory.

You should do:
1
2
delete name;
name = nullptr;


We do not really care whether the deletion actually succeeds. If delete name; fails, what could you do?
Logically correct program would not attempt to access memory that it assumes to be unavailable.
On the other hand, if the destructor does throw an exception, then we either handle it or the program exits.
Topic archived. No new replies allowed.