473,386 Members | 1,790 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,386 software developers and data experts.

about MALLOC

What is MALLOC exactly?
Where is it used?
Can someone please provide me with descriptive examples?

Thank you
Nov 14 '05 #1
31 3564
Caroline <pl***@letsdothatagain.com> scribbled the following:
What is MALLOC exactly?
We don't know. malloc, OTOH, is a C function.
Where is it used?
Wherever you need to dynamically allocate memory.
Can someone please provide me with descriptive examples?


char *foo = malloc(6);
if (foo)
strcpy(foo, "Hello");
free(foo);

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Insanity is to be shared."
- Tailgunner
Nov 14 '05 #2
Caroline <pl***@letsdothatagain.com> wrote:
What is MALLOC exactly?
A function to allocate memory dynamically
Where is it used?
When you cannot or do not want to allocate memory statically
Can someone please provide me with descriptive examples?


<http://www.google.com/search?q=mallo...zilla-search&s
tart=0&start=0&ie=utf-8&oe=utf-8>

--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
Nov 14 '05 #3

"Caroline" <pl***@letsdothatagain.com> schrieb im Newsbeitrag
news:da**************************@posting.google.c om...
What is MALLOC exactly?
Where is it used?
Can someone please provide me with descriptive examples?


Joona's example
(hope, you don't mind, Joona)

char *foo = malloc(6);
You request memory, enough for 6 characters..

if (foo)
If the request is granted, foo contains nof the address of the first
character of those six

strcpy(foo, "Hello");
Now you copy the characters 'H', 'e', 'l', 'l', 'o', and 0
into that memory space,
do your stuff with these characters...

free(foo);
As soon as you are done, you give that memory back.
Nov 14 '05 #4
On Tue, 09 Dec 2003 20:50:29 +0100, Robert Stankowic wrote:

Joona's example
(hope, you don't mind, Joona)

strcpy(foo, "Hello");
Now you copy the characters 'H', 'e', 'l', 'l', 'o', and 0


It should be the characters 'H', 'e', 'l', 'l', 'o' and '\0'

- Jake

Nov 14 '05 #5

On Tue, 9 Dec 2003, Jake Roersma wrote:

On Tue, 09 Dec 2003 20:50:29 +0100, Robert Stankowic wrote:

Joona's example
(hope, you don't mind, Joona)

strcpy(foo, "Hello");
Now you copy the characters 'H', 'e', 'l', 'l', 'o', and 0


It should be the characters 'H', 'e', 'l', 'l', 'o' and '\0'


'\0' and 0 are synonyms in C.

assert('\0' == 0);

I think this is true in all possible contexts in which 0 or '\0'
could be a complete token, but I would welcome counter-examples
if any exist.

-Arthur
Nov 14 '05 #6
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> scribbled the following:
On Tue, 9 Dec 2003, Jake Roersma wrote:
On Tue, 09 Dec 2003 20:50:29 +0100, Robert Stankowic wrote:
> Joona's example
> (hope, you don't mind, Joona)
>
> strcpy(foo, "Hello");
> Now you copy the characters 'H', 'e', 'l', 'l', 'o', and 0
It should be the characters 'H', 'e', 'l', 'l', 'o' and '\0'

'\0' and 0 are synonyms in C. assert('\0' == 0); I think this is true in all possible contexts in which 0 or '\0'
could be a complete token, but I would welcome counter-examples
if any exist.


Well, if 0 has not been defined... erm, wait a moment...
Seeing as neither '\0' or 0 has any alphabetic characters,
underscores or dollar signs, I think it's safe to say that the only
place one of them would not stand in for the other would be in a
comment or in a string literal.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
- Guardian in "Jinxter"
Nov 14 '05 #7

"Caroline" <pl***@letsdothatagain.com> wrote in message

What is MALLOC exactly?
Where is it used?
Can someone please provide me with descriptive examples?

The standard libray function is malloc(), not capitalised.

Imagine you are writing a payroll program. You have some employees

typedef struct
{
char name[128];
float salary;
} EMPLOYEE;

If you have ten employees you could declare an array

EMPLOYEE myemployees[10];

However in a real company you probably don't know how many employees you
will have. You might expand or contract dramatically.

To solve the problem, we do this.

EMPLOYEE *myemployees;

myemployees = malloc(N * sizeof(EMPLOYEE));

N is input from the user or read from a file, at run time. As long as the
computer has enough memory, you can effectively have an array of any number
of employees.

malloc() is at the heart of C programming. If you want to program in C, you
must learn to use it.
Nov 14 '05 #8
In <da**************************@posting.google.com > pl***@letsdothatagain.com (Caroline) writes:
What is MALLOC exactly?
Where is it used?
Can someone please provide me with descriptive examples?


This newsgroup is not supposed to be used as a substitute for reading
a C book.

Any good C tutorial book provides better explanations and more
descriptive examples than you can hope to get here.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9
>> Can someone please provide me with descriptive examples?

char *foo = malloc(6);
if (foo)
strcpy(foo, "Hello");
free(foo);


char *foo = malloc(6);
if (foo) {
strcpy(foo, "Hello");
free(foo);
}
Nov 14 '05 #10
Alexandre Jasmin <al*********@sympatico.ca> scribbled the following:
Can someone please provide me with descriptive examples? char *foo = malloc(6);
if (foo)
strcpy(foo, "Hello");
free(foo);

char *foo = malloc(6);
if (foo) {
strcpy(foo, "Hello");
free(foo);
}


Um, free(foo); is safe even if "if (foo)" fails, because then foo will
be NULL, and free(NULL) is a safe no-op.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
Nov 14 '05 #11
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Alexandre Jasmin <al*********@sympatico.ca> scribbled the following:
Can someone please provide me with descriptive examples?
char *foo = malloc(6);
if (foo)
strcpy(foo, "Hello");
free(foo);
char *foo = malloc(6);
if (foo) {
strcpy(foo, "Hello");
free(foo);
}
Um, free(foo); is safe even if "if (foo)" fails, because then foo will
be NULL, and free(NULL) is a safe no-op.


Just because 'free' gracefully handles NULL, it doesn't mean
that this "functionality" should be always exercised. If you
know whether your pointer is valid or not from the context, it
makes more sense to free it only if it is valid. If nothing
else, this provides symmetry to 'fopen' and 'fclose'.

Alex
Nov 14 '05 #12
Alex <al*******@hotmail.com> scribbled the following:
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Alexandre Jasmin <al*********@sympatico.ca> scribbled the following:
> Can someone please provide me with descriptive examples?
char *foo = malloc(6);
if (foo)
strcpy(foo, "Hello");
free(foo); char *foo = malloc(6);
if (foo) {
strcpy(foo, "Hello");
free(foo);
}
Um, free(foo); is safe even if "if (foo)" fails, because then foo will
be NULL, and free(NULL) is a safe no-op.
Just because 'free' gracefully handles NULL, it doesn't mean
that this "functionality" should be always exercised. If you
know whether your pointer is valid or not from the context, it
makes more sense to free it only if it is valid. If nothing
else, this provides symmetry to 'fopen' and 'fclose'.


There *is* something else - it saves a potential performance delay by
avoiding computing the address of free and jumping to that code, and
then immediately coming back from it. The compiler can, of course,
optimise "free(NULL);" to no code at all, but I don't think compilers
are sufficiently clever to remove the call to free() from the above
code, should the malloc() fail.
But still I chose to write the code the way I did, if nothing else, to
show that free(NULL) is valid.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To know me IS to love me."
- JIPsoft
Nov 14 '05 #13
Alex wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Alexandre Jasmin <al*********@sympatico.ca> scribbled the following:

char *foo = malloc(6);
if (foo)
strcpy(foo, "Hello");
free(foo); char *foo = malloc(6);
if (foo) {
strcpy(foo, "Hello");
free(foo);
}

Um, free(foo); is safe even if "if (foo)" fails, because then
foo will be NULL, and free(NULL) is a safe no-op.


Just because 'free' gracefully handles NULL, it doesn't mean
that this "functionality" should be always exercised. If you
know whether your pointer is valid or not from the context, it
makes more sense to free it only if it is valid. If nothing
else, this provides symmetry to 'fopen' and 'fclose'.


So what? The first was an example, and perfectly correct. There
was no reason for the second version to be posted other than the
joy of creating useless clutter. Your reasoning remains valid,
but has no application to this thread. If Alexandre Jasmin had
wished to make your point he could have done so, instead of
posting unannotated corrections to correct code for no discernable
reason.

The post should have been simply ignored.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #14
CBFalconer <cb********@yahoo.com> wrote:
Alex wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
> Alexandre Jasmin <al*********@sympatico.ca> scribbled the following:
>>> char *foo = malloc(6);
>>> if (foo)
>>> strcpy(foo, "Hello");
>>> free(foo);

>> char *foo = malloc(6);
>> if (foo) {
>> strcpy(foo, "Hello");
>> free(foo);
>> }

> Um, free(foo); is safe even if "if (foo)" fails, because then
> foo will be NULL, and free(NULL) is a safe no-op.


Just because 'free' gracefully handles NULL, it doesn't mean
that this "functionality" should be always exercised. If you
know whether your pointer is valid or not from the context, it
makes more sense to free it only if it is valid. If nothing
else, this provides symmetry to 'fopen' and 'fclose'.

So what? The first was an example, and perfectly correct. There
was no reason for the second version to be posted other than the
joy of creating useless clutter. Your reasoning remains valid,
but has no application to this thread. If Alexandre Jasmin had
wished to make your point he could have done so, instead of
posting unannotated corrections to correct code for no discernable
reason.


Fair enough.

Alex
Nov 14 '05 #15
On 10 Dec 2003 18:40:28 GMT, in comp.lang.c , Alex
<al*******@hotmail.com> wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:

Um, free(foo); is safe even if "if (foo)" fails, because then foo will
be NULL, and free(NULL) is a safe no-op.
Just because 'free' gracefully handles NULL, it doesn't mean
that this "functionality" should be always exercised.


why not? If you are guaranteed that something is done, then you can
safely rely on it.
If you
know whether your pointer is valid or not from the context, it
makes more sense to free it only if it is valid.


All you're doing is adding overhead to the code. This might be
important in time-critical programming.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #16
Joona I Palaste wrote:

char *foo = malloc(6);
if (foo)
strcpy(foo, "Hello");
free(foo);

Then wrote:

The compiler can, of course,
optimise "free(NULL);" to no code at all, but I don't think compilers
are sufficiently clever to remove the call to free() from the above
code, should the malloc() fail.


In most cases, you write calls to malloc in the hope that they will
succeed. Thus, a malloc failure is an event that will only be determined
at run time. Thus, the compiler, which only does a static analysis of
your code will NOT EVER be able to remove "calls to free() should the
malloc() fail".

There is a conceptual difference between:

void *ptr = NULL;
free(ptr);

and

void *ptr = malloc(some_integer_value);
free(ptr);

--
Bertrand Mollinier Toublet
"Reality exists" - Richard Heathfield, 1 July 2003

Nov 14 '05 #17
Eric wrote:
Caroline <pl***@letsdothatagain.com> wrote:

What is MALLOC exactly?

A function to allocate memory dynamically


I didn't find any function named "MALLOC" in my implementation (gcc...).
There is a "malloc" function, but since C is case-sensitive, it cannot
be the same !-)

Bruno

Nov 14 '05 #18
Bruno Desthuilliers <bd***********@removeme.free.fr> scribbled the following:
Eric wrote:
Caroline <pl***@letsdothatagain.com> wrote:
What is MALLOC exactly?
A function to allocate memory dynamically

I didn't find any function named "MALLOC" in my implementation (gcc...).
There is a "malloc" function, but since C is case-sensitive, it cannot
be the same !-)


I got to thinking... why are we all so smugly saying *both* "It's
malloc. not MALLOC" *and* "Read your friendly textbook" to Caroline?
What if she *knows* what malloc is, but has *really* encountered
*MALLOC* and doesn't know what it is? (Anymore than we do, because
it's not standard C.)

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To err is human. To really louse things up takes a computer."
- Anon
Nov 14 '05 #19
Mark McIntyre wrote:
Alex wrote:
Just because 'free' gracefully handles NULL, it doesn't mean
that this "functionality" should be always exercised.


why not? If you are guaranteed that something is done, then you can
safely rely on it.
If you
know whether your pointer is valid or not from the context, it
makes more sense to free it only if it is valid.


All you're doing is adding overhead to the code. This might be
important in time-critical programming.


Overhead? Could you, please, elaborate?

Joona's code will call free() every time. Alexandre's code will call
free() every time - epsilon.

Nov 14 '05 #20
nrk
Grumble wrote:
Mark McIntyre wrote:
Alex wrote:
Just because 'free' gracefully handles NULL, it doesn't mean
that this "functionality" should be always exercised.


why not? If you are guaranteed that something is done, then you can
safely rely on it.
If you
know whether your pointer is valid or not from the context, it
makes more sense to free it only if it is valid.


All you're doing is adding overhead to the code. This might be
important in time-critical programming.


Overhead? Could you, please, elaborate?

Joona's code will call free() every time. Alexandre's code will call
free() every time - epsilon.


Joona's code will not check the pointer against NULL everytime. Alexandre's
code will. Given that it is rather rare to run into situations where
malloc returns NULL, makes eminent sense to avoid the unnecessary check and
optimize the most common case.

-nrk.
Nov 14 '05 #21
nrk wrote:
Grumble wrote:
Mark McIntyre wrote:
All you're doing is adding overhead to the code. This might be
important in time-critical programming.


Overhead? Could you, please, elaborate?

Joona's code will call free() every time. Alexandre's code will call
free() every time - epsilon.


Joona's code will not check the pointer against NULL everytime. Alexandre's
code will. Given that it is rather rare to run into situations where
malloc returns NULL, makes eminent sense to avoid the unnecessary check and
optimize the most common case.


Someone snipped too much context.

Joona wrote:

char *foo = malloc(6);
if (foo)
strcpy(foo, "Hello");
free(foo);

How can you say that Joona's code will not check the pointer's value?

This is not a free(p) vs if (p) free(p) issue. The test must be done.

Nov 14 '05 #22
nrk <ra*********@deadbeef.verizon.net> scribbled the following:
Grumble wrote:
Mark McIntyre wrote:
Alex wrote:

Just because 'free' gracefully handles NULL, it doesn't mean
that this "functionality" should be always exercised.

why not? If you are guaranteed that something is done, then you can
safely rely on it.

If you
know whether your pointer is valid or not from the context, it
makes more sense to free it only if it is valid.

All you're doing is adding overhead to the code. This might be
important in time-critical programming.
Overhead? Could you, please, elaborate?

Joona's code will call free() every time. Alexandre's code will call
free() every time - epsilon.

Joona's code will not check the pointer against NULL everytime. Alexandre's
code will. Given that it is rather rare to run into situations where
malloc returns NULL, makes eminent sense to avoid the unnecessary check and
optimize the most common case.


Grumble is right, my code *will* check against NULL every time. It is
also true that there is *more* overhead in my code than in Alexandre's.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"When a man talks dirty to a woman, that's sexual harassment. When a woman talks
dirty to a man, that's 14.99 per minute + local telephone charges!"
- Ruben Stiller
Nov 14 '05 #23
In <br**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Bruno Desthuilliers <bd***********@removeme.free.fr> scribbled the following:
Eric wrote:
Caroline <pl***@letsdothatagain.com> wrote:

What is MALLOC exactly?

A function to allocate memory dynamically

I didn't find any function named "MALLOC" in my implementation (gcc...).
There is a "malloc" function, but since C is case-sensitive, it cannot
be the same !-)


I got to thinking... why are we all so smugly saying *both* "It's
malloc. not MALLOC" *and* "Read your friendly textbook" to Caroline?
What if she *knows* what malloc is, but has *really* encountered
*MALLOC* and doesn't know what it is? (Anymore than we do, because
it's not standard C.)


In this case, she would have worded her question differently.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #24
In <3f***********************@news.free.fr> Bruno Desthuilliers <bd***********@removeme.free.fr> writes:
Eric wrote:
Caroline <pl***@letsdothatagain.com> wrote:

What is MALLOC exactly?

A function to allocate memory dynamically


I didn't find any function named "MALLOC" in my implementation (gcc...).
There is a "malloc" function, but since C is case-sensitive, it cannot
be the same !-)


When it comes to external identifiers, C89 is not necessarily case
sensitive ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #25
In article <ol*****************@nwrddc01.gnilink.net>, ram_nrk2000
@deadbeef.verizon.net says...
Given that it is rather rare to run into situations where
malloc returns NULL, makes eminent sense to avoid the unnecessary
check and optimize the most common case.

-nrk.


You've got to be kidding. malloc() can indeed fail, and often,
particularly in algorithms that increase the size of the malloc
call (or realloc) exponentially for large data structures. Consider
what happens when you try to make a > 2GB malloc() request on a 32-bit
system for a moment. I've seen it happen.

Further, not all OS's support VM in a way that you always get the
memory you need from a malloc call on a heavily loaded system, even
for smaller amounts.

You *really* shouldn't be believing, or dishing out this type of
advice.

--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: po********@sco.com
Nov 14 '05 #26
Caroline wrote:
What is MALLOC exactly?
Where is it used?
Can someone please provide me with descriptive examples?


http://www.rocketaware.com/man/man3/malloc.3.htm
possibly provides a good overview.

Regards,
Robert
--
Robert Bachmann - Ro*************@rbdev.net (pgp key available)

Nov 14 '05 #27
On Thu, 11 Dec 2003 14:10:01 +0100, in comp.lang.c , Grumble
<in*****@kma.eu.org> wrote:
Mark McIntyre wrote:
All you're doing is adding overhead to the code. This might be
important in time-critical programming.


Overhead? Could you, please, elaborate?

Joona's code will call free() every time. Alexandre's code will call
free() every time - epsilon.


One of them performs an extra comparison.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #28
On Thu, 11 Dec 2003 14:42:14 +0100, in comp.lang.c , Grumble
<in*****@kma.eu.org> wrote:
Someone snipped too much context.
the context of my reply is this one:

On 10 Dec 2003 18:40:28 GMT, in comp.lang.c , Alex
<al*******@hotmail.com> wrote:Just because 'free' gracefully handles NULL, it doesn't mean
that this "functionality" should be always exercised.
which I took to mean "just because free() gracefully handles NULL,
doesn't mean you should always rely on this funcitonality".
This is not a free(p) vs if (p) free(p) issue.
that certainly wasn't clear to me.
The test must be done.


in the case of actually using foo, yup.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #29
nrk
Randy Howard wrote:
In article <ol*****************@nwrddc01.gnilink.net>, ram_nrk2000
@deadbeef.verizon.net says...
Given that it is rather rare to run into situations where
malloc returns NULL, makes eminent sense to avoid the unnecessary
check and optimize the most common case.

-nrk.


You've got to be kidding. malloc() can indeed fail, and often,
particularly in algorithms that increase the size of the malloc
call (or realloc) exponentially for large data structures. Consider
what happens when you try to make a > 2GB malloc() request on a 32-bit
system for a moment. I've seen it happen.

Further, not all OS's support VM in a way that you always get the
memory you need from a malloc call on a heavily loaded system, even
for smaller amounts.

You *really* shouldn't be believing, or dishing out this type of
advice.


Sorry, my mistake (but not the one you think I made). Too much context was
snipped out, and like the idiot that I am I didn't refer upthread to
refresh the context... I was talking about checking the pointer just before
calling free as in:
if ( ptr ) free(ptr);
which as you know is quite wasteful. Of course, this wasn't what was being
done in the code in question. I am in total agreement with your opinion on
checking malloc's return value.

-nrk.
Nov 14 '05 #30
nrk <ra*********@deadbeef.verizon.net> wrote:
Randy Howard wrote:
In article <ol*****************@nwrddc01.gnilink.net>, ram_nrk2000
@deadbeef.verizon.net says...
Given that it is rather rare to run into situations where
malloc returns NULL, makes eminent sense to avoid the unnecessary
check and optimize the most common case.

-nrk.


You've got to be kidding. malloc() can indeed fail, and often,
particularly in algorithms that increase the size of the malloc
call (or realloc) exponentially for large data structures. Consider
what happens when you try to make a > 2GB malloc() request on a 32-bit
system for a moment. I've seen it happen.

Further, not all OS's support VM in a way that you always get the
memory you need from a malloc call on a heavily loaded system, even
for smaller amounts.

You *really* shouldn't be believing, or dishing out this type of
advice.

Sorry, my mistake (but not the one you think I made). Too much context was
snipped out, and like the idiot that I am I didn't refer upthread to
refresh the context... I was talking about checking the pointer just before
calling free as in:
if ( ptr ) free(ptr);
which as you know is quite wasteful. Of course, this wasn't what was being


This is *never* necessary. It is arguably stylistically
better to free the pointer in the context in which it is
known to be valid, if such context is available. If it is
not, there is absolutely no need to introduce the check
specifically for 'free'.

Alex
Nov 14 '05 #31
In article <1C****************@nwrddc02.gnilink.net>, ram_nrk2000
@deadbeef.verizon.net says...
Sorry, my mistake (but not the one you think I made). Too much context was
snipped out, and like the idiot that I am I didn't refer upthread to
refresh the context... I was talking about checking the pointer just before
calling free as in:
if ( ptr ) free(ptr);
which as you know is quite wasteful.


Sorry, I could have read more of the context as well.

--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: po********@sco.com
Nov 14 '05 #32

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
17
by: kj | last post by:
How can one test if a pointer has been "freed" (i.e. with free())? My naive assumption was that such a pointer would equal NULL, but not so. Thanks, kj -- NOTE: In my address everything...
7
by: Yuri_Юрий | last post by:
I'm confused about the VARIABLE LENGTH ARRAYS. {scanf("%d",&n);float a;} In which compiler can I use it? I tried VC++6.0 SP6,but it's reported error:CONSTANT EXPRESSION! Another question, What...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
15
by: sethukr | last post by:
Hi everybody, While running the following program in GCC, i'm very much screwed. main() { char *ptr1; char arr; int i; char *ptr2;
21
by: softwindow | last post by:
#include "stdio.h" #include "malloc.h" struct student{ int age; char *nms; struct student *next; }; struct student *create(){ int ags=0,size=sizeof(struct student); char *nms=" ";
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.