Size of Allocated Memory

  • Follow


I have a function that returns a int pointer to a allocated memory.
How I know the size of this allocated memory?

Compiler = gcc


Ex.:

int main(void)
{
int *pointer;
pointer = fakefunc();

/* How I know the size of allocated memory?? */

return 0;
}
0
Reply mandark (1) 5/6/2004 3:54:42 PM

mandark_br <mandark@rockpesado.com.br> wrote:
> I have a function that returns a int pointer to a allocated memory.
> How I know the size of this allocated memory?

> int main(void)
> {
> int *pointer;
> pointer = fakefunc();

> /* How I know the size of allocated memory?? */

By simply storing it when you allocate the memory and then
returning it from fakefunc() together with the pointer.
There's no standard function that would give you that
information just from the pointer.

> Compiler = gcc

The compiler is completely irrelevant. If you're looking
for a non-portable solution you will have to check if the
libc you're using has an extension for doing that because
the libc is where malloc() and friends are defined.

                                Regards. Jens
-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.toerring.de
0
Reply Jens.Toerring (807) 5/6/2004 4:01:05 PM


mandark_br wrote:
> 
> I have a function that returns a int pointer to a allocated memory.
> How I know the size of this allocated memory?

    This is Question 7.27 in the comp.lang.c Frequently
Asked Questions (FAQ) list

	http://www.eskimo.com/~scs/C-faq/top.html

.... and you may want to look at Question 7.26 also.  You
won't like the answers, though.

-- 
Eric.Sosman@sun.com
0
Reply Eric.Sosman (4228) 5/6/2004 4:06:20 PM

mandark_br <mandark@rockpesado.com.br> wrote:
> I have a function that returns a int pointer to a allocated memory.
> How I know the size of this allocated memory?

> Compiler = gcc


> Ex.:

> int main(void)
> {
> int *pointer;
> pointer = fakefunc();

> /* How I know the size of allocated memory?? */

> return 0;
> }

You can't, and you shouldn't need to. It is your responsibility to keep
track of how much memory 'fakefunc' allocates. If you can't do that in
your context, then show us a more concrete example.

-- 
Alex Monjushko (monjushko@hotmail.com)
0
Reply monjushko (45) 5/6/2004 4:39:08 PM

3 Replies
39 Views

(page loaded in 0.085 seconds)

Similiar Articles:













7/8/2012 12:34:16 PM


Reply: