Gustavo G. Rondina wrote:
> Hi all
>
> I'm writting a simple code to solve an ACM problem
> (http://acm.uva.es, it is the problem #46
. In its code I have the
> following fragment:
>
> freq = calcfreq(hashfreq, strfreq, input);
We don't know, of course, what any of freq, calcfreqm hashfreq, (badly
named) strfreq, or input are.
> printf("before malloc: %s (%p)\n", input+INPUTLEN);
This is nonsense. The specification string expects two more arguments
(for three in all). You give only one more. Luckily for you, we can't
tell if you forgot to cast the argument corresponding to "%p" to
(void *).
> hchars = (char *)malloc(freq*sizeof(char));
> schars = (char *)malloc(freq*sizeof(char));
At this point we know for sure you have not followed normal usenet
etiquette and lurked before posting, or checked the archives, or checked
the FAQ. You would do better with
hchars = malloc(freq);
schars = malloc(freq);
or
hchars = malloc(freq * sizeof *hchars);
schars = malloc(freq * sizeof *schars);
Of course you should #include <stdlib.h> and check the returned values
from malloc for success.
> printf("after malloc: %s\n (%p)\n", input+INPUTLEN);
This is nonsense, again.
> Since input has nothing to do with the mallocs I expected it would be
> unchanged but this isn't happening. Suppouse input points to the
> string "xxxxyy", this fragment of code outputs like this:
>
> before malloc: xxxxyy (0x8049ef0)
> after malloc: xxxx (0x8049ef0)
>
> Anyone got a clue on what could be happening here?
It appears that the array which either is 'input' or is pointed to by
'input' has a size of 4 and was initialized incorrectly, perhaps with
char input[4]="xxxx";
Somehow you have gained a '\0' in the first char _following_ input in
the process of allocating space.
All this is guessing, since you have not not posted compilable code, but
have posted something that is *not* your real code, else the output
would not be what you claim.