473,378 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 6280
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
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
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
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
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
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
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
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
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
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.