By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,120 Members | 1,799 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,120 IT Pros & Developers. It's quick & easy.

C++ (char *) initialization issues

P: 3
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::

Expand|Select|Wrap|Line Numbers
  1. char * cSection (const char* data, int start, int finish)
  2. {
  3. if (start >= 0 && finish >= 0 && data != 0 && start < finish)
  4.     {
  5.     //allow for the null-terminator at the end of the
  6.     int ssLen = finish - start + 1 ;
  7.  
  8.     char * sect = new char[ssLen] ;
  9.  
  10.     for (int i = start ; i <= finish ; i++)
  11.         {
  12.         sect[i-start] = data[i] ;
  13.         }
  14.     return sect ;
  15.     }
  16.     return "Failed!" ;
  17. }
  18.  

::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"
-->"initializing character array to wrong size"
-->"initializing character array to wrong length"
-->"variable length char array initialization"
-->"initializing character array C++ but incorrect number elements"
-->"initializing 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?
Nov 15 '08 #1
Share this Question
Share on Google+
5 Replies


100+
P: 687
You forgot to place terminating null in result string.
PS - you do some checks before actually copying data, but do not check whether start and finish actually belong to source string, ie do not exceed its length.
Nov 15 '08 #2

Expert 100+
P: 671
Some things that I noticed as soon as I saw the code. If you want start and finish to be non-negative, make them unsigned int, and problem solved. ssLen needs to be finish-start +1 +1. There is a +1 for the null terminator. But there is also a +1 for finish-start. Letís say finish is 3 and start is 1. Finish-start is 2. What you probably want is an array that is 1,2,3,\0, not 1,2,\0. Right? (Think of fenceposts. You want the posts, not the spaces in between.)

Now, you claim that the array gets 15 extra slots despite the code you wrote. Actually, I donít see compilable code that demonstrates the issue. As in, I plop in the code to a compiler, and when I run it, I see the obvious result. I mention this because I have no idea how you determined the size of your array. You check ssLen in a debugger right before sect and then check the size of sect also in debugger right after it is created? I certainly hope you didnít use strlen.
Nov 15 '08 #3

P: 3
Hmmm. I had read something about 'null-terminated,' but hadn't made the connection. I'll add that to my code and see what happens.

@oler1s: I actually did use strlen because I didn't want to actually count the number of characters that showed up. I first noticed the problem when I output the character sequence to the command prompt and got a bunch of garbage on the end of what I expected.

As an additional note, the IDE I use also showed the additional characters when I examined the data in the watch window.

Hopefully, however, adding the null to the end of the character string will fix the problem.

Thank you both for your insight.

-codeGhost
Nov 16 '08 #4

P: 3
Alright! Fixed!

The answer was the null-terminator at the end. It turns out that when I created the char * pointer, the program was assigning garbage from the heap?, or from somewhere, and continued assigning garbage until it ran into its own '\0.' In any case, assigning a null pointer to the end of the char * fixed the problem!

In short: If you're getting a character pointer array that is longer than you expect it to be, and the additional elements are just garbage, assign a null terminator('\0') to/beyond the end of the data you want to keep to fix the problem.

Again, thanks guys for your help!

-codeGhost
Nov 17 '08 #5

100+
P: 687
Alright! Fixed!

The answer was the null-terminator at the end. It turns out that when I created the char * pointer, the program was assigning garbage from the heap?, or from somewhere, and continued assigning garbage until it ran into its own '\0.' In any case, assigning a null pointer to the end of the char * fixed the problem!
Array allocated is 15 chars, but function you used to inspect it ( printf? ) do not know about its allocated size, it goes forward by memory until it get null char or access violation exception occurs. In your case, there was a null char somewhere near.
Nov 17 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.