By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,068 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,068 IT Pros & Developers. It's quick & easy.

how to determine the size of memory pointed by a int pointer?

P: n/a
hi, there,

I was asked such a question: how to determine the size of memory of
the int
pointer pointed? for example

int GetTheSizeofMemory(int *buffer)
{
int size;
//enter your code here, to calculate the memory size of buffer
point to.
return size;
}
we can not use sizeof(buffer) to get the value, how should we do?
thanks.

paul
Nov 29 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Paul wrote:
hi, there,

I was asked such a question: how to determine the size of memory of
the int
pointer pointed? for example

int GetTheSizeofMemory(int *buffer)
{
int size;
//enter your code here, to calculate the memory size of buffer
point to.
return size;
}
we can not use sizeof(buffer) to get the value, how should we do?
thanks.
By remembering the size when the buffer is allocated. There is no other
portable way.

--
Ian Collins.
Nov 29 '07 #2

P: n/a

"Paul" <pa*********@gmail.comwrote in message
news:d7**********************************@w34g2000 hsg.googlegroups.com...
hi, there,

I was asked such a question: how to determine the size of memory of
the int
pointer pointed? for example

int GetTheSizeofMemory(int *buffer)
{
int size;
//enter your code here, to calculate the memory size of buffer
point to.
return size;
}
we can not use sizeof(buffer) to get the value, how should we do?
thanks.
you know or you don't know.
within the standard, nothing is provided for this.

however, if you implement your own heap (or something similar), you can also
provide your own means of determining object sizes...

paul

Nov 29 '07 #3

P: n/a
Paul wrote:
hi, there,

I was asked such a question: how to determine the size of memory of
the int pointer pointed?
By storing the size you specified when initialising the pointer to a
block of memory.
for example

int GetTheSizeofMemory(int *buffer)
{
int size;
//enter your code here, to calculate the memory size of buffer
point to.
return size;
}
we can not use sizeof(buffer) to get the value, how should we do?
thanks.
Some platforms have special routines for this purpose but from a
Standard C point of view there is no other way than to manually
remember the size.

You can also implement a wrapper on top of *alloc() that maintains a
table of pointer values and their associated size requests and provide
a querying function to retrieve the data associated with a valid
pointer value.

Nov 29 '07 #4

P: n/a
Paul ha scritto:
I was asked such a question: how to determine the size of memory of
the int pointer pointed? for example

int GetTheSizeofMemory(int *buffer)
...
we can not use sizeof(buffer) to get the value, how should we do?
As others have already answered, you can't and one option is to write
your own malloc/free functions.

I've never really done it, but I've always thought that, in case I need
them, I would have implemented something like this:

void *mymalloc(size_t sz)
{
size_t *p;

p = malloc(sz + sizeof(size_t));
if (p) {
*p = sz;
return p + 1;
}
return NULL;
}

void myfree(void *p)
{
free(((size_t *)p)-1);
}

size_t myblocksize(void *p)
{
return ((size_t *)p)[-1];
}

I can imagine all weird things happening if pointers obtained with
malloc get passed to these functions, but avoiding to pass invalid
pointer to functions is among the first things a C programmer learns.

I understood that some malloc() implementation behaves this way, storing
the relevant information just before the pointer returned.

What I've never investigated is if there are issues with alignment,
portability or any other drawback I might have missed.

Remo.D

Nov 29 '07 #5

P: n/a
Kenneth Brody ha scritto:
"Remo D." wrote:
[... keep track of malloc'ed size ...]
>I've never really done it, but I've always thought that, in case I need
them, I would have implemented something like this:
[...]
>What I've never investigated is if there are issues with alignment,
portability or any other drawback I might have missed.

Anything that has a stricter alignment requirement than size_t
will cause this to fail. Consider a not-too-unlikely scenario
where size_t has 4-byte alignment and double has 8-byte alignment.
I see! Thanks, I'll keep it in mind should I use somthing similar.

Remo.D
Nov 29 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.