ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How can I get how much I have allocated with malloc? I have a function that allocates memory using malloc but how can I later on check how much that was allocated? Is there some sort of function or will I have to keep track of that myself?
You can, but that requires architecture-dependent black magic. You'll have to keep track of it yourself somehow, either through a separate variable or by null termination (if it is an array of pointers).
Hi,
tuxdev is right. The language itself doesnt provide you a standard way to see how much memory you have allocated (or where, or blah). So you should write a myCountingMalloc which calls malloc and keep track of the statistics you want. Of course, you have to write since it does not exist
Other languages on non-unix platforms resort to what are called descriptors, usually kept in a global array. Old VMS did something like that in the kernel, for example. User processes had string descriptors in user space as well. The BSTR in Windows is a string descriptor and is derived from the VMS model -
Code:
/* this is the general idea */
typedef struct
{
void *ptr; /* copy of the base address */
size_t len; /* bytes allocated */
} desc_t;
desc_t malloc_arr[100]={NULL,0};
The descriptor table(s) require maintenance for every single malloc, free, and realloc call you make. So it's best to write a wrapper for each call that does the maintenance. It's pretty good for preventing overruns.
Thanks for your very fast and interesting replies. Tuxdev, where can I read about this black magic? I do not intend to use it in my code but it would be interesting just to learn about it.
I'm not sure about where to find such information for glibc, but I would think it is documented somewhere. For a simple implementation of malloc I did once, I stored the size of the chunk in the previous word. So, if you got "addr" from malloc, "*(addr-1)" got you the size.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.