473,508 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
5 6296
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2512
by: Tarundeep | last post by:
hi, let us say it is a 32 bit processor then the size of the pointer is 32 bits. now i want to know what would be the size of the class with vtable pointer in it , that is it has few virtual...
79
125147
by: syntax | last post by:
what is the size of a pointer? suppose i am writing, datatype *ptr; sizeof(ptr); now what does this sizeof(ptr) will give? will it give the size of the
22
2425
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
31
3680
by: bilbothebagginsbab5 AT freenet DOT de | last post by:
Hello, hello. So. I've read what I could find on google(groups) for this, also the faq of comp.lang.c. But still I do not understand why there is not standard method to "(...) query the...
5
4124
by: Charles M. Reinke | last post by:
OK, this may be a dumb question, but please bear with me. If I declare a pointer: int *p; the memory space for a pointer is allocated, but is the memory space also reserved for one integer,...
5
1819
by: yancheng.cheok | last post by:
hello, may i know how i can determine whether a pointer is pointing to an array or a single item during runtime? this is because in certain situation, i need to determine whether to use delete...
6
3024
by: sajohn | last post by:
Hi A separte application is sending me a pointer to some binary data and I need to find the size of the binary data being passed to me. Does anyone know I can go about doing this? TIA
43
4880
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy....
12
2065
by: Frodo Baggins | last post by:
Hi I need to know the size of the memory block pointed to by a char* in a function receiving this pointer. Typically, this pointer points to a string. strlen() will not do the job since sometimes...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.