Thread: Size of pointer?

  1. #1
    Registered User Mortanius's Avatar
    Join Date
    Jul 2016
    Posts
    7

    Size of pointer?

    Well, I know sizeof(pointer) does not give me the size of the allocated space, but the size of the pointer itself. But there isn't a way to know it? Since if we use the free() function it will free the allocated space receiving only the pointer as argument. How's that?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > But there isn't a way to know it?

    No standard way. Why? Because the committee decided it wasn't important enough, I suppose. That said, in the majority of cases you know the size that was allocated somewhere, and it's straightforward enough to pass around as needed.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,117
    > Since if we use the free() function it will free the allocated space receiving only the pointer as argument. How's that?
    When you called malloc, the size of the block allocated is typically stored somewhere, so that free knows what to do.

    Most programs are only interested in how many objects a pointer points to, not how many bytes.
    Eg
    Code:
    size_t num = 10;
    int *mem = malloc( num * sizeof(*mem) );
    for ( size_t i = 0 ; i < num ; i++ ) mem[i] = 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User Mortanius's Avatar
    Join Date
    Jul 2016
    Posts
    7
    Thanks for answering.
    I don't understand why it's not that important, but that's ok. It would be useful to me to know that, since in small programs I made I had to use 2 variables to store the size of the allocated space and the size of the using space in order to use realloc() whenever it's necessary

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    It's not that it wasn't deemed "important". It's that malloc shouldn't have to remember the exact size that you asked for. In order to manage and free the block, malloc only needs to remember the actual amount of memory that it set aside, which may be more than you asked for.

    On linux, you could try the following, which returns 136 for me, even though I only asked for 123 bytes. Note that there is in fact a (non-standard) function to get the usuable block size.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h> // malloc_usable_size()
    
    int main(void) {
        char *p = malloc(123);
        printf("%d\n", (int)malloc_usable_size(p));
        free(p);
        return 0;
    }

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,670
    Quote Originally Posted by Mortanius View Post
    I don't understand why it's not that important, but that's ok.
    Let me try to explain.

    First of all, think of C as a "close to the metal" language, meaning that, although "high level", it tries to be as fast, direct and low-on-resources as possible. Also, consider that C was initially created as a tool to build Unix with. Lastly, think of the myriad of devices( such as micro-controllers etc ) whose functionality has little resemblance to modern PCs.

    With the above said, what would be the benefits of an automatic length/size associated with a pointer/array? Convenience maybe... But would it? I'll list some of the potential problems/concerns:
    *) How many bytes should this length/size variable be?
    *) Would it be optional? ( Yes: efficiency problem ) ( No: size problem )
    *) Would it be enforced( not allowed to go out-of-bounds )?

    These are my two cents on the subject.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Size of a function pointer?
    By someprogr in forum C Programming
    Replies: 4
    Last Post: 10-31-2008, 08:42 AM
  2. How do i know the size of variable pointer
    By unikgila in forum C Programming
    Replies: 10
    Last Post: 10-28-2008, 10:08 AM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. size of a pointer
    By anykey in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2005, 11:16 AM
  5. size of a char pointer (*)
    By Garfield in forum C Programming
    Replies: 14
    Last Post: 10-04-2001, 07:00 PM

Tags for this Thread