?
Solved

size of a memory location pointed to by a pointer

Posted on 2004-09-26
8
Medium Priority
?
285 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 320 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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 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 pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

762 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