Re: [C99] A question about malloc()
- The block isn't created but allocated. This looks pedantic, but in some cases you have to remember that there is only so much memory to allocate.
- Do not cast the returned value of malloc(). malloc() returns a "void*", a pointer to anything that fits any typed pointer without a cast. When you cast you run the risk to not notice that you forgot to declare malloc() properly (because the compiler will think it returns an int, and with a cast it won't warn you about assigning the int to a pointer)
- Yes, the allocated block is continuous, so all pointers from the origin to the origin+allocated size-1(*) correspond to usable memory locations.
(*) using the pointer arithmetic semantics, adding N to the pointer moves it (N * size_of_type_pointed_to) bytes.
Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.