Calculating space efficiency using sizeof()

This is a discussion on Calculating space efficiency using sizeof() within the C Programming forums, part of the General Programming Boards category; When calculating for space efficiency, do I have to calculate everything, or just the node size? For example: Let's say ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    20

    Calculating space efficiency using sizeof()

    When calculating for space efficiency, do I have to calculate everything, or just the node size?

    For example:
    Let's say I have a linked list structure. It consists of a string, and a pointer to the next node. The string is "hello":

    Code:
    struct test
    {
       char* a;
       test* next;
    };
    
    a = malloc(5);
    strcpy(a, "hello");
    structure = 4 bytes
    char * = 4 bytes
    next node * = 4 bytes
    size of 'hello\0' = 5 bytes

    total = 17 bytes?

    or should the total just be 9 bytes (size of hello and size of pointer to next node) ?

    I'm doing this using the sizeof() function to calculate how much "space" I need. Hope you guys understand what I'm talking about

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Few things to consider. First of all, you can't access variable a directly like that. You need a struct object, since a is a member of that struct.

    Another thing to consider is that "hello" is really 6 bytes:

    Code:
    'h','e','l','l','o','\0'
    If you make mistakes like that, you'll cause possibly hard to find bugs. This is obviously not good. Always make sure you have enough room for all of your data.

    On to the subject of struct sizes.... they are never guarenteed to be the size of their members summed together. Compilers tend to pad structs with extra bytes in order to align them to be more efficient on the machines that you are compiling them for. Therefore, you can't be sure about the size, so you must use the sizeof operator when allocating memory for an entire struct object.

    Lastly, you have to allocate memory for the struct object itself, and then also allocate memory for each pointer value inside the struct object you created (unless you have something to point them those member pointers at). Example:

    Code:
    struct bleh
    {
    	char *s;
    };
    
    int main(void)
    {
    	struct bleh *p = malloc(sizeof(*p)); /* allocate for struct */
    	p->s = malloc(BUFSIZ); /* Allocate for struct member pointer */
    
    	.... /* do something */
    
    	free(p->s);	/* free struct member var, which is a string object */
    	free(p);	/* free struct object */
    
    	return 0;
    }

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    sizeof struct is 8 bytes (4+4)

    additionally you dynamically allocate 5 bytes, and it is not enough to store 6 bytes string "Hello" (5 chars + null char)

    sizeof will not report you the size of the allocated memory - you should store it by yourself if you need it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    20
    EDIT: Oh, looks like you answered my question, vart. It's (a), right? Thanks!

    Ah, you guys are amazingly good at spotting my mistakes!

    I do know that the size of hello is actually 6 bytes though, my mistake there.

    I always allocate my pointers and etc with the sizeof operator, e.g.

    Code:
    char* something(char* word)
    {
        char* a;
        a = malloc(sizeof(char) * (strlen(word) + 1));
    
        /* etc etc */
    
       return a;
    }
    But as my question states, what would the actual size of the whole thing be, then?

    a?
    Code:
    sizeof(theStruct->a) + sizeof(theStruct->next) + (strlen(theStruct->a) + 1)
    (strlen(theStruct->a) + 1 assumes that 1 character is 1 byte.)
    (Therefore this calculates the size of the char pointer, the size of the next pointer, and the string itself)

    b?
    Code:
    sizeof(theStruct)
    c?)
    Code:
    (sizeof(strlen(theStruct->a) + 1) + sizeof(theStruct->next)
    Last edited by markcls; 05-19-2007 at 02:28 AM.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    But as my question states, what would the actual size of the whole thing be, then?
    What is this "whole thing" you are talking about?

    theStruct is a pointer to some memory location, its size is 4 ( for example)
    *theStruct is a struct itself located somethere, its size if sizeof *theStruct

    a is a pointer - see above
    it points to some dynamically allocated memory (if malloc succeded) the size of memory is at least the size you requested... It can be more if the current implementation of malloc stores some additional info arond the memory you requested to check memory overruns...

    There is no such thing as "whole thing" in your sample
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The point I was making earlier about padding means that you can't be sure that a struct will be that size.

    Code:
    #include <stdio.h>
    
    struct bleh
    {
    	int w;
    	char x;
    	float y;
    	double z;
    };
    
    void printsize(const char *, size_t);
    
    int main(void)
    {
    	printsize("int",sizeof(int));
    	printsize("char",sizeof(char));
    	printsize("float",sizeof(float));
    	printsize("double",sizeof(double));
    	printsize("struct bleh",sizeof(struct bleh));
    	
    	return 0;
    }
    
    void printsize(const char *szType, size_t stType)
    {
    	printf("sizeof(%s) = %d\n",szType,stType);
    }
    Output when compiled with MinGW, Borland, and LCC on a Windows 32-bit environment:

    Code:
    sizeof(int) = 4
    sizeof(char) = 1
    sizeof(float) = 4
    sizeof(double) = 8
    sizeof(struct bleh) = 24
    4 + 1 + 4 + 8 = 17

    Yet the struct is padded to be 24 bytes in size.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    20
    Reason is, I'm trying to compare the space efficiency between 2 data structures... that's why I'm trying to print out all the memory used to run my programs.

    Thanks guys!

    MacGyver: Oh, okay. Very interesting indeed. Thanks for the info! I guess it's safer to do a sizeof(theStruct) rather than to calculate them one by one. THEN, add the sizeof any other allocated memory. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 02:43 PM
  2. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  3. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  4. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  5. How to load pixels of BMP into an array
    By brconner in forum Windows Programming
    Replies: 10
    Last Post: 06-02-2007, 04:30 AM

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