public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* why there is no memlen();
@ 2002-10-30  6:11 Arunachalam G
  0 siblings, 0 replies; 5+ messages in thread
From: Arunachalam G @ 2002-10-30  6:11 UTC (permalink / raw)
  To: gcc-help

Hi all,

is there any way to get the amount of memory allocated to a
pointer.

    ...
    char *m;

    m = (char *)malloc(sizeof(char) * 120);
    strcpy(m, "y no memlen");
    ...

in the above code strlen(m) will give  11. Is there any way to get the
amount of memory allocated to it, so that it should give 120.
and why there is no memlen() function in libc. any reason for this?

thanks
arun.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: why there is no memlen();
  2002-10-31  5:02     ` John Love-Jensen
@ 2002-10-31  5:15       ` Buddy Lott
  0 siblings, 0 replies; 5+ messages in thread
From: Buddy Lott @ 2002-10-31  5:15 UTC (permalink / raw)
  To: gcc-help

Many of the heap managers that I know about will only allocated blocks
of certain sizes or on certain boundaries (such as 8 byte boundaries).
Many also have a "minimum" allocation unit (some as high as 32 bytes).
These ideas help speed up the allocation and deallocation of memory.

Some of these managers can/will tag the beginning/end of blocks of
allocated memory with patterns. These patterns are used during
allocation/deallocation to see if the heap is corrupt.

I am not sure what the odd memory allocations (105 * 10009) are
mentioned below, but if you have a good memory debugger, you might be
able to look and see what is at those locations and tell what it is used
for.




> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]
On
> Behalf Of John Love-Jensen
> Sent: Thursday, October 31, 2002 7:59 AM
> To: Arunachalam G
> Cc: gcc-help@gcc.gnu.org
> Subject: Re: why there is no memlen();
> 
> Hi Arun,
> 
> > What is that? going 4 bytes back & reading it give the amount of
memory
> > allocated.
> 
> It's how the heap manager keeps track of the lengths of the allocated
> blocks.  Note:  not ALL heap managers use this particular bookkeeping
> trick.
> 
> > does it contains the attributes of the data type.
> 
> Hmmm, I suspect the answer is "no".
> 
> > and why it is compiler specific?
> 
> There are other ways to manage heaps.  If you were writing your own
heap
> manager, you could (for instance) put the bookkeeping information in
an
> entirely separate block of memory.
> 
> Or perhaps you have a debugging heap manager that NEVER returns memory
to
> the pool, so it doesn't need to keep track of lengths.  [Not
recommended
> for
> production code!]
> 
> > I use gcc 2.95.4 on linux in IA32. The above code gave some more
> addtional
> > length than the actually allocated. (for 100 bytes it gave 105 & for
> 1000
> > it gave 1009). Is it due to alignment?
> 
> Uncertain, but that sounds plausible.
> 
> --Eljay
> GCC 3.2 on Cygwin
> GCC 3.1 on OS X
> GCC 2.95.2 on Solaris

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: why there is no memlen();
  2002-10-31  2:52   ` Arunachalam G
@ 2002-10-31  5:02     ` John Love-Jensen
  2002-10-31  5:15       ` Buddy Lott
  0 siblings, 1 reply; 5+ messages in thread
From: John Love-Jensen @ 2002-10-31  5:02 UTC (permalink / raw)
  To: Arunachalam G; +Cc: gcc-help

Hi Arun,

> What is that? going 4 bytes back & reading it give the amount of memory
> allocated.

It's how the heap manager keeps track of the lengths of the allocated
blocks.  Note:  not ALL heap managers use this particular bookkeeping trick.

> does it contains the attributes of the data type.

Hmmm, I suspect the answer is "no".

> and why it is compiler specific?

There are other ways to manage heaps.  If you were writing your own heap
manager, you could (for instance) put the bookkeeping information in an
entirely separate block of memory.

Or perhaps you have a debugging heap manager that NEVER returns memory to
the pool, so it doesn't need to keep track of lengths.  [Not recommended for
production code!]

> I use gcc 2.95.4 on linux in IA32. The above code gave some more addtional
> length than the actually allocated. (for 100 bytes it gave 105 & for 1000
> it gave 1009). Is it due to alignment?

Uncertain, but that sounds plausible.

--Eljay
GCC 3.2 on Cygwin
GCC 3.1 on OS X
GCC 2.95.2 on Solaris

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: why there is no memlen();
  2002-10-30  6:29 ` Eljay Love-Jensen
@ 2002-10-31  2:52   ` Arunachalam G
  2002-10-31  5:02     ` John Love-Jensen
  0 siblings, 1 reply; 5+ messages in thread
From: Arunachalam G @ 2002-10-31  2:52 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: gcc-help

Hi,

>
> Depends entirely on your heap manager.
>
> For example, one platform that I worked on, you could do this:
>
> typedef unsigned char byte;
> struct MemChunk {
>    unsigned long length; // 32-bit
>    byte data[1]; // stretchy buffer
> };
>
Its very tricky way to get the allocated memory length. Never knew this.
Thanks.

What is that? going 4 bytes back & reading it give the amount of memory
allocated. does it contains the attributes of the data type. and why it is
compiler specific?

I use gcc 2.95.4 on linux in IA32. The above code gave some more addtional
length than the actually allocated. (for 100 bytes it gave 105 & for 1000
it gave 1009). Is it due to alignment?

thanks,
arun.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: why there is no memlen();
       [not found] <Pine.LNX.4.44.0210301913410.29570-100000@anubhav.lan.deepr oot.co.in>
@ 2002-10-30  6:29 ` Eljay Love-Jensen
  2002-10-31  2:52   ` Arunachalam G
  0 siblings, 1 reply; 5+ messages in thread
From: Eljay Love-Jensen @ 2002-10-30  6:29 UTC (permalink / raw)
  To: Arunachalam G, gcc-help

Hi Arun,

 >is there any way to get the amount of memory allocated to a pointer.

Depends entirely on your heap manager.

For example, one platform that I worked on, you could do this:

typedef unsigned char byte;
struct MemChunk {
   unsigned long length; // 32-bit
   byte data[1]; // stretchy buffer
};

unsigned long GetMemLength(void* p) {
   if(p == NULL) return 0;
   struct MemChunk* mem = (struct MemChunk*)((byte*)p - sizeof(unsigned long));
   return mem->length;
}


void Test() {
   void* p = malloc(1000);
   printf("%ld allocated\n", GetMemLength(p));
   free(p);
}

However, this trick is VERY platform (OS + Compiler) specific.

I'm not sure what GCC uses.  Even if something akin to the above works for 
GCC, it may be OS specific and/or version specific!  Caveat emptor!

--Eljay

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-10-31 13:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-30  6:11 why there is no memlen(); Arunachalam G
     [not found] <Pine.LNX.4.44.0210301913410.29570-100000@anubhav.lan.deepr oot.co.in>
2002-10-30  6:29 ` Eljay Love-Jensen
2002-10-31  2:52   ` Arunachalam G
2002-10-31  5:02     ` John Love-Jensen
2002-10-31  5:15       ` Buddy Lott

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).