-3

Before I continue, here's the code:

#include <iostream> using namespace std; int main() { char array[] = {'a','b','c'}; cout << array << endl; return 0; } 

My system:

  • VisualStudio 2019, default C++ settings
  • Using Debug build instead of release

When I run this code sample, I get something like this in my console output:

abcXXXXXXXXX 

Those X's represent seemingly random characters. I know they're from existing values in memory at that address, but I don't understand why I'm getting 12 bytes back instead of the three from my array.

Now, I know that if I were doing this with ints which are four bytes long, maybe this would make sense but sizeof(array) returns three (ie. three bytes long, I know the sizeof(array) / sizeof(array[0] trick.) And when I do try it with ints, I'm even more confused because I get some four-byte hex number instead (maybe a memory address?)

This may be some trivial question, I'm sorry, but I'm just trying to figure out why it behaves like this. No vectors please, I'm trying to stay as non-STL as possible here.

13
  • 6
    Thou shalt null terminate character arrays when printing as strings. Commented May 31, 2019 at 1:51
  • 1
    softwareengineering.stackexchange.com/questions/181505/… Commented May 31, 2019 at 1:52
  • I know how nullbytes work, but it doesn't change the fact that there are only three bytes. If the compiler cares so much that I declare a size when creating the variable, why doesn't it juststop reading once it hits the end of that soft-buffer? Commented May 31, 2019 at 1:57
  • Try this: cout << array[0] << array[1] << array[2] << endl; Commented May 31, 2019 at 1:58
  • @Akira while it's true that there is a known size of the array, the array decays to a char* pointer when you pass it tocout, which is then expected to be a null-terminated C-style string. Also remember that C++ offers zero protection against reading an array out of bounds, and weird things can happen when you do. Commented May 31, 2019 at 2:00

1 Answer 1

4

cout takes this char array and addresses it as a null-terminated string.

Since the terminating character in this array is not the null character (i.e., char(0)), it attempts to print until encountering the null character.

At this point, it attempts to read memory outside of the array which you have allocated, and technically, anything could happen.

For example, there can be different data in that memory every time the function is called, or the memory access operation may even be illegal, depending on the address where array was allocated at the time the function was called.

So the behavior of your program is generally considered undefined (or non-deterministic).

Sign up to request clarification or add additional context in comments.

9 Comments

note that undefined is a stronger condition than non-deterministic. There can be programs that randomly produce different integer sequences for example, without being undefined.
@M.M: I believe that on a finite-state machine, there is no such thing as true random, i.e., even the behavior of rand() can be predicted if one knows the initial seed (and more generally, when one knows the current state of every transistor on the machine). So I think that non-deterministic here is in fact equivalent to what most people usually refer to as undefined.
"undefined" is a term defined by the standard to mean that the behaviour is not defined at all by the standard. True randomness could be achieved by calling a hardware true randomness generator for example. (Which you can do in C++ of course, assuming the generator is made available for user code to call)
@goodvibration finite state machines are not Turing complete, and so can't generally run C++. Did you mean Turing machine?
@M.M: Well, I guess that it's more of a theoretical argument, but my point from the previous comment remains - if the hardware has a finite number of states then the random-generator output can be predicted, hence its behavior is deterministic.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.