LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-28-2006, 03:51 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
[C] get allocated size


Hi,

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?
 
Old 12-28-2006, 09:08 AM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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).
 
Old 12-28-2006, 09:38 AM   #3
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
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

Cheers!
 
Old 12-28-2006, 10:30 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.
 
Old 12-28-2006, 12:55 PM   #5
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
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.
 
Old 12-28-2006, 01:20 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.
 
Old 12-29-2006, 09:55 PM   #7
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Code:
void* MyMalloc(size_t bytes) {
   void* mem = malloc(sizeof(size_t) + bytes);

   if (mem == NULL) {
      return NULL;
   } else {
      *((size_t*) mem) = bytes;
      return mem + sizeof(size_t);
   }
}

size_t BytesAllocated(void* mem) {
   return ((size_t*) mem)[-1];
}

void MyFree(void* mem) {
   free(mem - sizeof(size_t));
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Freeing memory allocated in a function con Programming 4 01-02-2006 03:25 AM
When is the user stack allocated? moadon Programming 1 04-11-2005 11:28 PM
About allocated memory in C++ Ephracis Programming 2 12-28-2004 02:56 AM
allocated space of a directory Dave31836 Programming 8 10-04-2004 06:07 PM
UT2004 - Allocated Only 32M AGP! Phorem Linux - Games 0 04-16-2004 03:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration