about MALLOC

C

Caroline

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

Thank you
 
R

Robert Stankowic

Caroline said:
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.
 
J

Jake Roersma

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
 
A

Arthur J. O'Dwyer

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
 
J

Joona I Palaste

'\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.
 
B

Bruno Desthuilliers

Eric said:
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
 
M

Malcolm

Caroline said:
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.
 
D

Dan Pop

In said:
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
 
A

Alexandre Jasmin

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);
}
 
J

Joona I Palaste

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 ([email protected]) ------------- 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
 
A

Alex

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
 
J

Joona I Palaste

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

CBFalconer

Alex said:
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.
 
A

Alex

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
 
M

Mark McIntyre

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

Bertrand Mollinier Toublet

Joona said:
char *foo = malloc(6);
if (foo)
strcpy(foo, "Hello");
free(foo);
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);
 
J

Joona I Palaste

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

Grumble

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


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.
 

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,122
Messages
2,570,717
Members
47,283
Latest member
VonnieEwan

Latest Threads

Top