free global char pointers

Discussion in 'C Programming' started by Tim Quon, Sep 11, 2003.

  1. Tim Quon

    Tim Quon Guest

    I have some global char pinters

    char *valuea, *valueb, ...;

    and a function to load somedata.

    void load_data() {
    valuea = "abcdefg";
    valueb = "hijklmn";
    ...
    }

    Do I need to free this memory before I end my program?

    free(valuea);
    free(valueb);
    free(...);
     
    Tim Quon, Sep 11, 2003
    #1
    1. Advertising

  2. Tim Quon

    Dan Pop Guest

    In <> Tim Quon <> writes:

    >I have some global char pinters
    >
    > char *valuea, *valueb, ...;
    >
    >and a function to load somedata.
    >
    > void load_data() {
    > valuea = "abcdefg";
    > valueb = "hijklmn";
    > ...
    > }
    >
    >Do I need to free this memory before I end my program?
    >
    > free(valuea);
    > free(valueb);
    > free(...);


    Read the chapter dealing with malloc and friends in your favourite C
    book. NEVER use library functions before getting a full understanding of
    what they're supposed to do.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Sep 11, 2003
    #2
    1. Advertising

  3. On Thu, 11 Sep 2003 14:34:56 +0200
    Tim Quon <> wrote:
    > I have some global char pinters
    >
    > char *valuea, *valueb, ...;
    >
    > and a function to load somedata.
    >
    > void load_data() {
    > valuea = "abcdefg";
    > valueb = "hijklmn";
    > ...
    > }
    >
    > Do I need to free this memory before I end my program?
    >
    > free(valuea);
    > free(valueb);
    > free(...);


    No, because you didn't dynamically allocate this memory. If you use malloc()
    somewhere, which DOES do dynamic allocation, you'll have to free it.

    --
    char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
    c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
    c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
     
    Pieter Droogendijk, Sep 11, 2003
    #3
  4. Tim Quon

    Lew Pitcher Guest

    On Thu, 11 Sep 2003 14:34:56 +0200, Tim Quon <> wrote:

    >I have some global char pinters
    >
    > char *valuea, *valueb, ...;
    >
    >and a function to load somedata.
    >
    > void load_data() {
    > valuea = "abcdefg";
    > valueb = "hijklmn";
    > ...
    > }
    >
    >Do I need to free this memory before I end my program?


    No.

    --
    Lew Pitcher
    IT Consultant, Enterprise Technology Solutions
    Toronto Dominion Bank Financial Group

    (Opinions expressed are my own, not my employers')
     
    Lew Pitcher, Sep 11, 2003
    #4
  5. Tim Quon

    Tom Zych Guest

    Tim Quon wrote:

    > char *valuea, *valueb, ...;

    [snip]
    > valuea = "abcdefg";
    > valueb = "hijklmn";

    [snip]
    > Do I need to free this memory before I end my program?


    > free(valuea);
    > free(valueb);


    If you use the pointers that way you should definitely NOT free
    them. They don't point to something you malloc'ed from the heap;
    they point to some data locations within the program. Only free
    things that you malloc (or that a library function mallocs, which
    should be in the function's docs).

    If you do use malloc, whether you have to use free or not before
    exit is platform-dependent. Some machines will free everything your
    process used when it exits, so you don't have to worry about it.
    Other machines won't, or so I hear.

    --
    Tom Zych
    This email address will expire at some point to thwart spammers.
    Permanent address: echo '' | rot13
     
    Tom Zych, Sep 11, 2003
    #5
  6. Tim Quon

    Jeff Guest

    "Tom Zych" <> wrote in message
    news:...
    > Tim Quon wrote:
    >
    > > char *valuea, *valueb, ...;

    > [snip]
    > > valuea = "abcdefg";
    > > valueb = "hijklmn";

    > [snip]
    > > Do I need to free this memory before I end my program?

    >
    > > free(valuea);
    > > free(valueb);

    >
    > If you use the pointers that way you should definitely NOT free
    > them. They don't point to something you malloc'ed from the heap;


    The standard doesn't talk about the "heap", we doesn't know how the machine
    allocate the memory when we call malloc/calloc. "heap" is just a concept on
    OS.

    --
    Jeff
    -je6543 at yahoo.com
     
    Jeff, Sep 11, 2003
    #6
    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. wwj
    Replies:
    7
    Views:
    558
  2. wwj
    Replies:
    24
    Views:
    2,520
    Mike Wahler
    Nov 7, 2003
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,060
    Ian Collins
    May 9, 2006
  4. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    308
    August Karlstrom
    Sep 24, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    681
Loading...

Share This Page