-4

Let's say i have this program:

#include <iostream>
using namespace std;

int main(){
char *c = new char;
cin>>c;
c = "Hello";
cin>>c;
delete c;
}

Now, I understand that char pointer is a constant pointer who'se values must not be changed ( or else it's undefined behavior ) , however , in the following program I don't understand really what's going on , from what I do all I can see is that I'm trying to create constant string literals somewhere in memory and address them to the pointer ( hence the cin>>c; and c = "Hello"; ) which is allowed with char pointer ( I'm not trying to change the values pointed to by the pointer ) , however , perhaps there's some sugar-code in the program above or I'm just interpreting it wrong, and that's why I need help, why does the above program throws me an error indicating about undefined behavior? , Also , since i'm inputting more than 1 literals into c , am I supposed to do : delete[] c ? (since the collection of literals is basically an array, although c is not declared as an array of char pointers but a pointer to char )

Alan Stokes
  • 18,320
  • 3
  • 41
  • 63
zLeon
  • 127
  • 8
  • 2
    You are loosing the pointer created with `new` dude. – πάντα ῥεῖ Sep 13 '15 at 08:13
  • Even if I don't dynamically allocate ( get rid of the 'new' and 'delete' and initalize 'char *c = "hello " ' , it will still give an error. – zLeon Sep 13 '15 at 08:16
  • Have you by any chance programmed in a garbage collected language before, like Java? – Weak to Enuma Elish Sep 13 '15 at 08:19
  • 3
    You can't delete a pointer to a literal with static storage allocation. – πάντα ῥεῖ Sep 13 '15 at 08:19
  • I didn't used to program in java , and also , I know I cannot delete a pointer whose not dynamically allocated , what I meant was Is displayed in the following code : https://ideone.com/WJHnMf , even if I do my program without dynamic allocation of 'c' , it will still give me an error in my compiler ( code::blocks ) – zLeon Sep 13 '15 at 08:24
  • @zLeon: Try running it [with user input](https://ideone.com/rDCHFN) – Claudiu Sep 13 '15 at 08:26
  • 1
    Use `std::string`, drop `new` and `delete`, and all problems are gone. :) – leemes Sep 13 '15 at 08:31

1 Answers1

3
char *c = new char;

c now points to a memory address which has space for one character.

cin>>c;

This will probably cause undefined behavior. From this answer:

This operator [>>] expects that p points to some memory (automatic,global,dynamic - no matter) - it does not allocate memory by itself. It just reads characters from input stream until whitespace and copy it to the memory pointed by p - but p must already points to some memory.

If there is more than one character in the input then this will write to memory that isn't allocated.

c = "Hello";

This re-assigns the pointer to the address of the static, const string "Hello" - specifically, to the address of the first character of that const string. The address of the memory you allocated with new is now lost.

cin>>c;

This is undefined behavior because it tries to overwrite the characters of a const char * - writing to consts is undefined behavior.

delete c;

This is undefined behavior because it tries to delete a pointer (the address of the "Hello" literal) that was not returned by an operator new.

(Thanks to Peter for fleshing out the last two paragraphs.)

Community
  • 1
  • 1
Claudiu
  • 206,738
  • 150
  • 445
  • 651
  • Yes , I understand now that I shouldn't have allocated it dynamically in the first place, but even if 'char * c ' is not dynamically allocated , as such: https://ideone.com/rDCHFN I will still get errors . – zLeon Sep 13 '15 at 08:30
  • @zLeon: In that example this sentence applies: "I think this is undefined behavior because it tries to overwrite the values of the static string "Hello"." `"hello"` is essentially a `const char *` - you can't write to `const`s. – Claudiu Sep 13 '15 at 08:32
  • The assignment `c="Hello"` makes `c` point to the first character of the string literal `"Hello"`. Both the cases where you say "I think this is undefined behavior" are definitively undefined. The first is undefined behavior because a string literal is logically `const`, so overwriting its characters produces undefined behaviour. The second is undefined because it is attempting to use operator `delete` on a pointer (to the first character of a string literal) that was not returned by a corresponding operator `new`. – Peter Sep 13 '15 at 08:39
  • I'm testing in the compiler and I notice that each time i'm trying to use cin>>c , it makes my program crash , this is the main problem ( if we don't include the dynamic allocation ) because like you said in the answer , the operator>> expects something different with the char pointer. , So I should avoid using 'operator>>' on char pointers at all. – zLeon Sep 13 '15 at 08:44
  • @Peter: Thanks! I wasn't sure of the exact reasons off-hand - still relatively new to C++. I've updated the answer. – Claudiu Sep 13 '15 at 08:51
  • @zLeon: What would work more of the time is doing `char *c = new char[200]; cin >> c;`. This would work so long as there are less than 200 characters in the next word of stdin, which you can't guarantee, so you shouldn't do. You should just cin to `std::string`s . – Claudiu Sep 13 '15 at 08:52