Solved

size of a memory location pointed to by a pointer

Posted on 2004-09-26
8
249 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
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 and use switch statements in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now