I've been trying to ignore this issue for a while now, but I've come to the point in my code where I can't do so anymore. (For those of you who are wondering, this is NOT a homework question).
Platform: VC++ 2002
System: Windows XP, sp3
::First, here's the code in question::
::My understanding of the code, i.e. What Should Happen::
The code, as I understand and have written it, should create a character array of a size (minimum 1) equal to (finish - start) and should enter a number of characters from data into the resulting array.
::Notes::
Assume that data is a character array, and that start and finish are indexes in that array. The bounds checking takes place elsewhere in the program and so is not handled here.
::Problem, i.e. What Actually Happens::
The array gets created, with nearly 15 extra slots . Not only does it have those extra elements, but they are (naturally) filled with garbage that stays with the array until it gets deleted. This does get annoying as I am trying to parse information from the lines and the garbage is corrupting the data.
::What I have done so far::
I looked in the documentation, both online and on my computer, and nothing mentioned the extra character elements.
I googled some phrases:
-->"char initialization problems"
-->"initializin g character array to wrong size"
-->"initializin g character array to wrong length"
-->"variable length char array initialization"
-->"initializin g character array C++ but incorrect number elements"
-->"initializin g character array C++"
But I didn't find anything mentioning why I would get the extra elements in the array. I've never run into anything like this before in my programming career.
I guess I have two questions. Why this is happening? How can I fix this?
Platform: VC++ 2002
System: Windows XP, sp3
::First, here's the code in question::
Code:
char * cSection (const char* data, int start, int finish) { if (start >= 0 && finish >= 0 && data != 0 && start < finish) { //allow for the null-terminator at the end of the int ssLen = finish - start + 1 ; char * sect = new char[ssLen] ; for (int i = start ; i <= finish ; i++) { sect[i-start] = data[i] ; } return sect ; } return "Failed!" ; }
::My understanding of the code, i.e. What Should Happen::
The code, as I understand and have written it, should create a character array of a size (minimum 1) equal to (finish - start) and should enter a number of characters from data into the resulting array.
::Notes::
Assume that data is a character array, and that start and finish are indexes in that array. The bounds checking takes place elsewhere in the program and so is not handled here.
::Problem, i.e. What Actually Happens::
The array gets created, with nearly 15 extra slots . Not only does it have those extra elements, but they are (naturally) filled with garbage that stays with the array until it gets deleted. This does get annoying as I am trying to parse information from the lines and the garbage is corrupting the data.
::What I have done so far::
I looked in the documentation, both online and on my computer, and nothing mentioned the extra character elements.
I googled some phrases:
-->"char initialization problems"
-->"initializin g character array to wrong size"
-->"initializin g character array to wrong length"
-->"variable length char array initialization"
-->"initializin g character array C++ but incorrect number elements"
-->"initializin g character array C++"
But I didn't find anything mentioning why I would get the extra elements in the array. I've never run into anything like this before in my programming career.
I guess I have two questions. Why this is happening? How can I fix this?
Comment