Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calloc error under C++ compiler (works fine with GCC) 1

Status
Not open for further replies.

GoAskAlice

Programmer
Aug 31, 2006
14
GB
I am wondering why this code caused an error in C++ compiler under Windows when it compiles fine under Linux with GCC.

Code:
typedef struct {int key; int digit; char string[24];}person;
person *txPtr;
if( (txPtr = calloc( store, sizeof(person) )) == NULL) {

printf("txPtr:Insufficient memory\n");
abort();

}

The error is with the calloc line. I can not give a specific error message as I am on my Linux box and would need to upload the file to my Windows laptop via my web server and configure a workspace under Visual Studio to compile the code. Its a hassle.

I am just curious as to why there is an error under C++ compiler and not with GCC?
 
Well you could be less vague and say what the error is.

My guess is you're getting a "void* to person*" cast error because you're trying to compile C code with a C++ compiler.

The answer is obvious - use a C compiler to compile C code.

If you want C++ code, then use the C++ new/delete operators and not malloc/calloc/realloc/free C functions.

> Its a hassle.
You should try it from the perspective of the people you're expecting to answer the question then.

You must have done this once to see the error message, why didn't you just copy/paste them at that point?

--
 
When I transferred the code to the laptop and got the error I modified it completely to try and get it to work.

If I remember correctly the error was something to do with void* to person* conversion.

I was also curious to know if C code compiles under C++. I have a copy of Visual Studio 6 with a C++ compiler and its all I can use to create programs under Windows.

Is there a freeware C compiler for Windows like GCC that you would recommend or will I have to modify the code to get it to work?
 
Basically I don't know much about C++, however, when under Linux I renamed the file containing your code from file.c to file.cpp it caused an error: "invalid conversion from void* to person*"
I guess it might be a problem but due to my poor knowledge of C++ I can't help you. Any casting I tried to implement didn't fix the problem. This is the code.
Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct {int key; int digit; char string[24];}person;
person *txPtr;
int store =100;
int main(){

if( (txPtr = calloc( store, sizeof(person) )) == NULL) {

printf("txPtr:Insufficient memory\n");
abort();

}
free(txPtr);
return 0;
}

Martin
 
> Is there a freeware C compiler for Windows like GCC
MinGW is the port of gcc to the windows environment
Slightly fiddly to install, so read carefully.

Dev-C++ is an IDE wrapped around the MinGW compiler, with the added bonus that everything is wrapped up in a single executable install.

> or will I have to modify the code to get it to work?
Visual studio will compile C just fine so long as you name your source files with the .c extension.
Or use these flags
[tt]/Tc<source file> compile file as .c
/Tp<source file> compile file as .cpp
/TC compile all files as .c
/TP compile all files as .cpp[/tt]

> I thought C++ was 'backwards compatible' to C
Yeah, back in the mists of time when C++ was nothing more than some clever pre-processing in front of the C compiler.
Nowadays, they're two very distinct languages which happen to share a common ancestor (like English and American).

--
 
Thanks for the confirmation on the error.

I do not feel ready yet to move on to C++ as I am still learning C.

I thought I could compile C code in C++ and that C++ was just C with more libraries.

I have a C compiler now for windows so I can write code for both Linux and Windows.

Thanks for all the help on this matter.
 
>> or will I have to modify the code to get it to work?
>Visual studio will compile C just fine so long as you name >your source files with the .c extension.
>Or use these flags
>/Tc<source file> compile file as .c
>/Tp<source file> compile file as .cpp
>/TC compile all files as .c
>/TP compile all files as .cpp

This I did not know. My version of Visual Studio has not any documentation. This is one reason why I tend to stay with Linux and not venture into Visual Studio.

I will try those flags, I am not sure where to put the flags but I think its probably in compiler options.

I will have a nosy around the menu bar *smiles*
 
Most of C++ is backwards compatible with C, so you'll only need to make a few changes.

I believe this line:
Code:
if( (txPtr = calloc( store, sizeof(person) )) == NULL)

should be changed to:
Code:
if( (txPtr = (person*)calloc( store, sizeof(person) )) == NULL)

to get it to compile in C++.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top