Thread: Size of objects in Memory

  1. #1
    Gentoo System .......... Doorsdown's Avatar
    Join Date
    May 2007
    Location
    Rhode Island, USA
    Posts
    19

    Size of objects in Memory

    I have tried searching both google and these forums to no avail. I am looking to obtain the size of an object(s) in memory. I have researched sizeof() and found it to only show how many byte a given type takes up. But I need to know the size of any object in memory. I'm not exactly sure where to start or how to go about it. So any help would be great.

    Thank you
    Last edited by Doorsdown; 05-29-2007 at 10:16 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    The sizeof() function gives you the number of bytes in memory an object will occupy. I don't see how that's different from what you're asking, or maybe I'm misunderstanding your question.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Gentoo System .......... Doorsdown's Avatar
    Join Date
    May 2007
    Location
    Rhode Island, USA
    Posts
    19
    If i have a char array, each char should be 4 bytes, but always returns 4 bytes as the size

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Are you sure you're not checking the size of a pointer?
    http://c-faq.com/aryptr/aryparmsize.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually if you have an array of T, each T might not be four bytes according to sizeof.

    Proper use of sizeof when arrays enter the picture comes down to knowing when arrays decay into pointers and when they don't - because this dictactes (logically) what sizeof does. If an array is a pointer at the moment, you'll get something different than say sizeof double. I think what you need is simply a detailed explanation.


    edit: Dave types faster.

  6. #6
    Gentoo System .......... Doorsdown's Avatar
    Join Date
    May 2007
    Location
    Rhode Island, USA
    Posts
    19
    Code:
    char *key;
    int size = 0;
    printf("Please fill key: ");
    scanf(key);
    size = sizeof(*key);
    printf("\nSize of key is %i.\n",size);
    this outputs, Size of key is 1.

    Code:
    char *key;
    int size = 0;
    printf("Please fill key: ");
    scanf(key);
    size = sizeof(key);
    printf("\nSize of key is %i.\n",size);
    this outputs, Size of key is 4.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Doorsdown View Post
    Code:
    char *key;
    int size = 0;
    printf("Please fill key: ");
    scanf(key);
    size = sizeof(*key);
    printf("\nSize of key is %i.\n",size);
    this outputs, Size of key is 1.
    Yup, the size of an unsigned char is 1.

    Quote Originally Posted by Doorsdown View Post
    Code:
    char *key;
    int size = 0;
    printf("Please fill key: ");
    scanf(key);
    size = sizeof(key);
    printf("\nSize of key is %i.\n",size);
    this outputs, Size of key is 4.
    Yup, the size of a pointer to an unsigned char is 4, on your system.

    Are you looking for strlen? [edit=3]http://www.open-std.org/jtc1/sc22/wg...trlen_function[/edit]

    Wait, first allocate some memory into which to write the input. Oh, and see the FAQ about getting user input as text. And ditch scanf until you know more than you want to.
    Last edited by Dave_Sinkula; 05-29-2007 at 10:36 PM. Reason: Added link.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Gentoo System .......... Doorsdown's Avatar
    Join Date
    May 2007
    Location
    Rhode Island, USA
    Posts
    19
    that was just a example, i am writing a task for python and it will pass in a pyobject and i need to find a way to figure out its size in memory. So strlen will not cut it

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    147
    Strings can be an annoying case.

    Otherwise, if you've made the things you're passing, you should be able to calculate the size.

    With structures and built in primitives, the sizeof is fine.

    For arrays of anything OTHER than character arrays, sizeof(whatever) * num where num is the number of entries in the array is fine (number actually stored, not the allocated size from malloc).

    As declared, unless your compiler has specific rules to the contrary, a char array is in bytes, not words or dwords. This, however, is the annoying part. Sometimes one sees a char * or unsigned char * used to point to an otherwise undefined buffer of structures, unicode characters or MCBS characters. For this, you have to do more work.

    If you know you created a character array, and the string processing functions you've used are ASCII versions, then strlen will be correct as the number of bytes.

    Otherwise, you need to know what characters or other structures have been appended to the buffer, and that can get nasty (especially if the char * is otherwise used as a void * for things like bitmaps headers).

    If your compiler has defined the char as an unsigned short or signed short, the sizeof(char) would be 16 bits, but if you use ASCII string functions on that array, you'll probably have bytes. Control over the compiler's options is critical to pin down what you want.

    Keep it simple for yourself. If you're using unicode, get the size of a unicode character (which is constant over the character set) and use that to compute the size in bytes.

    If you're using MCBS, you'll have functions that give you the size in bytes (it's a variable sized character set).
    Last edited by JVene; 05-29-2007 at 10:58 PM.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Doorsdown View Post
    that was just a example, i am writing a task for python and it will pass in a pyobject and i need to find a way to figure out its size in memory. So strlen will not cut it
    Why not? Consider that C strings are a relatively simple type: The only thing strlen really does is count bytes (except you constantly have to +1 to account for the terminating zero). The difference here is that sizeof counts the bytes at compile time, and strlen does so at run time. just to prove it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main( void ) {
      const char stat[] = "deadbeef";
      char *heap = malloc( sizeof stat );
    
      if( stat )
      {
        strcpy( heap, stat );
        if( strlen( heap ) + 1 == sizeof stat )
        {
          printf( "%s is the same size regardless of storage.\n", stat );
          free( heap );
          heap = NULL;
        }
      }
      else
        perror( "\"deadbeef\" array" );
    
      return 0;
    }
    To be more serious, your options are thin. Memory allocated at run time will always use some sort of runtime devised method to find the relative size of things. And by "relative size of things" I mean that the bytesize of an object can often be less important than how many objects there are. If you used a Unicode string, for instance, the byte size would be up to four bytes per character, so you might feel better calculating the bytesize like so

    strlen( key ) * sizeof key[0]

    key[0]'s size is known at compile time, so sizeof works, but the string length could be (and if you're using heap memory, often should be) a question answered at run time.


    This advice also applies to any other object in the history, present, and future of the C world.
    Last edited by whiteflags; 05-30-2007 at 12:09 AM.

  11. #11
    Gentoo System .......... Doorsdown's Avatar
    Join Date
    May 2007
    Location
    Rhode Island, USA
    Posts
    19
    Code:
    09       double *key;
    10       int size = 0;
    11       key = "149186491491496146\n";
    12       size = strlen(key) * sizeof(key[0]);
    13       printf("Key is equal to %s and its %i bytes.\n",key,size);
    outputs

    Code:
    memory.c: In function 'main':
    memory.c:11: warning: assignment from incompatible pointer type
    memory.c:12: warning: passing argument 1 of 'strlen' from incompatible pointer type
    Key is equal to 149186491491496146
     and its 152 bytes.
    it works, but i need to know how to get ride of those errors before i can go any further. As i if i get a char array it will be fine, but it won't always be a char array

  12. #12
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I think what the OP is getting at is what I posed here. Basically, if you have a (void) pointer to some object in memory, why isn't there a standard way of finding out that object's size? It stands to reason that since free() is passed a void pointer and still manages to find out the size of that object (it has to in order to be able to deallocate), then why isn't there a function that will explicitly do this for you?
    The closest that was found was:
    Code:
    size_t _msize( void *memblock )
    But that's probably more port-a-bull than it is port-a-ble.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    It works? And the size part also? Do you see there 152 bytes of data?
    While it is char array - make it char array... When it want - change it to the appropriate type...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Gentoo System .......... Doorsdown's Avatar
    Join Date
    May 2007
    Location
    Rhode Island, USA
    Posts
    19
    the 152 bytes is because its of type double, i would expect that.

  15. #15
    Gentoo System .......... Doorsdown's Avatar
    Join Date
    May 2007
    Location
    Rhode Island, USA
    Posts
    19
    Quote Originally Posted by @nthony View Post
    I think what the OP is getting at is what I posed here. Basically, if you have a (void) pointer to some object in memory, why isn't there a standard way of finding out that object's size? It stands to reason that since free() is passed a void pointer and still manages to find out the size of that object (it has to in order to be able to deallocate), then why isn't there a function that will explicitly do this for you?
    The closest that was found was:
    Code:
    size_t _msize( void *memblock )
    But that's probably more port-a-bull than it is port-a-ble.
    i looked into that function but it wouldn't work as what ever i use has to be portable

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM