Dynamic Memory Allocation.

I am a practicing some Dynamic Memory Allocation for my final exam. I was trying something and I ran across a problem. Prof said, you have to always have a delete operator when you use the new operator, so I was trying something with chars and the delete operator isn't working as it is supposed to. Here is my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

 int main()
 {
cout << "Dynamic memory allocation with Char" << endl;
char*pointer_1 = new char;
cin>> pointer_1;
cout << pointer_1 << endl;

if(pointer_1 != NULL)
{delete[] pointer_1;
pointer_1 = NULL;}

int i;
cin>> i;

return 0;
 }

If I take out the the part from if....pointer_1=NULL} it works and also prints out what I input, but with the delete there is a crash . I have learned that it is a must to use delete[] or else it will/may cause memory leeks. Can someone point me as to what I am doing wrong? and another note is I haven't specified char[] because I wanted to put in whatever I wanted, not just a limited amount of characters.
EDIT: Small edit, I added delete right after I implemented it to look like this
1
2
char*pointer_1 = new char;
delete[] pointer_1;

Now I am able to do what I want but that just feels wrong, what was the point of having new and delete.
Last edited on
delete[] can only be used with a pointer created by new[]. Your pointer is created by new, so you can only use delete, if you must (better to use appropriate smart pointers)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
    cout << "Dynamic memory allocation with Char\n";
    char* pointer_1 = new char;
    cin >> *pointer_1;
    cout << *pointer_1 << '\n';

    delete pointer_1;
    int i;
    cin >> i;
}


And yes, there is no point: to input "whatever you want", you should be using a string.
Last edited on
Are you trying to input a single character or a string?

If just a single character, then:
1
2
3
cin>> *pointer_1; // You missed the * in front of pointer_1
// ... do stuff with *pointer_1 ...
delete pointer_1; // Not delete[] ! 


If you wanted to input a string, then:
1
2
3
4
char* pointer_1 = new char[someSize]; // Where someSize is some integer size
cin.getline(pointer_1, someSize); // Best use cin.getline
// ... do stuff with pointer_1 ...
delete[] pointer_1; // [] is very important 

(of course, if you want to input a string, best use std::string)

In general, if you use new to allocate memory, you must free it with delete.
If you use new[], then free it with delete[].
Last edited on
Cubbi, Thanks for that heads up, I wasn't aware of that. And also like long suggested I am trying to input strings like "Hello" and stuff. As for the edit that you made, if I put in "Hello" cout gives me H and rest is gone. Also now I am using this code and everything works the way I want it to, put in "hello" cout is "hello" and there is a delete and although there is no syntax errors or run time errors it just feels like its wrong. long I am aware of that, but I don't want to limit the size to an integer, I would like it to be as long as "the user" wants it to be. I know strings are a better idea for this but I wanna work with chars because most of the examples in my notes/textbook are using chars for this section(except they all have a integer size limit)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

 int main()
 {
cout << "Dynamic memory allocation with Char" << endl;
char*pointer_1 = new char;
delete pointer_1;
cin >> pointer_1;
cout << pointer_1 << strlen(pointer_1) << endl;

int i;
cin>> i;

return 0;
 }

It works, so maybe I am being paranoid, but I feel like something is wrong because it worked when I didn't have a delete.
This is an error:

1
2
delete pointer_1;
cin >> pointer_1;

you're accessing characters referenced by a deleted pointer. It is undefined behavior, that is, it may "work", it may crash, it may modify some entirely unrelated variables, it may do anything at all.

This is also an error:

1
2
char*pointer_1 = new char;
cin >> pointer_1;


You've allocated one character, but you're writing more than one (even if you enter one letter, two characters will be written, since this particular form of input adds the null character). Again, it's undefined behavior, it may or may not crash.
Topic archived. No new replies allowed.