The use of "free"

M

Mars

Why this error occurs??


typedef char *string;

int main(void)
{
string str;
str="ajsdklfajsdf";
printf("%s\n",str);
free(str);
printf("%s\n",str);
}
 
U

Ulrich Eckhardt

Mars said:
typedef char *string;

int main(void)
{
string str;
str="ajsdklfajsdf";

Bad idea, as you still can't modify the memory 'str' points to.
printf("%s\n",str);
free(str);

You can only use free() with pointers reveived from previous calls to
malloc() or related functions.

Uli
 
J

Jens.Toerring

Mars said:
Why this error occurs??

Which error?
typedef char *string;

I hope you realize that a char pointer isn't a string...
int main(void)
{
string str;
str="ajsdklfajsdf";
printf("%s\n",str);
free(str);

'str' isn't a pointer to memory you got from malloc(), calloc() or
realloc(). And in that case you're not allowed to call free() on
that pointer.
printf("%s\n",str);
}

You're missing a return statement here, at least for a C89 compiler.
main() returns an int.
Regards, Jens
 
B

bjrnove

Because you didn't allocate "ajsdklfajsdf". You would have to do
something like this to make it be freeable:

int main(void)
{
string str;

/* Allocate memory for the string */
if(!(str = malloc(strlen("ajsdklfajsdf") + 1))){
return -1;
}

/* Put the string into the allocated memory */
strcpy(str, "ajsdklfajsdf");

/* Free the allocated memory */
free(str);
/* It's always smart to set the freed pointer to NULL */
str = NULL;
}

Basicly you wouldn't have to worry about free at all in the example you
wrote.
 
K

ks

Mars said:
Why this error occurs??


typedef char *string;

int main(void)
{
string str;
str="ajsdklfajsdf";
printf("%s\n",str);
free(str);
printf("%s\n",str);
}

Well, you haven't specified what the error is :).

Seems to me as though you've missed out a malloc - freeing memory that
hasn't
been malloced would definitely give you trouble.

HTH,
K.
 
T

Thomas Matthews

Mars said:
Why this error occurs??


typedef char *string;

int main(void)
{
string str;
Very important:
** You have declared a pointer
** but you have not allocated
** any room for the characters.

str="ajsdklfajsdf";
Misnomer:
** This line assigns pointers
** BUT NOT CONTENT. DO NOT
** GET IN THE HABIT OF USING
** ASSIGNMENT FOR CHAR ARRAYS.
** That is what the str*()
** methods are for.

printf("%s\n",str);
free(str);
printf("%s\n",str);
}

Don't use pointers after you have
freed the memory they point to.
That is bad karma. The memory
could be given to another program
running concurrently with yours.
Only free memory when you are finished
with it.

See also: Reference Counting.

BTW, do not label "char *" as a
string. A string is a data structure
{which may have content}. The
"char *" is a pointer; no memory
allocated, just a pointer.
 
E

E. Robert Tisdale

Something said:
Why this error occurs??


typedef char *string;

int main(void)
{
string str;
str="ajsdklfajsdf";
printf("%s\n",str);
free(str);
printf("%s\n",str);
}

This is an obvious troll.
Please ignore it.
 
M

Martin Ambuhl

Mars said:
Why this error occurs??

Which of your multiple errors did you have in mind?
typedef char *string;

int main(void)
{
string str;
str="ajsdklfajsdf";
printf("%s\n",str);
free(str);

You didn't dynamically allocate the space for the anonymous string str
points to. free() all space that you dynamically allocate, none that
you do not. An attempt to free() that anonymous string -- which might
well be in a write-protected area of memory -- is an error.
printf("%s\n",str);

And never try to use a pointer value of a pointer that you have freed.
That makes no sense at all.

[Unless you have a C99 compiler, which is doubtful, you need a
'return 0;' or its equivalent here]
 
K

Keith Thompson

E. Robert Tisdale said:
This is an obvious troll.
Please ignore it.

How so? It demonstrates ignorance about C memory allocation, but it's
not obviously stupid, and the answer is straightforward, not likely to
result in a long contentious discussion. Several people have provided
good answers; that should be the end of it. If "Mars" is a troll,
he's a lousy one.

(Unless "This" refers to your own article, but somehow I doubt that
that's what you meant.)
 
K

Kenny McCormack

Why this error occurs??


typedef char *string;

int main(void)
{
string str;
str="ajsdklfajsdf";
printf("%s\n",str);
free(str);
printf("%s\n",str);
}

Because you typed it in. (And/or tried to compile and/or run it)
 
J

Joona I Palaste

How so? It demonstrates ignorance about C memory allocation, but it's
not obviously stupid, and the answer is straightforward, not likely to
result in a long contentious discussion. Several people have provided
good answers; that should be the end of it. If "Mars" is a troll,
he's a lousy one.
(Unless "This" refers to your own article, but somehow I doubt that
that's what you meant.)

You should know that "This is an obvious troll" is ERT's standard
reply to any article that includes a real, on-topic C language question,
or an answer to one.
 
K

Keith Thompson

Joona I Palaste said:
You should know that "This is an obvious troll" is ERT's standard
reply to any article that includes a real, on-topic C language question,
or an answer to one.

Alas, he's not that consistent.
 
E

Emmanuel Delahaye

Mars wrote on 11/03/05 :
Why this error occurs??

typedef char *string;

int main(void)
{
string str;
str="ajsdklfajsdf";
printf("%s\n",str);
free(str);
printf("%s\n",str);
}

free() only works with [m|c|re]alloc()ated blocks.

Additionally, using a freed block invokes an indefined behaviour, and a
prototype for printf() is mandatory (include <stdio.h>).

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 

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,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top