[prog] Re: learning c

Daniel. cristofd at hevanet.com
Mon Mar 29 04:39:19 EST 2004


>If anybody has time to take another look at it and
>make any additional comments, that would be great. But
>if you're all to busy, that's okay. I understand.
></alice>

Few little things.
-At least one of the off-by-one errors still seems to be there. Say 
you're reversing the string "cat". strlen("cat") will return 3. 
Great. Then 3 gets passed as the "size" parameter to STRreverse. Also 
great. Then STRreverse uses malloc to get 3 bytes. Not so great. You 
need FOUR bytes--'t', 'a', 'c', '\0'. You should malloc(size+1).

-Since you're trying to avoid getting bad habits, I will remark that 
it's a little weird to put a main program in a header file...maybe a 
separate file for the test programs? ("STRtest.c" maybe?)

-And in a similar vein, it's perhaps not ideal for utility functions 
to be interacting directly with the user. You already have STRreverse 
returning a recognizable error code (0) on failure, and you already 
have main telling the user what went wrong, based on that code; 
that's the right organization. STRreverse should not talk to the user 
itself.

-If the plan is to use realloc to get a big enough buffer to hold the 
entire string, why not use realloc each time the buffer fills, 
instead of malloc with extra bookkeeping? Does it actually end up 
being much faster?

Good luck;
-Daniel.
-- 
        ()  ASCII ribbon campaign      ()    Hopeless ribbon campaign
        /\    against HTML mail        /\  against gratuitous bloodshed


More information about the Programming mailing list