Solved

Casting calloc

Posted on 1998-03-06
5
579 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 50 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

734 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