I've got some questions about dynamically allocating an array. I've got the allocation to work, but I was wondering why the following works:
(this constructor has been edited so it only shows the allocation stuff)
stack::stack( char name, int size )
stck = (char *)malloc( size * sizeof(char));
*stck = '\0';
1. Why is the (char *) cast required in C++? When I was learning C, I once saw a post from Stee saying that the cast is optional and bad because it can mask an error (something I found to be true). If you take the cast out, the above line won't compile in C++ (but it will in C).
2. Why does the cast create an array of char's (1 byte each on my computer) instead of an array of pointers (4 bytes each).
3. In my destructor, I have:
free( stck )
Why does this work? Seems to me this should only free the first member of the array, while leaving the others in memory. If that's unclear, I think of it as akin to freeing the head of a linked list and assuming that destroys the entire list.
Thanks in advance for your help!