Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Casting calloc

Posted on 1998-03-06
5
Medium Priority
?
590 Views
Last Modified: 2012-08-14
I'm taking a C-programming course this term and my instructor did not explain the use and syntax of calling the calloc function very well. My question is, does a statement that calls calloc to generate an address for a pointer always have to have a cast to convert it to the type of pointer for which it is generating the address?  I'm using Symantec C++ for my compiler and it doesn't seem to care about the cast, but some of my classmates are using other compilers, such as Borland C++ 4.5, Turbo C 1.0 or Visual C++, and these compilers seem to require the cast to convert the return of the calloc function from a pointer to void to a pointer to char.

For example, is the cast (char*) required in the calling statement in the function definition below where STRINGMAX is defined as a symbolic constant 255 and arraySize equals 10:

void initStrArray(char *A[], int arraySize)
{
   char inLine[STRINGMAX];
   int i;

   for (i=0; i<arraySize; i++)
   {
      fprintf(stderr, "Enter a string: ");
      gets(inLine);
      A[i] = (char*) calloc(strlen(inLine)+1, sizeof(char));
      strcpy(A[i], inLine);
   }
   return;
}
0
Comment
Question by:zircon
  • 2
  • 2
5 Comments
 
LVL 1

Author Comment

by:zircon
ID: 1257610
Edited text of question
0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1257611
I believe that this is truely compiler dependent. However, I think ( please someone tell me if I am wrong) that you do need the cast to be standard.
0
 
LVL 16

Accepted Solution

by:
imladris earned 200 total points
ID: 1257612
No, it is not required. The point of the "pointer to void" concept is to allow any type of pointer to be passed around through the system without altering it.

However, it is now considered standard good coding practice to cast it when you assign such a pointer to a variable. This would catch potential subtle bugs later on, if the type of the variable that is being assigned the pointer were to change.

0
 
LVL 16

Expert Comment

by:imladris
ID: 1257613
To be more precise I should say, it is not required by the ANSI standard of the language. The "void *" was introduced by the standards body to formalize certain aspects of the language. However, one of the guiding principles of the standardization was: "don't break existing reasonable code" (a paraphrase I'm sure). Therefor constructs such as "cp=calloc(5,1);" which abound in existing code must continue to be compilable.

On the other hand, there are lots of subtle bugs that can be caused by these practices. This is the reason that the ANSI body introduced the opportunity for using various safeguards such as prototypes etc.

Therefore, when writing new code, it is good professional practice to cast such pointers.

I assume that the compilers that are "requiring" the cast can be "reduced" to accepting standard ANSI code. But that should probably only be done, when dealing with legacy applications.

0
 
LVL 1

Author Comment

by:zircon
ID: 1257614
imladris,

Thank you!  Your answer is more-or-less what I expected to hear, but I wanted confirmation before I made the assumption.  My instructor's explanation was only cursory because we ran out of time.  The books I'm using as texts and references don't explain this issue very well either. I knew I could get the answer here. Thanks again.

kellyjj,

Thanks for your comment. Clearly, casting is somewhat compiler dependent, since it works without the cast on my compiler and does not on my classmates' compilers. It looks as though both you and imladris agree that it is standard practice to cast calls to calloc.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question