Solved

size of a memory location pointed to by a pointer

Posted on 2004-09-26
8
263 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:spike3382
  • 5
  • 3
8 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 80 total points
ID: 12157836
Hi spike3382,

> How do you count the total number of bytes of the area allocated for the string?
There is no way to determine that. Atleast no standard way. You will have to maintain the amount of memory that you requested in a separate variable.

Uusually this information will also be stored by the malloc implementation in the header of the allocated memory block. However, you application is not supposed to know all that information and moreover, the format of the block will change with different implementations of malloc therby making it highly unportable


Sunnycoder
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12157840
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
0
 
LVL 1

Author Comment

by:spike3382
ID: 12157855
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
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 12157864
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
0
 
LVL 1

Author Comment

by:spike3382
ID: 12157884
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
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12157952
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/
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12157958
The above tools were for linux. Sorry, do not have much knowledge of OSX to recommend one :-(
0
 
LVL 1

Author Comment

by:spike3382
ID: 12157975
No problem.  Thanks a lot!
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question