Questions regarding the Heap

+1 Toyen . · August 27, 2015
Hey there everyone!

I´d love it if anybody could clear this up for me a little bit.

So int * points; is a pointer called points that is not pointing to anything?

points = (int *) malloc(5* sizeof(int)); so now the name of the pointer that is not pointing to anything is a pointer to an unnamed integer of a big size?

Also which int exactly are we speaking of when using the sizeof(int)? Or it doesnt matter and its like an abstract int that is very big?

Also, the asterisk in the first bracket is used because we are typecasting a pointer?

Post a Reply


Oldest  Newest  Rating
+1 c student · August 27, 2015
points is declared as a pointer to an int but it really means it points to 4 bytes of memory.  when you malloc, you are allocated 5 int sizes of memory which means 5*4 bytes = 20 bytes.
points -> [] [] [] [] []    // points is a pointer to 20 bytes; each [] is 4 bytes

think of it like an array.  it would be similar to the following:
int array[5];    // array contains 5 int-sized (4 bytes) blocks of memory

in c, you are not required to typecast a malloc call.
0 Toyen . · August 28, 2015
I see, thanks for your reply.

So why exactly is the (int *) there?
0 c student · August 29, 2015
you'll have to ask the author
0 Toyen . · August 29, 2015
Somebody asked in the comments under the video and someone else replied saying that:

"the malloc function returns a void pointer , but we need an integer pointer , That is why the void pointer has been typecasted to return an integer pointer."
0 c student · August 30, 2015
0 Toyen . · August 31, 2015
Oh that clears it up a bit, thanks for the link! : )
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator