|
|
|
#33 |
|
Messages: n/a
Hébergeur: |
wisdo@WISDO-WORK.hfwrp.webex.com writes:
> Richard <rgrdev@gmail.com> writes: > > >>>>>> 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? > > padding isn't related to malloc, it's about sizeof(). > It was an example of a case. Malloc takes no type information so clearly can only allocate from one block of memory. Since it has NO idea to which type of storage it will eventually be used it follows that any pointer to the memory therein must be compatible with any pointer at any time. |
|
|
|
#34 |
|
Messages: n/a
Hébergeur: |
santosh <santosh.k83@gmail.com> writes:
> Richard wrote: > >> rlb@hoekstra-uitgeverij.nl (Richard Bos) writes: >> >>> 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 >> >> "there are systems for which it is not true" >> >> I ask again : how does malloc know what kind of "special" memory to >> allocate? Special memory might have these special pointers. > > The point was that different pointer types might differ in size. That > doesn't mean that the memory pool from which malloc gets space has to > be different for each differing pointer types. > > It may be that the implementation, for example, decides to append extra > information with pointers to floating types, but not to other types. In > this case such pointers might conceivably be larger than others. > > Also an implementation might decide to allocate objects of a certain > type in lower address spaces than others. In such a case, pointers to > that type may be of smaller width, though the memory for the entire > gamut is the same. > > I confess all the above to be hypothetical speculations on my part. > Maybe others more experienced can actually point to implementations > with heterogeneous pointer types. Yes, As i remeber, the GNU glibc use the later one. they keeps the extra size information in the allocated memory block by malloc. look like as following.. [ extra information ] return_ptr --> [ memeory block ] so the size of return_ptr is the same. anyway, the fixed size pointer have many advantages. -Road |
|
|
|
#35 |
|
Messages: n/a
Hébergeur: |
Richard wrote:
.... > How can you allocate memory for "all types"? Where is this "special > block of memory with extra bits for padding" case? The malloc() family merely makes the space available. Padding is a type-specific feature about how the space is used, once it's been allocated. It doesn't present any problems that malloc() needs to worry about. |
|
|
|
#36 |
|
Messages: n/a
Hébergeur: |
Richard wrote:
> wisdo@WISDO-WORK.hfwrp.webex.com writes: > >> Richard <rgrdev@gmail.com> writes: >> >> >>>>>>> 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? >> >> padding isn't related to malloc, it's about sizeof(). >> > > It was an example of a case. Malloc takes no type information so > clearly can only allocate from one block of memory. Since it has NO > idea to which type of storage it will eventually be used it follows > that any pointer to the memory therein must be compatible with any > pointer at any time. No. The pointer value returned from malloc must be compatible with all other pointer types, but the other types need not be compatible with each other. The storage of course must be suitable aligned for the strictest alignment possible on the machine, but this has nothing to do with differing pointer sizes. |
|
|
|
#37 |
|
Messages: n/a
Hébergeur: |
santosh <santosh.k83@gmail.com> writes:
> Richard wrote: > >> wisdo@WISDO-WORK.hfwrp.webex.com writes: >> >>> Richard <rgrdev@gmail.com> writes: >>> >>> >>>>>>>> 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? >>> >>> padding isn't related to malloc, it's about sizeof(). >>> >> >> It was an example of a case. Malloc takes no type information so >> clearly can only allocate from one block of memory. Since it has NO >> idea to which type of storage it will eventually be used it follows >> that any pointer to the memory therein must be compatible with any >> pointer at any time. > > No. The pointer value returned from malloc must be compatible with all > other pointer types, but the other types need not be compatible with > each other. The storage of course must be suitable aligned for the > strictest alignment possible on the machine, but this has nothing to do > with differing pointer sizes. We were talking about pointers being different sizes because they are used to point to different areas in memory. Or I was. |
|
|
|
#38 |
|
Messages: n/a
Hébergeur: |
Richard wrote:
.... > We were talking about pointers being different sizes because they are > used to point to different areas in memory. Or I was. Well, everyone else was talking about pointer that might be different sizes because they are used to point to objects of different types, which is a very different thing. However, looking back on the thread, I see that this was never explicitly stated; people who knew what they were talking about just assumed that you did too. The key message was: On 2007-10-22, 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. If you knew what Barry Schwarz was talking about, this makes perfect sense, and seems like a complete answer. However, I can see how it could be misconstrued the way that you did, which means it wasn't a complete answer. The standard does require that sizeof(T*) return a value which is the size of that type. This implies that, for that type, there is a single size which is the size of any pointer of that type. It can't be different for pointers of the same type to different locations in memory. However, with exceptions that have been noted elsewhere, the standard does not require that pointers to different types have the same size. |
|
|
|
#39 |
|
Messages: n/a
Hébergeur: |
"James Kuyper Jr." <jameskuyper@verizon.net> writes:
> Richard wrote: > ... >> We were talking about pointers being different sizes because they are >> used to point to different areas in memory. Or I was. > > Well, everyone else was talking about pointer that might be different > sizes because they are used to point to objects of different types, > which is a very different thing. However, looking back on the thread, > I see that this was never explicitly stated; people who knew what they > were talking about just assumed that you did too. The key message was: > > > On 2007-10-22, 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. > > If you knew what Barry Schwarz was talking about, this makes perfect > sense, and seems like a complete answer. However, I can see how it > could be misconstrued the way that you did, which means it wasn't a > complete answer. I am going back, again, to the pointers being different sizes. If we agree that all memory from Malloc is to a single "type of memory". It returns a pointer to "thingies". Lets say this "pointer" must fit in a void * (which it must). thingies *p = malloc(2048);// (for example) at what point can p become bigger or smaller than p2 here: char *p2=malloc(2048); Would you agree it can not become smaller since that would imply losing some address information required for the subsequent call to free()? If it became bigger than the char * above, I guess the cast to put it (implicit or explicit) into a void * for a call to free then takes care of converting it again to something that free understands? But if free then understands it - what information from its "bigger implementation" has been lost and how does that get "replaced" when we convert the void * back to a pointer to thingies? Maybe I have a head tumor on this thing, but I'm constantly puzzled by how frequently people tell us that pointers to blocks of memory returned by malloc can be different sizes. |
|
|
|
#40 |
|
Messages: n/a
Hébergeur: |
Richard wrote On 10/22/07 22:06,:
> [...] > It was an example of a case. Malloc takes no type information so clearly > can only allocate from one block of memory. Since it has NO idea to > which type of storage it will eventually be used it follows that any > pointer to the memory therein must be compatible with any pointer at any > time. Not at all. char *p = malloc(1 + sizeof(double)); /* assume success */ char *q = p + 1; /* "pointer to the memory therein" */ double *dp = (double*)q; /* potential U.B. */ A pointer to the *start* of the allocated area (not "any pointer to the memory therein") must be *convertible to* (not "compatible with") a valid value of any data pointer type. -- Eric.Sosman@sun.com |
|
![]() |
| Outils de la discussion | |
|
|