Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > free global char pointers

Reply
Thread Tools

free global char pointers

 
 
Tim Quon
Guest
Posts: n/a
 
      09-11-2003
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(...);
 
Reply With Quote
 
 
 
 
Dan Pop
Guest
Posts: n/a
 
      09-11-2003
In <(E-Mail Removed)> Tim Quon <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Pieter Droogendijk
Guest
Posts: n/a
 
      09-11-2003
On Thu, 11 Sep 2003 14:34:56 +0200
Tim Quon <(E-Mail Removed)> 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#;+,)/'///*");}
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      09-11-2003
On Thu, 11 Sep 2003 14:34:56 +0200, Tim Quon <(E-Mail Removed)> 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')
 
Reply With Quote
 
Tom Zych
Guest
Posts: n/a
 
      09-11-2003
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 '(E-Mail Removed)' | rot13
 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      09-11-2003

"Tom Zych" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Pointers to char pointers in functions newbie C Programming 9 09-24-2006 10:31 AM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments