void *malloc(size_t num_bytes);

A

Alawna

Hi,
i was reading a chapter about c pointers in "c++builder complete
reference" and it is said there that malloc returns a pointer to the
start of the memory allocated but i see the prototype of it saying void
*malloc(----);
does not void mean that the function does not return anything?
thanks
 
M

Mark F. Haigh

Alawna said:
Hi,
i was reading a chapter about c pointers in "c++builder complete
reference" and it is said there that malloc returns a pointer to the
start of the memory allocated but i see the prototype of it saying void
*malloc(----);
does not void mean that the function does not return anything?
thanks

A "void *" means roughly "a pointer to something". Thus, the malloc
function returns a pointer to a memory location where you can store
something.

Review your section on pointers. Don't confuse "void" with "void *".

Mark F. Haigh
(e-mail address removed)
 
A

Alawna

so let us suppose that malloc returns an integer then its prototype
would be int void *malloc(-----); . is this accurate?
 
F

forayer

Alawna said:
so let us suppose that malloc returns an integer then its prototype
would be int void *malloc(-----); . is this accurate?

not exactly.

u see, a malloc call is as below:
<pointer var> = (pointer type cast)malloc( num bytes needed );

now, if u want to dynamically allocate an int, u wud pass da size of an
int to malloc as:
malloc( sizeof(int) );
this will allocate 2 bytes (assuming int is 2 bytes long) in some loc
in memory, n return the _address_ to that loc.
to use that allocated mem u need sumthin to ref it. so u use an 'int'
ptr. so if u hav an int *p, then
p = (int *)malloc( sizeof(int) );
will make p point to the allocated addr.

in case of allocation failure, malloc will return NULL, so chk 4 it b4
usin p, or b prepared 4 sum nasty seg faults!

cheers,
forayer
 
M

Martin Ambuhl

Alawna said:
so let us suppose that malloc returns an integer then its prototype
would be int void *malloc(-----); . is this accurate?
No, it would be (if the integer type happens to be int)
int malloc( /* whatever */);
But we know that malloc does not return an int.
 
E

Emmanuel Delahaye

Alawna wrote on 19/06/05 :
Hi,
i was reading a chapter about c pointers in "c++builder complete
reference" and it is said there that malloc returns a pointer to the
start of the memory allocated but i see the prototype of it saying void
*malloc(----);
does not void mean that the function does not return anything?
thanks

Nope. You are mixing 'void' (returns nothing) and 'void*' (returns an
address of unspecified type).

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
A

Alawna

thank you guys.
got it now. need more time to get used to c syntax. coming from pascal.
 
F

forayer

Tim said:
Mark F. Haigh said:

A "void *" means roughly "a pointer to something".

So "pointer to nothing" means roughly "a pointer to something"?!??

no, 'void *' means a pointer to any kind of thing! but it definitely
points to something, though this "something" can even be NULL!!!

Now, that's why we luv C
;-D
 
F

Flash Gordon

forayer said:
not exactly.

u see, a malloc call is as below:

Please don't use abbreviations like u for you, it makes it unnecessarily
hard to read your posts, especially for those whose native language is
not English.
<pointer var> = (pointer type cast)malloc( num bytes needed );

The cast is *not* required.
now, if u want to dynamically allocate an int, u wud pass da size of an
int to malloc as:
malloc( sizeof(int) );

A far better method is
ptr = malloc( N * sizeof *ptr );
Then you don't get the type wrong (which typically happens when
allocating space for more complex items such as pointers to pointers)
and you don't have to change the line if the type of ptr is changed.
this will allocate 2 bytes (assuming int is 2 bytes long) in some loc
in memory, n return the _address_ to that loc.
to use that allocated mem u need sumthin to ref it. so u use an 'int'
ptr. so if u hav an int *p, then
p = (int *)malloc( sizeof(int) );
will make p point to the allocated addr.

As stated above, the cast is not required. If the compiler complains
without the cast then there is something else wrong, such as using a C++
compiler or failing to include stdlib.h
in case of allocation failure, malloc will return NULL, so chk 4 it b4
usin p, or b prepared 4 sum nasty seg faults!

That sentence took me twice as long to read as it should have because of
the stupid abbreviations.

seg faults are specific to some OSs, not required by the standard. Far
worse than getting seg faults can happen, such as it appearing to work
until you show the program to you girlfriend to demonstrate how clever
you are and instead of doing what you expected it prints out a love poem
addressed to her best friend.
 
J

Joe Wright

forayer said:
Tim said:
[snip]

A "void *" means roughly "a pointer to something".

So "pointer to nothing" means roughly "a pointer to something"?!??


no, 'void *' means a pointer to any kind of thing! but it definitely
points to something, though this "something" can even be NULL!!!

Now, that's why we luv C
;-D
No. NULL is a value, not a type. NULL valued pointers point to nothing
regardless the type of the pointer.
 
F

forayer

Joe said:
No. NULL is a value, not a type. NULL valued pointers point to nothing
regardless the type of the pointer.

all i meant to say was that a 'void *' can _sometimes_ be a pointer to
nothing, but not always as Tim said it to be. :)

cheers,
forayer
 
T

Tim Rentsch

forayer said:
Tim said:
Mark F. Haigh said:
[snip]

A "void *" means roughly "a pointer to something".

So "pointer to nothing" means roughly "a pointer to something"?!??

no, 'void *' means a pointer to any kind of thing! but it definitely
points to something, though this "something" can even be NULL!!!

A NULL pointer isn't a pointer to anything.

A 'void *' is a pointer to nothing - just dereference it
to get that void value. :)

Speaking of which, why isn't

void
foo(){
void *p = ...whatever...;

return *p;
}

allowed?

It's all just humor, don't take it too seriously....
 
F

forayer

Flash said:
Please don't use abbreviations like u for you, it makes it unnecessarily
hard to read your posts, especially for those whose native language is
not English.
mate, i apologized once, and apologized to everyone in that message.
now what do you guys want me to do? say sorry for each and every post i
made in which i have used the abbreviations??? come on...
The cast is *not* required.

Yes, it is not required is C, but C++ does require it. besides the cast
cant harm anyone, unless its being incorrectly cast!
A far better method is
ptr = malloc( N * sizeof *ptr );
Then you don't get the type wrong (which typically happens when
allocating space for more complex items such as pointers to pointers)
and you don't have to change the line if the type of ptr is changed.

That does make some sense, but what if ptr is of type 'void *'?
That sentence took me twice as long to read as it should have because of
the stupid abbreviations.
well, i will just expand that sentence so that someone else reading it
doesnt have to go through as much trouble as you did (no offence
intented):
"in case of an allocation failure, malloc will return 'NULL', so check
for it before using 'p', or be prepared for some nasty segmentation
faults!"
seg faults are specific to some OSs, not required by the standard. Far
worse than getting seg faults can happen, such as it appearing to work
until you show the program to you girlfriend to demonstrate how clever
you are and instead of doing what you expected it prints out a love poem
addressed to her best friend.

I agree. seg faults are specific to some OSs. But if p=NULL, and then
you do a p->func(), no matter what the OS, it is a runtime error, dont
you think?

cheers,
forayer
 
F

forayer

Tim said:
It's all just humor, don't take it too seriously....

Hey, i meant it in good health too. so, dont take the rest seriously,
okay?
A NULL pointer isn't a pointer to anything.

A 'void *' is a pointer to nothing - just dereference it
to get that void value. :)
Huh? a pointer that doesnt point to anything, and a pointer that points
to nothing are not the same, eh? hmmm...
Speaking of which, why isn't

void
foo(){
void *p = ...whatever...;

return *p;
}

allowed?
maybe because it doesnt make any sense! how can you make use of a void
return anyway? ;-)

cheers,
forayer
 
C

Chris Torek

Flash said:
The cast [on malloc's return value] is *not* required.

Yes, it is not required is C, but C++ does require it. besides the cast
cant harm anyone, unless its being incorrectly cast!

If you are writing C++ code, you should not be using malloc() at all
(you should be using container classes and/or "new"). It is generally
unwise to compile code intended as C using a C++ compiler, as the
semantics may change unexpectedly:

#include <stdio.h>
struct S { char a[1]; };
int main(void) {
struct T { struct S { char a[1000]; } a; };
printf("sizeof(struct S) = %lu\n", (unsigned long)sizeof(struct S));
return 0;
}

(Exercise: what is the most likely output of the above, as both
C and C++? Why is it different?)

In any case, in my experience here in comp.lang.c (since it was
net.lang.c back in 1983 or so), I find that code that contains a
cast usually does so *because* it is being "incorrectly cast".
(In particular, the use of a cast is a strong hint that the programmer
has failed to "#include <stdlib.h>". This was not true in 1983,

(This is my favorite version as well.)
That does make some sense, but what if ptr is of type 'void *'?

Yes, as Lawrence Kirby noted, in that case, the call cannot follow
the pattern. Of course, if "ptr" is declared as "void *", the
appropriate size for the malloc() call has to be supplied separately:

/* like malloc, but print error message and exit on failure */
void *emalloc(size_t size) {
void *p = malloc(size);
if (p == NULL) {
panic("out of memory");
/* NOTREACHED */
}
return p;
}

There are cases where I drop the "N *" part, when I know I want
one object:

struct S *p;
...
p = malloc(sizeof *p);

Of course, 1 * x == x, for all integral x, in C.

There is another case for which I drop the "sizeof *ptr", when
"ptr" points to (plain) char:

char *dupstr(char *orig) {
char *s;
size_t len = strlen(orig) + 1; /* +1 to account for '\0' */

s = malloc(len);
if (s != NULL) /* or maybe use emalloc() */
memcpy(s, orig, len);
return s;
}

because sizeof(char) == 1 (by definition). I feel reasonably safe
in this case, because if someone decides to change "char" to some
internal "wider character" type, the strlen() call will also become
incorrect, and the dupstr() function as a whole will need rework,
not just the type of "s".
I agree. seg faults are specific to some OSs. But if p=NULL, and then
you do a p->func(), no matter what the OS, it is a runtime error, dont
you think?

This depends on how you define "runtime error". There are machines
on which this turns out to be a recursive call to main(). (In C,
main() may be called recursively, unlike C++. This is another
reason not to attempt to compile C code with a C++ compiler. :) )
 
F

forayer

Chris said:
If you are writing C++ code, you should not be using malloc() at all
(you should be using container classes and/or "new"). It is generally
unwise to compile code intended as C using a C++ compiler, as the
semantics may change unexpectedly:

#include <stdio.h>
struct S { char a[1]; };
int main(void) {
struct T { struct S { char a[1000]; } a; };
printf("sizeof(struct S) = %lu\n", (unsigned long)sizeof(struct S));
return 0;
}

(Exercise: what is the most likely output of the above, as both
C and C++? Why is it different?)

1000 using C, and 1 using C++. My money is on scope-resolution
differences between the languages. Please correct me if I am wrong.
In any case, in my experience here in comp.lang.c (since it was
net.lang.c back in 1983 or so), I find that code that contains a
cast usually does so *because* it is being "incorrectly cast".
(In particular, the use of a cast is a strong hint that the programmer
has failed to "#include <stdlib.h>". This was not true in 1983,
mind you; but at that time, <stdlib.h> did not exist either. Nor
did C++, for that matter. :) )

Neither did I, LOL ;-)
This depends on how you define "runtime error". There are machines
on which this turns out to be a recursive call to main().

I really didn't know that, thank you. :)

regards,
forayer
 
E

Emmanuel Delahaye

forayer wrote on 19/06/05 :
Yes, it is not required is C, but C++ does require it.

So what ? Tell me you are not compiling a C source with a C++
compiler...
That does make some sense, but what if ptr is of type 'void *'?

It means that the type is probably wrong.
I agree. seg faults are specific to some OSs. But if p=NULL, and then
you do a p->func(), no matter what the OS, it is a runtime error, dont
you think?

Who knows. The behaviour is undefined...

--
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++
 

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

Forum statistics

Threads
474,166
Messages
2,570,902
Members
47,442
Latest member
KevinLocki

Latest Threads

Top