calloc / free

Discussion in 'C Programming' started by Oodini, Jun 27, 2003.

  1. Oodini

    Oodini Guest

    Hello,

    I create a pointer, get some memory with calloc, but when I want to free
    it, I get a run-time error.

    Here is the code:

    char *script;
    script = scalloc(71,sizeof(char));
    script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014
    setlinewidth\n0 setgray\n";
    fprintf(image_file,script);
    fclose(image_file);
    // free(script);

    image_file is defined elsewhere, and there isn't any problem with it.

    Note: I read the FAQ at
    http://www.eskimo.com/~scs/C-faq/top.html

    Thanks for help.

    And if my questions does irritate you, please don't respond...
    Oodini, Jun 27, 2003
    #1
    1. Advertising

  2. Oodini

    Morris Dovey Guest

    Oodini wrote:
    > Hello,
    >
    > I create a pointer, get some memory with calloc, but when I want to free
    > it, I get a run-time error.
    >
    > Here is the code:
    >
    > char *script;
    > script = scalloc(71,sizeof(char));
    > script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014
    > setlinewidth\n0 setgray\n";
    > fprintf(image_file,script);
    > fclose(image_file);
    > // free(script);
    >
    > image_file is defined elsewhere, and there isn't any problem with it.


    Oodini...

    Note that when you assigned a pointer to the string "/cm... " to
    script, you destroyed the pointer returned from calloc(). You
    aren't, of course, entitled to free the space occupied by that
    string literal.

    <OT> Did you really mean "0.014 cm setlinewidth"? 0.014 pt might
    as well be zero.
    </OT>

    --
    Morris Dovey
    West Des Moines, Iowa USA
    C links at http://www.iedu.com/c
    Morris Dovey, Jun 27, 2003
    #2
    1. Advertising

  3. Oodini

    Neil Cerutti Guest

    In article <>, Oodini wrote:
    > Hello,
    >
    > I create a pointer, get some memory with calloc, but when I want to free
    > it, I get a run-time error.
    >
    > Here is the code:
    >
    > char *script;
    > script = scalloc(71,sizeof(char));


    What is scalloc?

    I'm going to pretend you said malloc.

    > script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014
    > setlinewidth\n0 setgray\n";


    You have overwritten your pointer from malloc with a pointer to
    a literal string. When you pass this pointer to free, worlds
    collide.

    --
    Neil Cerutti
    Neil Cerutti, Jun 27, 2003
    #3
  4. Oodini

    Oodini Guest

    >>char *script;
    >>script = scalloc(71,sizeof(char));

    >
    > What is scalloc?


    A "safe calloc" function (it checks if calloc receives NULL).

    >>script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014
    >>setlinewidth\n0 setgray\n";

    >
    >
    > You have overwritten your pointer from malloc with a pointer to
    > a literal string. When you pass this pointer to free, worlds
    > collide.


    Actually, my goal was to put the string in the pointer.
    I didn't thought it wil use an other memory space.

    What do you suggest ?
    Should I use only a string, and wait for its destruction at the end of
    the function, or copy the string to the pojnter in an other way ?

    Thanks.
    Oodini, Jun 27, 2003
    #4
  5. Oodini

    Oodini Guest

    >> char *script;
    >> script = scalloc(71,sizeof(char));
    >> script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014


    > Note that when you assigned a pointer to the string "/cm... " to script,
    > you destroyed the pointer returned from calloc(). You aren't, of course,
    > entitled to free the space occupied by that string literal.


    That was the point. :)
    Thanks.
    Oodini, Jun 27, 2003
    #5
  6. Oodini

    Oodini Guest

    >>char *script;
    >>script = scalloc(71,sizeof(char));



    > assuming calloc(), you have initialized this pointer with the value returned
    > by calloc() which is the address of a freshly allocated block. All bits are
    > set to zero. Note that sizeof(char) is 1 by-definition. How did you specify
    > 71? Isn't it a better way of doing it? I mean automagically (strlen, sizeof
    > etc.)


    Please don't laugh: I counted them. :)
    To use strlen, I have to create a string.
    But my goal was to create a pointer to free it later.
    Not really needed (a string would be OK).
    But may I free a string ?? I don't think so...

    > The value passed to free() must be exactly the one you got from malloc() (or
    > one of its brothers). You have changed it, creating a memory leak, hence the
    > undefined behaviour.


    OK. i got it now. Thanks.
    Oodini, Jun 27, 2003
    #6
  7. Oodini

    Neil Cerutti Guest

    In article <>, Oodini wrote:
    >>>char *script;
    >>>script = scalloc(71,sizeof(char));

    >>
    >> What is scalloc?

    >
    > A "safe calloc" function (it checks if calloc receives NULL).
    >
    >>>script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014
    >>>setlinewidth\n0 setgray\n";

    >>
    >> You have overwritten your pointer from malloc with a pointer
    >> to a literal string. When you pass this pointer to free,
    >> worlds collide.

    >
    > Actually, my goal was to put the string in the pointer. I
    > didn't thought it wil use an other memory space.


    You can only store the address of things in pointers. You can't
    store anything in pointers except pointers.

    > What do you suggest ?


    Copy the string into the storage pointed to by script using
    strcpy.

    strcpy(script, "/cm {72 mul 2.54 div}...");

    --
    Neil Cerutti
    Neil Cerutti, Jun 27, 2003
    #7
  8. Oodini

    bd Guest

    On Fri, 27 Jun 2003 17:25:46 +0200, Oodini wrote:

    > Hello,
    >
    > I create a pointer, get some memory with calloc, but when I want to free
    > it, I get a run-time error.
    >
    > Here is the code:
    >
    > char *script;
    > script = scalloc(71,sizeof(char));


    What is this 'scalloc'?

    > script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014


    Strings must be on one line. I'll assume your newsreader broke this line.

    > setlinewidth\n0 setgray\n";


    You don't copy strings like that. You just lost the memory you allocated
    (literally!)

    You meant:

    strcpy(script, "...");

    Also, you need to allocate one extra character, for the terminating null.
    Here's how I'd do it:
    const char *orig = "/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014 "
    "setlinewidth\n0 setgray\n";
    char *script;
    script = malloc(strlen(orig) + 1);
    if(!script){
    fprintf(stderr, "Unable to allocate memory\n");
    exit(EXIT_FAILURE);
    }
    strcpy(script, orig);

    > fprintf(image_file,script);
    > fclose(image_file);
    > // free(script);


    Why'd you copy it? Just use:
    fprintf(image_file, "...");
    and get rid of the script variable.

    --
    Freenet distribution not available
    Teutonic:
    Not enough gin.
    bd, Jun 27, 2003
    #8
  9. Oodini

    bd Guest

    On Fri, 27 Jun 2003 19:30:34 +0200, Oodini wrote:

    >>>char *script;
    >>>script = scalloc(71,sizeof(char));

    >
    >
    >> assuming calloc(), you have initialized this pointer with the value returned
    >> by calloc() which is the address of a freshly allocated block. All bits are
    >> set to zero. Note that sizeof(char) is 1 by-definition. How did you specify
    >> 71? Isn't it a better way of doing it? I mean automagically (strlen, sizeof
    >> etc.)

    >
    > Please don't laugh: I counted them. :)
    > To use strlen, I have to create a string.


    strlen("I am a string literal. I can be used in strlen") works.
    Also,
    const char *foo = "I am a string literal.";
    strlen(foo);
    works.

    > But my goal was to create a pointer to free it later.
    > Not really needed (a string would be OK).
    > But may I free a string ?? I don't think so...


    Nope, but there's no reason to. Your memory usage increases if you
    manually allocate somewhere to put a copy - string literals are never
    freed.

    >
    >> The value passed to free() must be exactly the one you got from malloc() (or
    >> one of its brothers). You have changed it, creating a memory leak, hence the
    >> undefined behaviour.

    >
    > OK. i got it now. Thanks.


    --
    Freenet distribution not available
    There's no saint like a reformed sinner.
    bd, Jun 27, 2003
    #9
  10. Oodini

    bd Guest

    On Fri, 27 Jun 2003 19:26:29 +0200, Oodini wrote:

    >>>char *script;
    >>>script = scalloc(71,sizeof(char));

    >>
    >> What is scalloc?

    >
    > A "safe calloc" function (it checks if calloc receives NULL).
    >
    >>>script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014
    >>>setlinewidth\n0 setgray\n";

    >>
    >>
    >> You have overwritten your pointer from malloc with a pointer to
    >> a literal string. When you pass this pointer to free, worlds
    >> collide.

    >
    > Actually, my goal was to put the string in the pointer.
    > I didn't thought it wil use an other memory space.
    >
    > What do you suggest ?
    > Should I use only a string, and wait for its destruction at the end of
    > the function, or copy the string to the pojnter in an other way ?


    The correct way to copy a string is using strcpy:
    strcpy(destination, source);

    There must be room for strlen(source) + 1 chars in destination.

    --
    Freenet distribution not available
    As he had feared, his orders had been forgotten and everyone had brought
    the potato salad.
    bd, Jun 27, 2003
    #10
  11. Oodini

    Micah Cowan Guest

    Oodini <> writes:

    > Hello,
    >
    > I create a pointer, get some memory with calloc, but when I want to
    > free it, I get a run-time error.
    >
    > Here is the code:
    >
    > char *script;
    > script = scalloc(71,sizeof(char));


    is that meant to be calloc()? Also, sizeof(char) is 1, by definition.

    > script="/cm {72 mul 2.54 div} def\n1 cm 1 cm scale\n0.014
    > setlinewidth\n0 setgray\n";


    Was that line break in the code, or just wrapping from your
    newsreader? Line breaks within string literals are not permitted.

    At this point, whatever memory you allocated with calloc() is lost
    forever: you are not changing the memory allocated, but setting script
    to point at an entirely different region of memory: that occupied by
    the string literal.

    If you want to set the memory you allocated, use a standard C function
    such as strcpy(), strncpy() or memcpy() to copy the contents of the
    string literal into your newly allocated space. Something like:

    memcpy(script,THE_STRING_LITERAL,sizeof THE_STRING_LITERAL);

    where THE_STRING_LITERAL was #defined to be the literal you used.

    When doing this, though, make sure you actually allocated enough space
    to store the string literal (use sizeof THE_STRING_LITERAL again for
    this). The 71 characters you currently have allocated is too short to
    hold the all-important terminating null character ('\0').

    > fprintf(image_file,script);
    > fclose(image_file);
    > // free(script);


    an attempt to free script now will fail, because script no longer
    points at dynamically allocated memory, but at a string literal.

    > And if my questions does irritate you, please don't respond...


    Most people here aren't irritated by questions; many of us are
    irritated by failure to follow standard USENET procedure: read the
    FAQ, and lurk a while before posting.

    -Micah
    Micah Cowan, Jun 28, 2003
    #11
  12. Oodini

    Micah Cowan Guest

    "bd" <-ip.org> writes:

    > On Fri, 27 Jun 2003 19:30:34 +0200, Oodini wrote:
    >
    > > Please don't laugh: I counted them. :)
    > > To use strlen, I have to create a string.

    >
    > strlen("I am a string literal. I can be used in strlen") works.
    > Also,
    > const char *foo = "I am a string literal.";
    > strlen(foo);
    > works.


    Provided you remember to add one for the null character.

    -Micah
    Micah Cowan, Jun 28, 2003
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. luca
    Replies:
    5
    Views:
    27,012
    Default User
    Mar 2, 2004
  2. Jun Woong

    Re: Override malloc,calloc,realloc and free?

    Jun Woong, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,077
    Jun Woong
    Jun 26, 2003
  3. Dan Pop
    Replies:
    0
    Views:
    897
    Dan Pop
    Jun 26, 2003
  4. Douglas A. Gwyn

    Re: Override malloc,calloc,realloc and free?

    Douglas A. Gwyn, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    746
    Douglas A. Gwyn
    Jun 26, 2003
  5. Dan Pop
    Replies:
    2
    Views:
    2,050
    Jun Woong
    Jun 27, 2003
Loading...

Share This Page