Cheers guys,

After reviewing the code the general issues I was having was memory
management, and trying to access memory that had been freed. Having only
started coding in C two days ago it's probably a fair common problem.

This is the full code that I am using (I should've done this yesterday.
Didn't think to pastebin)
http://pastebin.com/tewJSX3W

Compiling and running that code through valgrind gives me no errors which
is great, however there is still 12 non-freed blocks at the end.
9 definitely lost - Am I right in saying these are getInput.in (allocated
line 25), getInputNoNewline.input (allocated line 39) and
main.IntIn(initialised line 51). And there are 3 still reachable blocks,
which i'm guessing are the three blocks that make up the players array
(allocated line 53) ?

Dave


On Tue, Feb 12, 2013 at 1:24 AM, Eliot Moss <[email protected]> wrote:

> On 2/11/2013 12:55 AM, David Hourn (DTH Design) wrote:
>
>  char *getInputNoNewline(int max, char message[]) { static char* input;
>> int maxchars = max+ADDCHARS
>> input = (char*)malloc(maxchars); input = getInput(max, message); if
>> (input[strlen(input) - 1] ==
>> '\n') { input[strlen(input) - 1] = '\0'; } return input; }
>>
>
> Just looking at this code, I see these issues:
>
> - You allocate space for input then overwrite the pointer to that space,
>   as other correspondents have noted.
> - You call strlen more than once.  Wasteful, though not *harmful*.
> - You are counting on getInput to return a string that has at least
>   one character in it; if it has no characters then you will access
>   input[-1], which is out of range (and you might potentially damage
>   some other data structure, such as a header that malloc uses to
>   track items and to handle freeing).
>
> If you expect no more than one \n in the string, then strchr may be
> what you want, or strrchr to find the last \n regardless.  If they
> return non-null, you can examine the next byte to see if it is \0
> and then clear the \n safely.
>
> Regards -- Eliot Moss
>
------------------------------------------------------------------------------
Free Next-Gen Firewall Hardware Offer
Buy your Sophos next-gen firewall before the end March 2013 
and get the hardware for free! Learn more.
http://p.sf.net/sfu/sophos-d2d-feb
_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to