Solved

size of a memory location pointed to by a pointer

Posted on 2004-09-26
8
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using unmanaged DLL from managed application 14 297
How to creat good software interface ? 1 71
Resolve Dependency Issues 4 87
A  &&  B  || (C||D) how it works in c 7 152
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.

730 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