473,320 Members | 1,817 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,320 software developers and data experts.

Determining the size of allocated memory from a pointer

Hackles
18
Hello,
Is it possible to retrieve the size of allocated memory from a pointer (more specifically, a pointer returned my malloc/realloc). This question may have already been asked countless times. However, please consider the following:
  1. The program's heap stores the size of an element
  2. When you use realloc to allocate more memory, a new address may sometimes be issued in order to accommodate the space demand. In these cases, the existing data is copied to the new location, which seems to indicate that realloc can determine the allocated size
I realise that common reactions to such questions include "maintain the size with an int/long/struct" or "use a sentinel", but these approaches would be inappropriate for overloading the assignment constructor (if this was to be used in a class), or efficiency (as decoding escape characters can result in unnecessary overhead) respectively.
If there is a platform-dependent method that requires assembly, please tell me.
Thank you.
Oct 13 '07 #1
8 16896
JosAH
11,448 Expert 8TB
Hello,
Is it possible to retrieve the size of allocated memory from a pointer (more specifically, a pointer returned my malloc/realloc). This question may have already been asked countless times. However, please consider the following:
  1. The program's heap stores the size of an element
  2. When you use realloc to allocate more memory, a new address may sometimes be issued in order to accommodate the space demand. In these cases, the existing data is copied to the new location, which seems to indicate that realloc can determine the allocated size
I realise that common reactions to such questions include "maintain the size with an int/long/struct" or "use a sentinel", but these approaches would be inappropriate for overloading the assignment constructor (if this was to be used in a class), or efficiency (as decoding escape characters can result in unnecessary overhead) respectively.
If there is a platform-dependent method that requires assembly, please tell me.
Thank you.
If you're talking about malloc, it oftenly stores the size of the allocated block somewhere
'before' the pointer it returns (read: at a lower address); this all happens in an
implementation dependent way. The C++ 'new' operator stores a bit more there,
e.g. a pointer to a class description.

kind regards,

Jos
Oct 13 '07 #2
Hackles
18
If you're talking about malloc, it oftenly stores the size of the allocated block somewhere
'before' the pointer it returns (read: at a lower address); this all happens in an
implementation dependent way. The C++ 'new' operator stores a bit more there,
e.g. a pointer to a class description.

kind regards,

Jos
Thank you for your response Jos,
Are there any functions defined in the standard C/C++ libraries that can be used to determine the size for either malloc or new?
Thank you,
Xiao
Oct 13 '07 #3
JosAH
11,448 Expert 8TB
Thank you for your response Jos,
Are there any functions defined in the standard C/C++ libraries that can be used to determine the size for either malloc or new?
Thank you,
Xiao
No there aren't. Every system is free to organize and keep track of allocated blocks
of memory and none of that is supposed to be exposed to the programmer, nor
defined in the Standards for those languages.

kind regards,

Jos
Oct 13 '07 #4
No there aren't. Every system is free to organize and keep track of allocated blocks
of memory and none of that is supposed to be exposed to the programmer, nor
defined in the Standards for those languages.

kind regards,

Jos
I will xplain my understanding.

1. In one word the answer is "yes" on unix. That is you, as a application writer should be able to retrive the size of the memory allocated from the pointer. But there is more.

I will tell you how all this happens in linux, I have some intution on windows but not sure.

When you make a call to malloc, it allocates a block of memory from heap to your program. And it will keep some house keeping information ie the size of the block allocated to this pointer. Generally this information will be kept at a constant offset from the returned pointer.

Thus the answer is totally dependent on how your C/C++ runtime is implemented. If you know the offset you should be able to retrieve the information. But be very careful if you do anything fancy with the location then you will end up crashing.


Indepth, when a process is created OS (read kernel) will give it some memory as heap (how much is a result of negotiation between OS and the programmer)

so if you malloc a small amount of memory, then your process size neither grows nor shrinks. If you alloc large memory, then your C/C++ runtime will internally request the OS to increase the heap size. Programmer stays opaque to all this. You can also voluntary shrink your size but that generally does not happen. The system calls of importance on linux/unix is brk() and sbrk() calls.


Windows story (I am not pretty sure about it):

In windows malloc is just like a wrapper function that talks to OS (read kernel32.dll) every time it is called. So the house keeping is not kept by C/C++ runtime n windows but by the kernel32.dll. I have not got any idea if you be able to retrieve the information even if you knew how it is done on linux you might be restricted.


On other OS say some embedded OS.. It will be there own story. So you should not at all bank on it if you are writing portable code, rather you should keep your info in your structures.
Oct 13 '07 #5
Just to add,

C++ standard container define a method capacity, though not related to your question you might want to know about them. If you know already please ignore
Oct 13 '07 #6
Hackles
18
Thank you smalpani for your explanation. It just seems surprising to me that the C/C++ specifications would miss such a useful function, considering the malloc/realloc specifications imply that size information would be stored. In any case, I will attempt to find a solution to my problem (I am developing for the Windows platform) and post back once successful.
Thank you all for your help,
Xiao
Oct 14 '07 #7
Hackles
18
I've got it!

Expand|Select|Wrap|Line Numbers
  1. unsigned char *test = malloc(42);
  2. cout << "The size allocated is " << _msize(test) << "bytes!";
  3. free(test);
  4.  
Obviously, this is Visual C++/Windows-specific, but still I felt I should tell you (source ).

Anyway, thank you to all that contributed!

Have fun!
Oct 15 '07 #8
JosAH
11,448 Expert 8TB
I've got it!

Expand|Select|Wrap|Line Numbers
  1. unsigned char *test = malloc(42);
  2. cout << "The size allocated is " << _msize(test) << "bytes!";
  3. free(test);
  4.  
Obviously, this is Visual C++/Windows-specific, but still I felt I should tell you (source ).

Anyway, thank you to all that contributed!

Have fun!
Please realize that _msize() is not part of the Standard and is highly implementation
defined. _msize() might even not be there on unix boxes.

kind regards,

Jos
Oct 15 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: binaya | last post by:
Dear all, Say I allocate a block of memory using the command malloc.I have a question. Can I deallocate certain portion of it only during runtime ? For eg: Say I allocate 5 pointers to int...
34
by: Andrew | last post by:
Is there anyway to test if a pointer points to allocated memory or not? For example if I have a pointer such as char *p is there a standard way to test whether an assignment such as the following...
3
by: mandark_br | last post by:
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) {
19
by: pinkfloydhomer | last post by:
Please read the example below. I'm sorry I couldn't make it smaller, but it is pretty simple. When client code is calling newThingy(), it is similar to malloc: the client gets a pointer to a...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
3
by: Jinkuzo | last post by:
Have been working on this project for a week, unfortunately missed the lesson on it and have been struggling to figure it out. These are the instructions and the point at which I'm up to. ...
4
by: soxmax | last post by:
I am working with very limited memory so I thought I would allocate all the memory I would need in my top level DLL and then pass a pointer to the other DLLs so they could use the same allocated...
5
by: Paul | last post by:
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,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.