linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Edmondson <inbox@andy.co.uk>
To: smaggon@dypatil.edu
Cc: linux-c-programming@vger.kernel.org
Subject: Re: malloc and free
Date: Mon, 20 May 2002 19:16:52 +0100	[thread overview]
Message-ID: <02052019165200.07993@localhost.localdomain> (raw)
In-Reply-To: <200205201126.QAA01696@ns1.dypatil.edu>

On Mon, 20 May 2002, Sameer Maggon wrote:
> Hi,
>   In one function i have a definiton like
>
>   char *token;
>   token = (char *)malloc(256 * sizeof(char));

No real need to cast here and in fact it could mask a missing 
#include <stdlib.h>. 

man malloc 
---< snip
RETURN VALUES
       For calloc() and malloc(), the value returned is a pointer
       to the allocated memory, which is suitably aligned for any
       kind of variable, or NULL if the request fails.
---> snip


Also, char is always size 1 so there is no need for the sizeof(). If you did 
want to use sizeof() use it with the object not the type so that if you want 
to change the type of the object you don't need to also change the malloc:

token = malloc(256 * sizeof(*token));

Also, I assume you are checking the return value of the malloc is not NULL at 
some point.

if (  ( token = malloc(256) ) == NULL  )
{
	puts("Malloc: Memory allocation failed.");
	exit(1);
}


>
>   ...
>   ...
>   token = strtok(...,"\0");

Here's the problem, you are assigning another value to token and have lost 
track of your malloc'd memory.

>
>   ...
>   free(token);

Free may now be freeing the wrong memory. 


-- 
Andrew Edmondson
Test Development Engineer
<-------------------------->
The discerning heart seeks knowledge, 
but the mouth of a fool feeds on folly.

  parent reply	other threads:[~2002-05-20 18:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-20 11:26 malloc and free Sameer Maggon
2002-05-20 10:06 ` wwp
2002-05-20 18:16 ` Andrew Edmondson [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-12-24 18:39 Ankit Jain
2004-12-24 18:52 ` Eric Bambach
2002-05-20  9:55 Alvarez Alberto-AALVARB1

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=02052019165200.07993@localhost.localdomain \
    --to=inbox@andy.co.uk \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=smaggon@dypatil.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).