free global char pointers

T

Tim Quon

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(...);
 
D

Dan Pop

In said:
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
 
P

Pieter Droogendijk

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.
 
L

Lew Pitcher

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')
 
T

Tom Zych

Tim said:
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.
 
J

Jeff

Tom Zych said:
Tim said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top