Thread: malloc - verifying amount of memory allocated

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    4

    malloc - verifying amount of memory allocated

    How can I view the number of bytes that have been allocated by using the malloc function?

    I tried:

    mem = (float*)malloc(num*sizeof(float));

    printf("The amount of memory allocated using malloc is %d.", mem);

    Note: The variable "num" in my program is equal to 7.

    But every time I run the program, this value changes.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The size is precisely the size you requested. If it was unable to be allocated in full, malloc returns NULL.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The program is printing the address of the allocated memory, not the size. malloc() doesn't return the size of the actual amount of memory allocated. malloc() uses internal structures to keep track of the amount of allocated memory, but those structures will be different depending on the operating system and/or the compiler being used. A program wouldn't normally be able to access those internal structures, so there's no real way to verify the size requested, other than to trust that malloc() worked if it returns a non-NULL pointer.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by rcgldr View Post
    The program is printing the address of the allocated memory, not the size. malloc() doesn't return the size of the actual amount of memory allocated. malloc() uses internal structures to keep track of the amount of allocated memory, but those structures will be different depending on the operating system and/or the compiler being used. A program wouldn't normally be able to access those internal structures, so there's no real way to verify the size requested, other than to trust that malloc() worked if it returns a non-NULL pointer.
    I think you're confusing the op now. Perhaps this will make things clear:
    Code:
    char * p;
    size_t sz = 7 * sizeof *p;
    
    if (p = malloc(sz))
      fprintf(stderr, "Successfully allocated _at least_ %lu bytes at pointer %p\n", sz, p);
    else
      fprintf(stderr, "Failed to allocate %lu bytes\n", sz);

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It may be confusing but the op should start to learn that a pointer is an address and that valgrind is life. And that tcmalloc also exists and that google is da best!

  6. #6
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    @mutantjohn: google iz 4 kidz, u got 2 write ur own web crawler!

    @nonpuz: n u got 2 cast sz to unsigned long in call 2 fprintf!

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    @Barney: Hey, probably some letters in your keyboard have jammed or something... Consider changing your keyboard as soon as possible, if that is the case.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    I just felt like using a different writing style. ^_^

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    [SPAM]
    My eyes felt pain.
    [/SPAM]
    Next time, I would suggest you to write normally.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by std10093 View Post
    @Barney: Hey, probably some letters in your keyboard have jammed or something... Consider changing your keyboard as soon as possible, if that is the case.
    Personally I thought it looked kewl.

  11. #11
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    google iz 4 kidz, u got 2 write ur own web crawler!
    XD

    I heard tcmalloc has multithreaded performance comparable to hoard which compared to malloc is like 8x better. Granted, that's for multithreaded allocations but those might be more common than you think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc not actually allocating right amount of memory?
    By somerandomdude in forum C Programming
    Replies: 4
    Last Post: 10-02-2011, 12:43 PM
  2. Replies: 9
    Last Post: 09-15-2011, 03:28 PM
  3. Replies: 2
    Last Post: 06-25-2010, 04:04 AM
  4. Replies: 4
    Last Post: 12-01-2005, 11:16 PM
  5. amount of allocated memory...
    By lobo in forum Windows Programming
    Replies: 2
    Last Post: 06-20-2002, 10:18 PM