-4

I am confused as to what the sizeof function returns.

Suppose I enter the following into a program for sizeof, and this is what is returned:

sizeof(char) ==  1
sizeof(int) == 4
sizeof(float) == 4
sizeof(long) ==  8
sizeof(double) == 8
sizeof(void *) == 8

sizeof Pointer is 8.
So if I malloc a long array:

long *array = (long *)malloc(64)

Would this sizeof() inserted into the malloc of my long array be equivalent?

long *array = (long *)malloc(8*sizeof(long))

Now if I have a struct:

typedef struct treenode {
    struct treenode *left;
    struct treenode *right;
    void *content;
} Treenode; 

And I run sizeof(Treenode), do I add all the elements of the struct together?

Pointer + Pointer + void * = 8 + 8 + 8 = 24

I tried running this in a program, however I get sizeof(Treenode) to return 12.

I am also wondering why when I create a new struct like this:

typedef struct country {
    char city[40];
    int population;
    int rank;
}

and try to malloc(sizeof(country *)) only 8 bytes are allocated instead of the bytes of the struct.

Finally when I do sizeof("Korea"), why is my return value 6 and not 5? Should it not be 5 because there are only 5 chars in the string?

whoan
  • 8,143
  • 4
  • 39
  • 48
George
  • 1
  • 1
  • What is the definition of `Pointer`? BTW all of your questions have been answered multiple times here on SO. You should be able to find them. – Steve Fallows Nov 22 '14 at 20:14
  • You are missing ***A LOT*** of very basic concepts of C. For example, the fact that the size of a pointer is not the same as the size of its pointed object. And that strings contain an extra 0-terminator. You should first familiarize yourself with the fundamentals of the language before trying to do advanced stuff (like dynamic memory management). – The Paramagnetic Croissant Nov 22 '14 at 21:20
  • Note that `sizeof` is an operator, not a function. – Clifford Nov 22 '14 at 22:17
  • Please read the help text on how to ask question here, again: don't ask several questions in one go, take more care in the formulation your question and search the site before asking. – Jens Gustedt Nov 22 '14 at 22:20

3 Answers3

2

I will address each question separately:

  • These are equivalent in the context you have described

    long *array = (long *)malloc(64)
    long *array = (long *)malloc(8*sizeof(long))
    

    However, this is not necessarily correct on all machines (if say on one machine a long is 16 bytes), therefore this code is not portable. You should only ever use the second method which is portable.

  • I'm not sure why the size is claiming to be 12 - Sorry. (are you sure all pointer's sizes are 8?)

  • while a country struct may be very large, a pointer to a country is just a pointer and as such it's size is in your case 8.
  • In C strings are null terminated so that a string with five letters has a trailing null which is presumably part of the string's size.
user2008934
  • 321
  • 2
  • 9
1
  1. If sizeof(Treenode) is 12, then you are not using 64-bit pointers/memory. If you got different results elsewhere (sizeof(void *) == 8) then you must have different targets.

  2. sizeof(country *) is the size of a pointer to a country, that is -- the same as any other pointer. Use malloc(sizeof(country)) instead.

  3. sizeof("Korea") includes the size of the string's null terminator.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88
0

It is all matter of which architecture you are using.
on 32 bit, each pointer size is 4 bytes.
on 64 bit each pointer size is 8 bytes.

So if you run the following code:

long *array = (long *)malloc(64) ;

and the following code:

long *array = (long *)malloc(8*sizeof(long))

it would be the same on 64-bit but not in 32 bit.

malloc(sizeof(country *)) allocates only 8 bytes because (country *) is type of a pointer (pointer to your country struct) and each pointer is 8 bytes long in 64-bit.

whoan
  • 8,143
  • 4
  • 39
  • 48
CodeNinja
  • 291
  • 5
  • 19