Size of an object/variable from its pointer?

This is a discussion on Size of an object/variable from its pointer? within the C Programming forums, part of the General Programming Boards category; Does anyone know if its possible to find the size of an variable from a pointer to the said object. ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    1

    Size of an object/variable from its pointer?

    Does anyone know if its possible to find the size of an variable from a pointer to the said object. I have the below code (and similar code in my_malloc, which for obvouse reasons is simpler to implement) ovcourse the below returns the size of the pointer not the object, can anyone help?

    Code:
    void my_free(void *ptr){
    	div_t temp;
    	
    	if (memmoryusage == NULL){
    		memmoryusage = 0;
    	}
    	
    	memmoryusage = memmoryusage - sizeof(&ptr);
    
    	temp = div( memmoryusage, 100 );
    	printf("(F) MEMORY = %d, %d, %d%\n", memmoryusage, sizeof(&ptr), temp.quot);
    	free(ptr);
    	
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    If ptr points to an object then by dereferencing and taking its sizeof gives the size of the said object as in
    Code:
    char *ptr, c;
    ptr = &c;
    sizeof *ptr; /* gives you the sizeof the character c */

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by cerberus_jh View Post
    Does anyone know if its possible to find the size of an variable from a pointer to the said object.
    No it is not possible. I recommend having a map of pointers allocated by my_malloc() that associates them with the size.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by itCbitC View Post
    If ptr points to an object then by dereferencing and taking its sizeof gives the size of the said object as in
    Code:
    char *ptr, c;
    ptr = &c;
    sizeof *ptr; /* gives you the sizeof the character c */
    1) You don't need *ptr to be a valid pointer for size_of to work. size_of does not evaluate its expression.
    2) In that case you may as well do size_of char, since you know the type.
    3) In the OP case, he can't do that because void pointers are not dereferenceable.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by itCbitC View Post
    If ptr points to an object then by dereferencing and taking its sizeof gives the size of the said object
    No, that just gives the size of the type the pointer is pointing to. That doesn't mean the object itself is of that type.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by brewbuck View Post
    No, that just gives the size of the type the pointer is pointing to. That doesn't mean the object itself is of that type.
    This is C, so it does. There is no polymorphism.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by King Mir View Post
    This is C, so it does. There is no polymorphism.
    Uh, no.

    Code:
    struct A
    {
        int x;
        int y;
    };
    
    struct B
    {
        struct A base;
        int z;
    };
    
    B *newB = malloc(sizeof(B));
    ...
    A *basePtr = (A *)newB;
    sizeof(*basePtr) /* is not equal to the size of the object pointed to */
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Well yes, if you cast, you change the type of the object in the context of the expression. But that's semantics.

    The point is well made that size_of does not magically guess the size of the original malloc.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what about this?
    Code:
    double *dynarr;
    dynarr = malloc(sizeof(double) * 200);
    printf("size=%d\n", sizeof(*dynarr));

    Normally, malloc & free rely on storing the size (and possibly some other bits of data) in a block of memory immediately before the memory returned to the "client". That's the correct way to do it.

    Code:
    	if (memmoryusage == NULL){
    		memmoryusage = 0;
    	}
    That's surely incorrect.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Perhaps it's compiler and/or machine dependent. I tried it on my machine and both the C and C++ compiler give correct results for sizeof *basePtr;
    Quote Originally Posted by brewbuck View Post
    Uh, no.

    Code:
    struct A
    {
        int x;
        int y;
    };
    
    struct B
    {
        struct A base;
        int z;
    };
    
    B *newB = malloc(sizeof(B));
    ...
    A *basePtr = (A *)newB;
    sizeof(*basePtr) /* is not equal to the size of the object pointed to */
    Last edited by itCbitC; 12-10-2008 at 08:21 AM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you consider "correct size"? I would expect the compiler to print 8, but the allocated size is 12.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Correct *newB is 12 and *basePtr is 8 bytes.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    Correct *newB is 12 and *basePtr is 8 bytes.
    So that is exactly what the original poster DOESN'T want.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    This is a great example for those learning the language to take the time to use gdb and perhaps printing out the assemply language to see how memory is placed on the stack for such a small piece of code like this. This will shed light on what the sizes amount to and why.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Gdb? ...Nah.
    Visual Studio is far better
    Of for the lack of a better word, this is a great example why people should take time to use a debugger!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 10:27 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. size of the memory reserved for a pointer
    By cris_orpi in forum C Programming
    Replies: 13
    Last Post: 03-05-2003, 10:42 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21