The only problem I see is that you are not initializing the contents of the
decrypted c-string. You allocate the memory calling malloc, but as the
reference says: http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/,
that memory is not initialized and values are whatever.

Try adding:
decrypted[0] = 0;

after this line:
decrypted=(char*)malloc(ORIGINAL_STRING_SIZE*4 +1);

Putting the null string terminator in the first position makes decrypted an
empty c-string, so later calls to strcat and strlen can work properly.

On a side note, you are leaking the memory assigned to "dbit", you are
calling malloc every time inside the loop and and never freeing that memory.
That's not generating the segfault here, and the ammount of memory leaked is
not so high, 5 bytes per character in the original string.

1) Free the memory at the end of the loop, after the call to strcat, add:
free(dbit);
2) Allocate the memory of dbit before entering the loop, you are using
strcpy to fill it's it doesn't need to be allocated every time.
3) There is no need for dbit at all, you can concatenate bin[move] to
decrypted directly, instead of copying the contents to dbit first and then
concatenating dbit to decrypted. You would call strcat(decrypted,
bin[move])  directly.

Hope it helps,
Carlos Guía


On Sun, Feb 27, 2011 at 2:18 PM, Shoubhik <[email protected]> wrote:

> https://ideone.com/HkUYy
>
> the above code runs fine in my compiler  ( gcc )
>
> but segfaults in the online compiler.
>
> Please help.
>
> Whats wrong with my code?
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-codejam" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-code?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en.

Reply via email to