1

I am writing simple function for merging two C-strings. While trying to dynamically alocate new chunk of memory i found out that :

char * out = new char[size];
std::cout << strlen(out) <<std::endl;

Returns 16 when size is 4 and 40 when size is 22 and so on. Does anyone have an idea why it acts like that and how to allocate memory for c-string with certain number of chars?

pawels1991
  • 91
  • 1
  • 6
  • 1
    You don't initialize the string. `strlen()` needs a 0-terminated buffer. Asked so many times... –  Aug 06 '13 at 20:18
  • 2
    Yup - as soon as I see strlen() in a question.... – Martin James Aug 06 '13 at 20:22
  • It is not clear this is a duplicate of the proposed original. That question involved applying `strlen` to an array that was initialized but that did not containing a terminating null character. This question applies `strlen` to an array that is not initialized. It is not clear from the question whether the confusion is about how `strlen` operates or about whether and how `new` initializes an array allocated this way. – Eric Postpischil Aug 06 '13 at 20:43
  • @H2CO3 - like I read SO for my whole free time and know that. The issue was problem with allocation, but lack of string terminator. It is just one of simple things that just get out of your head because you are not using it for long time. And it always suprised me - how SoftDev guys can gain such a butthurt while asked simple question . Good advice - If it hurts your soul too much and you are going to express just how much it does , instead of giving real answer - just don't do it. – pawels1991 Aug 06 '13 at 20:47
  • @EricPostpischil Right, do you have a better dupe? I am pretty confident this is a duplicate. –  Aug 06 '13 at 20:48
  • @EricPostpischil - but it is very clear when you look at the topic. – pawels1991 Aug 06 '13 at 20:48
  • @pawels1991 That's why I didn't write an answer, but a comment. –  Aug 06 '13 at 20:48
  • @H2CO3 And express a butthurt. – pawels1991 Aug 06 '13 at 20:51
  • That's why std::string was invented – Neil Kirk Aug 06 '13 at 21:01
  • @Neil because std::string was invented i forgot how use c-strings ;) – pawels1991 Aug 06 '13 at 21:06

2 Answers2

5

The allocated memory is not initialized. strlen() just keeps charging through memory until it encounters a null character ('\0'). Since you have not initialized the memory pointed to by out, the contents of this memory are undefined, and therefore so is the behavior of strlen().

strlen() does not measure the size of an allocation, so your interpretation that new is allocating too much memory is incorrect simply because the tool you are using to measure the size of the allocation doesn't do that at all.

In your case, strlen() is reading beyond the boundary of this allocation. In this circumstance, the result could be a "wrong" value (as you see here) or your program could simply crash.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • `new` initializes some objects it creates and not others, so it might be useful to explain why this allocation does not initialize the allocated memory. – Eric Postpischil Aug 06 '13 at 20:40
0

Using new to allocate memory does not initialize the memory:

char * out = new char[size];

// Add this code to initialize out
memset(out,'A',size-1);
out[size-1]='\0';

std::cout << strlen(out) <<std::endl;
Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181