|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
Richard wrote:
> Barry Schwarz <schwarz45@yahoo.com> writes: > >> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote: >>> >I test it with char, short, int, long, float, double and struct >>> > pointers, all return 4 bytes. Does the standard say anything about >>> > sizeof a pointer? (I didn't find it in the standard...) >>> >>> > -- >>> > William >>> >>> Pointers of any type will have the same size because they hold the >>> address value of memory that they point to, thus they always hold >>> the same type of data. On standard PC's this is a 32bit address (4 >>> bytes), but it depends on the system what the size is. >> >> While this may be true in your experience, the C standard imposes no >> such restriction. Furthermore, there are systems for which it is not >> true. > > Could you explain how malloc works for such systems please. Because void * is guaranteed to be able to store values of all object pointer types. |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
On 22 Oct, 12:51, Barry Schwarz <schwar...@yahoo.com> wrote:
<snip> > (I wonder if there is enough memory in the entire > world to fully populate a system with a 64 BYTE pointer.) 64 x 8 = 512 bits a 512 bit address can address 2^512 locations 2^512 ~ 10^154 the number of atoms in the observable universe is estimated to be ~10^80 so no you couldn't populate your system if you confine yourself to the observable universe -- Nick Keighley |
|
|
|
#19 |
|
Messages: n/a
Hébergeur: |
William Xu wrote:
> I test it with char, short, int, long, float, double and struct > pointers, all return 4 bytes. Does the standard say anything about > sizeof a pointer? (I didn't find it in the standard...) (Some of these points have been made in other responses, but a few don't seem to have appeared yet.) The Standard guarantees that - Any kind of data pointer can be converted to `void*' and back without changing its value (this implies that no data pointer has more value bits than `void*') - Any function pointer can be converted to any other type of function pointer and back without changing value (hence, all function pointers have the same number of value bits) - The value 0 (the "null pointer constant") is valid for all pointer types, both data pointers and function pointers - `void*' and pointers to all three kinds of `char' have the same size and representation (whatever it is) - All pointers to all struct types have the same size and representation (whatever it is) - All pointers to all union types have the same size and representation (whatever it is) - All non-bit-field objects (hence, all pointers) occupy at least one byte each - A qualified type (`const', etc.) has the same size and representation as its unqualified cousin The Standard does *not* guarantee that all pointers have the same representation or even the same size, and does *not* guarantee that function pointers can be converted to data pointers or vice versa. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|
|
#20 |
|
Messages: n/a
Hébergeur: |
santosh wrote:
> Richard wrote: > >> Barry Schwarz <schwarz45@yahoo.com> writes: >> >>> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote: >>>> Pointers of any type will have the same size because they hold the >>>> address value of memory that they point to, thus they always hold >>>> the same type of data. On standard PC's this is a 32bit address (4 >>>> bytes), but it depends on the system what the size is. >>> While this may be true in your experience, the C standard imposes no >>> such restriction. Furthermore, there are systems for which it is not >>> true. >> Could you explain how malloc works for such systems please. > Because void * is guaranteed to be able to store values of all object > pointer types. People do seem to get very worried when they find that some of what they thought they knew is in fact false, don't they? The point here is that all the standard requires is that a) there must be a pointer representation (void *) which is capable of pointing to any category of data. malloc() returns its result in such a pointer. b) there must be a valid way of converting from void * to any other pointer type. c) malloc() must ensure that the memory it allocates is suitably aligned for any data type. Nothing requires that the same representation should be used for both the generic pointer type (void *) and all other (data) pointer types. -- Mark "And ASCII isn't required by the standard either" Bluemel |
|
|
|
#21 |
|
Messages: n/a
Hébergeur: |
santosh <santosh.k83@gmail.com> writes:
> Richard wrote: > >> Barry Schwarz <schwarz45@yahoo.com> writes: >> >>> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote: >>>> >I test it with char, short, int, long, float, double and struct >>>> > pointers, all return 4 bytes. Does the standard say anything about >>>> > sizeof a pointer? (I didn't find it in the standard...) >>>> >>>> > -- >>>> > William >>>> >>>> Pointers of any type will have the same size because they hold the >>>> address value of memory that they point to, thus they always hold >>>> the same type of data. On standard PC's this is a 32bit address (4 >>>> bytes), but it depends on the system what the size is. >>> >>> While this may be true in your experience, the C standard imposes no >>> such restriction. Furthermore, there are systems for which it is not >>> true. >> >> Could you explain how malloc works for such systems please. > > Because void * is guaranteed to be able to store values of all object > pointer types. But we are talking about values of "special memory blocks" where the pointers are different. How does malloc know which "memory type" to allocate? |
|
|
|
#22 |
|
Messages: n/a
Hébergeur: |
Richard wrote:
> santosh <santosh.k83@gmail.com> writes: > >> Richard wrote: >> >>> Barry Schwarz <schwarz45@yahoo.com> writes: >>> >>>> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote: >>>>> >I test it with char, short, int, long, float, double and struct >>>>> > pointers, all return 4 bytes. Does the standard say anything >>>>> > about sizeof a pointer? (I didn't find it in the standard...) >>>>> >>>>> > -- >>>>> > William >>>>> >>>>> Pointers of any type will have the same size because they hold the >>>>> address value of memory that they point to, thus they always hold >>>>> the same type of data. On standard PC's this is a 32bit address (4 >>>>> bytes), but it depends on the system what the size is. >>>> >>>> While this may be true in your experience, the C standard imposes >>>> no >>>> such restriction. Furthermore, there are systems for which it is >>>> not true. >>> >>> Could you explain how malloc works for such systems please. >> >> Because void * is guaranteed to be able to store values of all object >> pointer types. > > But we are talking about values of "special memory blocks" where the > pointers are different. How does malloc know which "memory type" to > allocate? It doesn't. It allocates memory suitable for all types. After the conversion to an appropriate pointer type, the memory block is accessed as an array of that type. |
|
|
|
#23 |
|
Messages: n/a
Hébergeur: |
santosh <santosh.k83@gmail.com> writes:
> Richard wrote: > >> santosh <santosh.k83@gmail.com> writes: >> >>> Richard wrote: >>> >>>> Barry Schwarz <schwarz45@yahoo.com> writes: >>>> >>>>> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote: >>>>>> >I test it with char, short, int, long, float, double and struct >>>>>> > pointers, all return 4 bytes. Does the standard say anything >>>>>> > about sizeof a pointer? (I didn't find it in the standard...) >>>>>> >>>>>> > -- >>>>>> > William >>>>>> >>>>>> Pointers of any type will have the same size because they hold the >>>>>> address value of memory that they point to, thus they always hold >>>>>> the same type of data. On standard PC's this is a 32bit address (4 >>>>>> bytes), but it depends on the system what the size is. >>>>> >>>>> While this may be true in your experience, the C standard imposes >>>>> no >>>>> such restriction. Furthermore, there are systems for which it is >>>>> not true. >>>> >>>> Could you explain how malloc works for such systems please. >>> >>> Because void * is guaranteed to be able to store values of all object >>> pointer types. >> >> But we are talking about values of "special memory blocks" where the >> pointers are different. How does malloc know which "memory type" to >> allocate? > > It doesn't. It allocates memory suitable for all types. After the > conversion to an appropriate pointer type, the memory block is accessed > as an array of that type. How can you allocate memory for "all types"? Where is this "special block of memory with extra bits for padding" case? |
|
|
|
#24 |
|
Messages: n/a
Hébergeur: |
Richard <rgrdev@gmail.com> wrote:
> santosh <santosh.k83@gmail.com> writes: > > > Richard wrote: > > > >> Barry Schwarz <schwarz45@yahoo.com> writes: > >> > >>> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote: > >>>> >I test it with char, short, int, long, float, double and struct > >>>> > pointers, all return 4 bytes. Does the standard say anything about > >>>> > sizeof a pointer? (I didn't find it in the standard...) > >>>> > >>>> Pointers of any type will have the same size because they hold the > >>>> address value of memory that they point to, thus they always hold > >>>> the same type of data. On standard PC's this is a 32bit address (4 > >>>> bytes), but it depends on the system what the size is. > >>> > >>> While this may be true in your experience, the C standard imposes no > >>> such restriction. Furthermore, there are systems for which it is not > >>> true. > >> > >> Could you explain how malloc works for such systems please. > > > > Because void * is guaranteed to be able to store values of all object > > pointer types. > > But we are talking about values of "special memory blocks" where the > pointers are different. No, we're not. We weren't the last time you spouted this nonsensical misinterpretation of C's pointer system, and we still aren't. Nor will we next time you will stubbornly refuse to learn. Richard |
|
![]() |
| Outils de la discussion | |
|
|