Size of Allocated Memory

M

mandark_br

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;
}
 
J

Jens.Toerring

mandark_br said:
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
 
A

Alex Monjushko

mandark_br said:
I have a function that returns a int pointer to a allocated memory.
How I know the size of this allocated memory?
Compiler = gcc


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top