468,766 Members | 1,340 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,766 developers. It's quick & easy.

The use of "free"

Why this error occurs??
typedef char *string;

int main(void)
{
string str;
str="ajsdklfajsdf";
printf("%s\n",str);
free(str);
printf("%s\n",str);
}
Nov 14 '05 #1
13 1443
Mars <Mars@mars> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
Mars wrote:
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

Nov 14 '05 #3
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.

--
bjrnove

Nov 14 '05 #4
ks
Mars wrote:
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.

Nov 14 '05 #5
bjrnove wrote:
malloc(strlen("ajsdklfajsdf") + 1)


Another way to write that is:

malloc(sizeof "ajsdklfajsdf")

--
pete
Nov 14 '05 #6
Mars wrote:
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.

--
Thomas
Nov 14 '05 #7
Something that calls itself Mars wrote:
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.
Nov 14 '05 #8
Mars wrote:
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]
}

Nov 14 '05 #9
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
Something that calls itself Mars wrote:
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.


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10
In article <42**********@rain.i-cable.com>, Mars <Mars@Mars> wrote:
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)

Nov 14 '05 #11
Keith Thompson <ks***@mib.org> scribbled the following:
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
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.)


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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"You could take his life and..."
- Mirja Tolsa
Nov 14 '05 #12
Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Keith Thompson <ks***@mib.org> scribbled the following:
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
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.)


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #13
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++

Nov 14 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.