473,394 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1647
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Darren Dale | last post by:
I love the language. I love the community. My only complaint is that Python for Windows is built with Visual Studio. It is too difficult to build python, or a module, from source. This is what...
5
by: Michael | last post by:
How can I determine a users PC Total system memory and memory available or free at the time of my program loading. Thanks
5
by: prashantkhoje | last post by:
hi ppl, i am developing an C application in Microsoft visual C++ 6.0. i get following error while running it in debug mode: /* here's the error message */ program: my_application.exe...
16
by: Stef Mientki | last post by:
If I create a large array of data or class, how do I destroy it (when not needed anymore) ? Assign it to an empty list ? thanks, Stef Mientki
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.