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

P

Paul

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
 
I

Ian Collins

Paul said:
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.
 
C

cr88192

Paul said:
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...
 
S

santosh

Paul said:
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.
 
R

Remo D.

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
 
K

Kenneth Brody

:
[... 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:

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

p = malloc(sz + sizeof(size_t));
if (p) {
*p = sz;
return p + 1;
}
return NULL;
} [...]
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.
The following will do "bad things"[tm]:

double *mypt = mymalloc(10 * sizeof(*mypt));
if ( mypt != NULL )
mypt[0] = 1.23;

You need to know the strictest alignment requirement, and use that
in your wrapper. And there is no portable way to know what is the
strictest alignment requirement. The closest you could do would
be create a union containing every type that you might malloc, and
hope you never call it for something stricter. Or, you could make
a #define in a header file and document "make sure you set this
properly, or else."

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

Remo D.

Kenneth Brody ha scritto:
:
[... 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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top