Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Owen Jacobson <angrybaldguy@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Message-ID | <2012062600033947401-angrybaldguy@gmailcom> (permalink) |
| References | (3 earlier) <0.581957541fbcb1c6e540.20120625204245BST.87txxzjhze.fsf@bsb.me.uk> <189a4d68-002b-4733-951b-e33ab24a4eff@googlegroups.com> <a4s02mFaukU5@mid.individual.net> <97ef912e-0849-4003-820f-0a5f70cc1f0c@googlegroups.com> <8959beec-9a66-4763-b7a8-8f3ef79db4e2@googlegroups.com> |
| Subject | Re: Different sizes of data and function pointers on a machine -- void* return type of malloc, calloc, and realloc |
| Date | 2012-06-26 00:03 -0400 |
On 2012-06-25 20:58:17 +0000, Myth__Buster said:
>
> On Monday, June 25, 2012 1:53:10 PM UTC-7, Myth__Buster wrote:
>>
>> What would that achieve? malloc returns a void* and a void* is not> >
>> compatible with a function pointer. Would you add a note for every> >
>> function that returns something incompatible with something else?
>>
>> --
>> Ian Collins
>
> Achievement : Clarity and completeness of malloc's behavior in malloc's
> documentation. More appropriately, a word of caution similar to this -
> In C, malloc when used to directly allocate memory for function
> pointers(without any third-variable's allocation such as encompassing
> struct variable, etc) on machines wherein the data and function
> pointers are of different sizes, the return data pointer wouldn't be a
> proper pointer with which the entire memory allocated for function
> pointer could be accessed. Hence, care should be exercised before the
> allocated memory could be used. (On similar lines, the applicable
> alternative approaches can be mentioned therein to accomplish the same
> and thereby help the reader/programmer).
Given the declaration
int (*p)(void) = malloc(sizeof(int (*)(void)));
and reading it "for intent" rather than as written:
- 'p' identifies a function pointer value.
- malloc is being (mis-)used to allocate memory for a function, and is
incorrectly being expected to return a pointer to that function.
The second thing is flatly nonsensical. Nothing malloc could reasonably
do with the block of memory it allocates could make that block into a
function, and C does not provide tools for composing functions in
random blocks of memory, so there's no way subsequent code could turn
the block into a legitimate function either.
I don't know how you've gotten to "malloc is allocating a function
pointer" here. It's not: it's allocating a block of memory, then
*returning* a (value) pointer to that block. Trying to interpret the
pointed-to block as a function will at best crash your program, so the
returned pointer cannot be interpreted as a pointer to a function.
(Ignoring the case where malloc returns a null pointer.)
The only sensible values for function pointers (which 'p' already
identifies, without any need for dynamic allocation of a pointer-sized
block) are the values obtainable from the addresses of functions in
your program:
int random_number(void) {
return 4; /* generated by fair die roll */
}
int main(void) {
int (*p)(void) = random_number;
/* and so on */
return 0;
}
It's completely unnecessary to note in the docs that malloc can't
return a function pointer for the same reason that's unnecessary to
mention that memcpy can't receive a function pointer as an argument:
it's taken as given that if you use function pointers, you understand
how they're different from value pointers, and how the pointed-to
entities differ in both allocation and behaviour. Non-null function
pointers are *never* compatible with value pointers, and vice versa, as
far as I know.
If you really wanted to use malloc to "allocate a function pointer"
(for whatever unlikely reason), you'd need to add another step of
indirection, and declare 'p' as a pointer to a function pointer:
int (**p)(void) = malloc(sizeof(*p));
if (p != NULL) {
*p = random_number;
/* and so on */
}
Here, malloc allocates a block at least the size of a function pointer,
and returns a pointer *to* that block. I can't imagine why you'd write
code this way, though.
-o
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