free() an empty string ?

C

c.arnet

Hi there,

I'm checking out the Valgrind debugging tool. It reports an 'invalid
free' error on a code chunk like this:

int main(int argc, char *argv[])
{
char *cpStr;

cpStr = "";
free(cpStr);
}

What's exactly going on in main memory here?

Thanks,
Cornel
 
P

Pieter Droogendijk

On 15 Sep 2003 06:52:46 -0700
Hi there,

I'm checking out the Valgrind debugging tool. It reports an 'invalid
free' error on a code chunk like this:

int main(int argc, char *argv[])
{
char *cpStr;

cpStr = "";
free(cpStr);
}

You're free()ing a string literal. That invokes nasal demons.
 
H

Hallvard B Furuseth

cpStr = "";
free(cpStr);

Free() should only be used to free memory allocated with malloc(),
realloc(), or calloc(). Or a function which uses them of course, like
the common strdup() function.

malloc() & co are for the _programmer_ to allocate memory at
run-time, so the programmer must clean up afterwards with free().

Other kinds of memory (variables, string literals like "" or "foo")
are managed implicitly by the run-time system.
 
A

Andreas Kahari

I'm checking out the Valgrind debugging tool. It reports an 'invalid
free' error on a code chunk like this:

int main(int argc, char *argv[])
{
char *cpStr;

cpStr = "";
free(cpStr);
}

What's exactly going on in main memory here?

Noone can say what's going on in main memory, but you're
freeing memory that was never allocated.

Don't do that.

Every call to malloc() should be accompanied by a call to free()
somewhere else in the code, and vice versa. I.e., free what you
allocate, no more, no less.
 
R

Richard Bos

I'm checking out the Valgrind debugging tool. It reports an 'invalid
free' error on a code chunk like this:

int main(int argc, char *argv[])
{
char *cpStr;

cpStr = "";
free(cpStr);
}

Well, and so it should. You're not allocating any memory for cpStr. You
point it at a string literal, but you can't free string literals. You
can only call free() with a pointer to memory you have previously
received from malloc(), calloc() or realloc().

Richard
 
J

Jake Roersma

int main(int argc, char *argv[])
{
char *cpStr;

cpStr = ""; Here is your problem.
free(cpStr);

return 0;

Make sure you return an integer for main.
}

What's exactly going on in main memory here?

No one knows what is going into to memory. Safe guess would be nothing at
all because your pointer points to unallocated memory. You need to
allocate some memory by using malloc.

free() frees the memory space pointed to by ptr, which must have been
returned by a previous call to malloc(), calloc() or realloc(). Other-
wise, or if free(ptr) has already been called before, undefined
behaviour occurs. If ptr is NULL, no operation is performed.

- Jake
 
M

Mark McIntyre

No one knows what is going into to memory. Safe guess would be nothing at
all because your pointer points to unallocated memory.

er, no it doesn't - he pointed it at a string literal containing one
char, the null character.
You need to allocate some memory by using malloc.

this is certainly true, if you plan to free() it.
 

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
474,266
Messages
2,571,072
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top