Hi, why can i dynamically allocate memory for integer array, but not string. I mean, when i compile it outputs both: 9909 and "what's up" but crashes if i include the list variable. I'm using windows 7 and a box pops up after displaying 9909 and "what's up" that myprogrm.exe has stopped working...
To elaborate, if you want an array, you need to allocate an array:
1 2 3 4 5 6 7 8 9 10 11 12 13
int* holder = newint; // only gives you 1 int
holder[0] = 777; // ok
holder[3] = 9909; // VERY BAD COULD CRASH PROGRAM OR WORSE
delete holder; // new -> delete
//...
int* foo = newint[4]; // gives you 4 ints
foo[0] = 777; // ok
foo[3] = 9909; // also ok
foo[4] = 0xbaad; // BAD
delete[] foo; // new[] -> delete[]
so I was playing it safe by assuming next chunk of memory is free right, so short of it is I should always use an array for multiple values to store at once. Ok thanks. And I guess vectors are a step up from dynamically allocated arrays (implies uses pointers) as no troubles with having to manually deallocate memory, right?
I wouldn't call that playing it safe. Your use of holder[3] could very well crash the program or even cause unpredictable bugs later on. list[1] has the same issue; it's possible that the string crashed because it internally relies on dynamic memory but was not initialized properly (but don't write anything that depends on this behavior).
Vectors are almost always better than dynamic arrays. They add a bunch of useful features like optional range checking, iterators, and copy semantics, and they automatically clean up their data, even if an exception is thrown like this: