Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| 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 15:59 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <4FE8C322.9010604@verizon.net> (permalink) |
| References | <1bac9b67-4192-4046-82ac-85df142cd6af@googlegroups.com> <4FE89746.6030309@verizon.net> <63070d51-7bf5-4d64-a4a1-7c626cb1e237@googlegroups.com> |
On 06/25/2012 03:28 PM, Myth__Buster wrote:
...
> 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?
That depends upon what the problem is that you're talking about, which I
do not yet understand. If it is a problem with the specification of
malloc() provided by the C standard, then by definition the
documentation of malloc() already mentions it, if only implicitly. If
it's a problem with a particular implementation of malloc(), I agree
with you that the implementation's documentation for malloc() should be
modified to mention it.
Could you provide an example of code that would cause problems due to
the impropriety you perceive? The above code has no problems worse than
a memory leak. malloc() allocates enough space to store a pointer to a
function type, even if that space is larger than would be needed to
store a pointer to an object type. The function pointer itself has an
object type, and malloc() returns a void* pointer that can be converted
to pointer to a function pointer of that type, which is itself a data
pointer, and therefore, on such an implementation, has a different size.
Let me provide my own example, based upon yours, which may clarify the
issue for you:
#include <stdlib.h>
int f(void) {
printf("f() will return 1.\n");
return 1;
}
int main(void)
{
int (**p)(void) = malloc(sizeof *p);
if(p)
{
printf("p() returned %d\n", (*p)());
free(p);
}
return 0;
}
Now, *p has a pointer-to-function type: int (*)(void). p itself has a
pointer-to-data type: int (**)(void). On a machine such as you describe,
sizeof p would be different from sizeof *p. If all pointers on that
machine have only one of those two sizes, then sizeof(void*), the size
of the pointer that is returned by malloc(), will certainly be the same
as sizeof p, not sizeof *p. I get the impression that you consider this
problematic. Could you provide an explanation or at least an example of
how you think this could be problematic?
Note: under modern C rules, (*p)() could also have been written as
simply p(), without change in meaning. This is a syntactic convenience,
but potentially confusing. In this context I thought I should avoid any
unnecessary potential confusion.
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