Re: sizeof(ptr) = ?



"James Kuyper Jr." <jameskuyper@xxxxxxxxxxx> 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@xxxxxxxxx> writes:

On Oct 22, 2:01 am, "MisterE" <Mist...@xxxxxxxxx> 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.



.



Relevant Pages

  • Re: Policy on rebooting?
    ... >> appropriate sizes of the pools statically was impossible. ... The problem is not the use of dynamic memory, ... so that the allocation can be ... If you can guarantee that only one pointer to a particular block is ...
    (comp.arch.embedded)
  • Re: [opensuse] Should I go 64-bit?
    ... I wouldn't say the sizes are ... Yes, they are bigger, taking up more space on disc and more memory, ... allocated space requirements that are shown by the "size" command. ...
    (SuSE)
  • Re: The usage of %p in C
    ... Richard wrote: ... two pointer types are allowed to be of different sizes ... Can you explain how,say, a "special" pointer can be ... stored in a malloc'ed block of memory which basically returns a single ...
    (comp.lang.c)
  • Re: Solaris page sizes
    ... question that bigger is better. ... allocate as much memory as possible at ... possible TLB and cache misses. ... > Certainly the studio 10 cc man page suggest that larger page sizes ...
    (comp.unix.solaris)
  • Re: The usage of %p in C
    ... two pointer types are allowed to be of different sizes ... Can you explain how,say, a "special" pointer can be ... stored in a malloc'ed block of memory which basically returns a single ... char; ...
    (comp.lang.c)