C
Caroline
What is MALLOC exactly?
Where is it used?
Can someone please provide me with descriptive examples?
Thank you
Where is it used?
Can someone please provide me with descriptive examples?
Thank you
Caroline said:What is MALLOC exactly?
Where is it used?
Can someone please provide me with descriptive examples?
Caroline said:What is MALLOC exactly?
Where is it used?
Can someone please provide me with descriptive examples?
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)
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.
Eric said:A function to allocate memory dynamically
The standard libray function is malloc(), not capitalised.Caroline said:What is MALLOC exactly?
Where is it used?
Can someone please provide me with descriptive examples?
In said:What is MALLOC exactly?
Where is it used?
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 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.
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.
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.
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 !-)
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.
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.