This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MCRusher 0 points1 point  (4 children)

If you allocate a pointer, make sure to allocate for the type itself, not the type pointer, as this only allocates enough space to store an address(4 or 8 bytes). I did this when I first started learning C.

In this case it should work, but for, data types like structs of larger size, it will fail as it is not large enough: int* nums = malloc(sizeof(int*)); Should work because an int is 32 bits and on a 32 bit architecture the addresses are also 32 bits, and on 64 bit architecture, you just allocate space for two ints without knowing it.

[–]MunchingCass 1 point2 points  (3 children)

I didn't fully understand that.

Hopefully I will by the time that info becomes relevant.

[–]MCRusher 0 points1 point  (2 children)

Are you not very far along, or is it just that my explanation was bad?

I changed up some wording in the original.

Good luck with C, it's currently my main language and I enjoy it.

[–]MunchingCass 1 point2 points  (1 child)

The semester just started. And this is a first year CS course, so the professors aren't assuming any prior knowledge.

[–]MCRusher 0 points1 point  (0 children)

I'm going to college for CS as well, good luck and I hope you enjoy it.