# Thread: Calculating space efficiency using sizeof()

1. ## 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. 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. 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

4. 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)`

5. 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

6. 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. 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