tags:

views:

62

answers:

2

I put C++ because I'm just starting in C# and I'm not sure if there's a difference.

if you declare an array

char arr[10] 

and fill in values for arr[0] through arr[8], what value will be put in arr[9]?

a space ' '? An endline '\n'? '\0'? Or is it nothing at all?

I'm asking this because I've always used tactics like this

char word[20];
for(count = 0 ; count < 20 ; count++)
{
  cout << word[count];
}

to print the entire contents of an array, and I was wondering if I could simplify it (e.g., if the last entry was '\0') by using something like this

char word[20];
while(word[count] != '\0')
{
  cout << word[count];
}

that way, I wouldn't have to remember how many pieces of data were entered into an array if all the spaces weren't filled up.

If you know an even faster way, let me know. I tend to make a bunch of mistakes on arrays.

+2  A: 

In C++ an array declared like char word[20] will essentially have garbage at every entry. It does not have nice default values like C# but instead likely just has whatever previously occupied that particular place in memory. In short don't trust the value unless you've explicitly set it yourself.

JaredPar
+1, Although some third party memory management libraries will overwrite the heap they manage with a well-known (to them) pattern in order to detect buffer overruns, new/delete problems, etc.
Eric J.
This is true, but not very helpful and doesn't answer the question at all.
Amir Rachum
@jwaffe: Suppose the array is allocated on the stack. The "garbage" that is there is the contents of whatever was previously using the stack at that address: local variables, return addresses, whatever. Suppose the array is allocated on the heap. The garbage that is there is whatever was previously using the heap at that address: free list pointers, block sizes, old data, and so on. Because languages like C++ make no guarantees about the contents of memory, they save on the expense of having to initialize it. The cost in the bugs and vulns this strategy causes is passed on to you.
Eric Lippert
+5  A: 

The C# syntax for constructing a character array is:

char[] arr = new char[10];

All values in the array will be initialized to '\0'.

Perhaps a List<char> would be better for your situation where you don't know how many characters you need. Another option to consider is a StringBuilder. Or use a string if you don't need mutability.

Mark Byers
+1 for suggesting List.
Amir Rachum
ok. List<char>, I haven't heard of that. Is that C# or C++?thanks!
jwaffe
@jwaffe: C#. The C++ equivalent of `List` is `std::vector`. You should really decide which language you wish to program in. It is very unclear from your question whether you want an answer for C++ or C#.
Mark Byers
ok. Sorry, I'm just starting in C#, I don't know enough to really use it for everything, so I still use C++ for console apps. I was trying to fix a bad habit while I was figuring out how C# handles arrays for the first time.
jwaffe
@jwaffe: Next time I suggest you make two separate questions. :)
Mark Byers