0

Could you please explain me, how should I understand following behavior:

typedef unsigned char byte;    
byte * test = new byte[8];

Here is the value of test I can see in the Watch: ÍÍÍÍÍÍÍÍýýýý««««««««îþîþ

Why is it happening? Why I see 24 symbols, not 8?

Olga
  • 49
  • 5
  • You should accept one of the answers to close the question. If you are satisfied with the answer that is. I think your question has been answered correctly. For more information check out the FAQ http://stackoverflow.com/about – Martin Drozdik Oct 18 '13 at 13:02

6 Answers6

3

Since you allocated the array dynamically, the debugger doesn't know from the type how big the array is. Some compilers (e.g. Visual Studio) allow special expressions to interpret a pointer as an array (see e.g. View array in Visual Studio debugger?), in your case, you could try

test,8

And you should see only the 8 values of test. However, the shown values won't make more sense, since they're still uninitialized after the snippet you show.

Community
  • 1
  • 1
codeling
  • 11,056
  • 4
  • 42
  • 71
  • Thanks, that makes sense. I had a thought, that all extra characters I see just are in memory consequentially after my 8 bytes, but why then are they always the same? – Olga Oct 18 '13 at 13:23
2

I don't know what "the Watch" is (something in your debugger perhaps?), but given just a pointer it has no way of knowing how much memory you've allocated. Probably, it assumes that the pointer refers to a C-style string of printable characters followed by a zero terminator, and displays all the memory it finds until it reaches a zero-valued byte.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

Studio is treating test as a C string, an array of characters terminated by \0. It will not impact the functioning of your program, it is just the debugger trying to be smart.

Adam Burry
  • 1,904
  • 13
  • 20
1

Because your visualiser works with your array as it was array of chars. It tries to display it as a string which is supposed to end with '\0'.

So what you see is uninitialized memory.

mrate
  • 96
  • 2
1

The value of test is uninitialised and you shouldn't worry what it is.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
1

Why the unexpected amount of symbols seen after dynamic memory allocation of array?

test refers to c-string. End of c-string is marked by \0.

test isn't explicitly initialized in the program posted in the question. test has indeterminate value and so you can't expect \0 to be present exactly after the end of c-string. The extra characters that you see in your debugger are those that exist between the last character pointed to by test and the first \0 that the debugger found.


Why it is said that test has indeterminate value?

When you use new as in below statement,

unsigned char *p_dynamic_alloc = new unsigned char[8];

a pointer to unsigned char is returned, which points to the address of the first element of the array. Dynamic memory of size: 8 * sizeof (unsigned char) is allocated. Initializer is not mentioned.

Lets understand how new handles dynamic allocation without the initializer:

[C++11: §5.3.4/15]: A new-expression that creates an object of type T initializes that object as follows:

  • If the new-initializer is omitted, the object is default-initialized (8.5); if no initialization is performed, the object has indeterminate value.
  • Otherwise, the new-initializer is interpreted according to the initialization rules of 8.5 for direct-initialization.

Lets understand if it is really default initialized or if no initialization is performed.

Default initialization (of an unsigned char*) is stated as,

[C++11: §8.5/7]:To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9), the default constructor (12.1) or T is called (and the initialization is ill-formed if T has no default constructor or overload resolution (13.3) results in an ambiguity or in a function that is deleted or inaccessible from the context of the initialization);
  • if T is an array type, each element is default-initialized;
  • otherwise, no initialization is performed.

which means as pointed out earlier, the object has indeterminate value.


Solution

Properly initialize your dynamic byte array:

byte *test = new byte[8]();
smRaj
  • 1,246
  • 1
  • 9
  • 13
  • But still, when I declare a couple of such variables I always see the same characters "between the last character pointed to by test and the first \0 that the debugger found". Why? – Olga Oct 22 '13 at 11:53
  • Looks like your debugger uses the same (garbage)value for all the _uninitialized_ objects to let the programmer know the problem. Please update your question with the Compiler, Debugger and additional related details that you are using. – smRaj Oct 22 '13 at 13:43