Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc |
| Date | 2012-06-25 14:08 -0700 |
| Organization | None to speak of |
| Message-ID | <ln1ul36qx0.fsf@nuthaus.mib.org> (permalink) |
| References | <1bac9b67-4192-4046-82ac-85df142cd6af@googlegroups.com> <4FE89746.6030309@verizon.net> <63070d51-7bf5-4d64-a4a1-7c626cb1e237@googlegroups.com> |
Myth__Buster <raghavanil4m@gmail.com> writes:
[...]
> Well, am aware of the possible implications of converting a void* into
> a function pointer. But, the initialization of function pointer p is
> present only for the completeness of the code. For the question being
> discussed, let's consider the code as under :
>
> #include <stdlib.h>
> int main(void)
> {
> (void)malloc(sizeof(int (*)(void)));
> // Ignore the usability of this statement, possible
> // memory-leaks, and any other trivial factors involved
> // which are irrelevant to the question being discussed.
> return 0;
> }
>
> Now, would like to reiterate myself - malloc() returns a void*(data
> pointer as always known) even when we have allocated memory for a
> function pointer on a machine wherein data and function pointers are
> of different sizes. This behavior of malloc appears to me as improper
> and hence I am questioning its generality of returning a void*(data
> pointer) always. Shouldn't there be a note in malloc's documentation
> regarding such usage?
The expression
sizeof(int (*)(void))
yields a value of type size_t. The particular value is likely to be 4
or 8 on many typical systems.
malloc() only sees this value as its parameter; it doesn't know or care
that it happens to be the size of a function pointer. So you might as
well have written:
(void)malloc(4);
or
(void)malloc(8);
malloc will attempt to allocate that many bytes of memory, and will
return a void* value that points to it (or a null pointer if the
allocation fails).
If you like, you can then use that allocated memory as an object of
pointer-to-function type. There's nothing problematic about that. A
function pointer is just another data type, like an object pointer, an
integer, a floating-point object, or a struct object.
But if you want to use malloc() to allocate an object of some type, you
need to convert the result to a pointer to that type. Your original
code didn't do that correctly; it attempted to convert the result of
malloc() to a pointer-to-function type rather than to a
pointer-to-pointer-to-function type.
A typedef might make this a bit clearer:
typedef void (*func_ptr)(void);
...
func_ptr *p = malloc(sizeof *p);
p is a pointer to a pointer to function.
Another thing that you might *think* you can do is to set a function
pointer to point to space allocated by malloc():
func_ptr wrong = malloc(some_size);
For this to work, some_size would probably have to indicate the size of
the function. (That's assuming function pointers are implemented as
pointers to the function's code, which is not specified; a function
pointer might instead point to some kind of descriptor, for
example). You'd also need some way to initialize the newly allocated
memory so it acts like a function. This can be done on some systems, but
it's *extremely* system-specific.
What I *think* you're trying to do is allocate a single
pointer-to-function object using malloc() (that's the closest thing to
the code you showed us that makes any sense). But it's not clear *why*
you're doing this. If you're only allocating space for a single pointer
object, why not just declare the object directly?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Myth__Buster <raghavanil4m@gmail.com> - 2012-06-25 09:08 -0700
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-06-25 09:16 -0700
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Myth__Buster <raghavanil4m@gmail.com> - 2012-06-25 12:08 -0700
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc James Kuyper <jameskuyper@verizon.net> - 2012-06-25 15:27 -0400
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-25 15:30 -0400
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Myth__Buster <raghavanil4m@gmail.com> - 2012-06-25 12:47 -0700
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-25 16:10 -0400
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-25 20:42 +0100
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Myth__Buster <raghavanil4m@gmail.com> - 2012-06-25 13:14 -0700
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Ian Collins <ian-news@hotmail.com> - 2012-06-26 08:30 +1200
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Myth__Buster <raghavanil4m@gmail.com> - 2012-06-25 13:53 -0700
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Ian Collins <ian-news@hotmail.com> - 2012-06-26 09:00 +1200
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc James Kuyper <jameskuyper@verizon.net> - 2012-06-25 17:13 -0400
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Myth__Buster <raghavanil4m@gmail.com> - 2012-06-25 13:58 -0700
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Owen Jacobson <angrybaldguy@gmail.com> - 2012-06-26 00:03 -0400
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc James Kuyper <jameskuyper@verizon.net> - 2012-06-25 16:47 -0400
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc gordonb.vk4xa@burditt.org (Gordon Burditt) - 2012-06-25 16:06 -0500
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Keith Thompson <kst-u@mib.org> - 2012-06-25 14:17 -0700
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-25 17:24 -0400
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc James Kuyper <jameskuyper@verizon.net> - 2012-06-25 12:52 -0400
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Myth__Buster <raghavanil4m@gmail.com> - 2012-06-25 12:28 -0700
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc James Kuyper <jameskuyper@verizon.net> - 2012-06-25 15:59 -0400
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Keith Thompson <kst-u@mib.org> - 2012-06-25 14:08 -0700
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Nobody <nobody@nowhere.com> - 2012-06-26 21:29 +0100
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Keith Thompson <kst-u@mib.org> - 2012-06-25 13:13 -0700
Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc Paul N <gw7rib@aol.com> - 2012-06-25 13:17 -0700
csiph-web