Link to home
Start Free TrialLog in
Avatar of spike3382
spike3382

asked on

size of a memory location pointed to by a pointer

Hi all.  I hope this is an easy question--how does one determine the size of a memory location pointed to by a pointer?  For example, to determine the number of bytes in a string you do strlen(), which counts the number of bytes until the string termination character.  How do you count the total number of bytes of the area allocated for the string?

The actual specific reason I ask is that I have a pointer to a MYSQL_RES (a type that holds info on a result set from the mysql c api which contains, among other things, an indeterminate amount of rows), and I'd like to know how much memory it's taking up.

This is probably something I should have learned long ago....

Thanks a lot!
-matt
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
spike3382,

> among other things, an indeterminate amount of rows), and I'd like to know how much memory
> it's taking up.
use some good memory profiling tool. There are some good ones for each platform. Tell us your working environment and we should be able to recommend some

Sunnycoder
Avatar of spike3382
spike3382

ASKER

Hi Sunnycoder,

Thanks!  That was quick.  I'm developing this app simultaneously on a Linux box running Gentoo and on a mac OSX (long story).  

So, there's no way to know dynamically in the program itself how big this thing is?  What if I wanted to memcpy it?  How many bytes would I tell it to copy?

Thanks,
Matt
spike3382,

> So, there's no way to know dynamically in the program itself how big this thing is?  What if
> I wanted to memcpy it?  How many bytes would I tell it to copy?
C expects the programmer to know how much data he/she want to have copied and also to make sure that there is sufficient memory to do that. When you issue a malloc or realloc, you supply amount of memory required as a parameter. All you need to do is, keep it somewhere safe for later use :)

Sunnycoder
That all makes sense.  The only thing that I still don't get is that I never call malloc for this pointer.  I guess that happens in the mysql shared library.

Anyway, I'm still interested in a good memory profiling tool for either system (OSX would be ideal).  Thanks!

-Matt
I have not worked much with OSX but you can use gnu memprof at a higher level and valgrind for in depth memory usage and debugging

http://valgrind.kde.org/
http://www.gnome.org/projects/memprof/
The above tools were for linux. Sorry, do not have much knowledge of OSX to recommend one :-(
No problem.  Thanks a lot!