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.
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 = newchar;
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.
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)
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 = newchar[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[].
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)
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 = newchar;
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.