tags:

views:

193

answers:

6

I am passing an empty char array that I need to recursively fill using strcat(). However, in the VS debugger, the array is not empty, it's full of some weird junk characters that I don't recognise. strcat() then appends to the end of these junk characters rather than at the front of the array.

I have also tried encoded[0] = '\0' to clear the junk before passing the array, but then strcat() doesn't append anything on the recursive call.

This is the code that supplies the array and calls the recursive function:

char encoded[512];
text_to_binary("Some text", encoded);

This is the recursive function:

void text_to_binary(const char* str, char* encoded)
{   
    char bintemp[9];
    bintemp[0] = '\0';

    while(*str != '\0')
    {
        ascii_to_binary(*str, bintemp);
        strcat(encoded, bintemp);
        str++;
        text_to_binary(str, encoded);
    }
}

What is going on?

ps. I can't use std::string - I am stuck with the char*.

Edit: This is the junk character in the array: ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ...

+6  A: 

You are not initialising the array. Change:

char encoded[512];

to

char encoded[512] = "";
anon
+3  A: 

strcat appends to the end of the string, the end is marked by a \0, it then appends a \0 to the new end position.

You should clear the destination encoded with either encoded[0]=0; or memset first.

Martin Beckett
+2  A: 

char encoded[512];.. encoded is not initialized and will contain junk (or 0xCCCCCCCC in debug builds).

Andreas Bonini
`0xcd` is used by the debug heap. `0xcc` is used for uninitialized variables on the stack.
James McNellis
All very compiler specific. If you mention this detail mention the compiler and OS at the same time.
Martin York
My apologies. Andreas's answer and my comment are both specific to the Microsoft Visual C++ debug runtime (at least since Visual Studio 2005).
James McNellis
The answer clearly states in the title that it's about VS2008; I don't believe it's necessary to state it in the answer too.
Andreas Bonini
+1  A: 

Your problem was due to encode initialization I think. A few comment on your program:

it's better to avoid recursive function when you can do it with a loop.

Second you should add the size of encoded to avoid possible overflow error (in the case the size of string is bigger than encoded).

void text_to_binary(const char* str, char* encoded)
{   
    char bintemp[9];
    bintemp[0] = '\0';
    encode[0] = '\0';

    for(const char *i = str; i!='\0'; i++)
    {
        ascii_to_binary(*i, bintemp);
        strcat(encoded, bintemp);
    }
}

PS: i didn't tried the source code, so if there is an error add a comment and I will correct it.

Good contination on your project.

Phong
yeah, thanks, i do know all of this, it's just a trivial example
tgh
+1  A: 

The solution to your immediate problem has been posted already, but your text_to_binary is still inefficient. You are essentially calling strcat in a loop with always the same string to concatenate to, and strcat needs to iterate through the string to find its end. This makes your algorithm quadratic. What you should do is to keep track of the end of encoded on your own and put the content of bintemp directly there. A better way to write the loop would be

while(*str != '\0')
{
    ascii_to_binary(*str, bintemp);
    strcpy(encoded, bintemp);
    encoded += strlen(bintemp);
    str++;
}

You don't need the recursion because you are already looping over str (I believe this to be correct, as your original code will fill encoded pretty weirdly). Also, in the modified version, encoded is always pointing to the end of the original encoded string, so you can just use strcpy instead of strcat.

jk
A: 
  1. You didn't attached source of ascii_to_binary, let's assume that it will fill buffer with hex dump of the char (if this is the case it's easier to use sprintf(encoded+(i2),"%2x",*(str+i));
  2. What's the point of recursively calling text_to_binary? I think this might be a problem.
XAder