3

I have a strange issue related to dynamic memory allocation to char pointer. I have something like

char *input = new char; //1
gets(input) //2
char *dest = new char; //3

during the step3, i get heap corruption error during runtime. This only happens if the length of the string that i enter is more than 23 characters.

If i dont do any new operations, then there is no issue.

This issue is resolved if i specify

 char *input = new char[100]; 

But i want the input to be dynamic based on the user's input.

I am not sure what is the role of 24 bytes in this case. I dont want to limit to 100 or some n characters... I am kinda weak in memory allocation...Can somebody explain this scenario?

Kartikkumar Rao
  • 171
  • 2
  • 18
  • 1
    You only allocate enough space for one character. Then you store some larger number of characters into the too-small space. That's just broken. – David Schwartz Dec 24 '12 at 14:14

5 Answers5

6

No, you can't do that way. You could use only static buffer or use "cpp-way", which is:

std::string str;
std::getline(std::cin, str);
kassak
  • 3,974
  • 1
  • 25
  • 36
5

Your program exhibits undefined behavior on input of any length greater than zero, because gets adds a null terminator. The fact that the program does not crash up to 23 characters is an unfortunate coincidence.

If you want the buffer to be allocated dynamically, do not use gets or char-based input; instead, read your data into a std::string.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Is it just a coincidence, cos i am able to reproduce this issue in other machines as well... – Kartikkumar Rao Dec 24 '12 at 15:28
  • 3
    @karthikkumar24 Absolutely, it is: the implementation of `new` in your compiler gives you 24 bytes when you ask for just one, as a way to reduce fragmentation. On different compilers the additional size will be different, and could potentially be as small as zero. The standard prohibits you from reading or writing beyond the allocated boundary, so in theory the program could crash even when you read one character. Errors like that are hard to find, because very often the program does not crash (that's why I called it an unfortunate coincidence). You need valgrind to discover errors like that. – Sergey Kalinichenko Dec 24 '12 at 15:39
2

If you don't know what length user will input and you don't want to limit user input, then you have to allocate an enough length of buffer. You may use STL string, but it also allocate enough memory for you dynamically, it just hide the allocation detail to your program.

TieDad
  • 9,143
  • 5
  • 32
  • 58
1

Never ever use gets() (from man page):

No check for buffer overrun is performed

Depending what is entered there you get a buffer overflow.

Andreas Florath
  • 4,418
  • 22
  • 32
0
new char

Allocates memory for ONE char. It could fail after the second char (actually even after the first as you'll get an '\0' char.

Please, use C++ if you tag the question as C++, not C! That means using std::string

Tristram Gräbener
  • 9,601
  • 3
  • 34
  • 50