Thread: How can I view the size of memory allocated via malloc in gdb?

  1. #1
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    134

    Question How can I view the size of memory allocated via malloc in gdb?

    I am debugging a program with gdb and I have a ptr to memory allocated using malloc. It is dynamically allocatted based off of some math so it would be useful to be able to type into gdb a command and specify the ptr, and be able to get back the exact actual size of memory allocated. Is this possible? Are there any alternative methods? I'm just trying to debug and make sure my math isn't off at runtime.

    Thanks.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,506
    Why not put the result of the math into a variable, then examine that?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    134
    Quote Originally Posted by laserlight View Post
    Why not put the result of the math into a variable, then examine that?
    That's a possibility for sure but was wondering if the functionality was in gdb that way I could examine other buffers as well. I'm guessing gdb does not do this? I've used ptype for stack buffers but doesn't work on malloc'd ones.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc - verifying amount of memory allocated
    By mbxs3 in forum C Programming
    Replies: 10
    Last Post: 09-08-2013, 08:03 PM
  2. Replies: 9
    Last Post: 09-15-2011, 03:28 PM
  3. Replies: 2
    Last Post: 06-25-2010, 04:04 AM
  4. Size of allocated memory
    By TriKri in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 01:22 PM
  5. Replies: 4
    Last Post: 12-01-2005, 11:16 PM

Tags for this Thread