is typecasting return from malloc required?

C

ceo

hi,

why do i get the following error? do i need to explicitly typecast the
pointer returned by malloc to char *?

thanks,
ceo

[root@lin1 tmp]# cat malloc.cpp
#include <stdio.h>
#include <malloc.h>
#include <string.h>

int main() {

char *s = NULL;
s = malloc(sizeof( char) * 10);
strcpy(s, "blah blah");
printf("\n%s\n", s);
return 0;
}

[root@lin1 tmp]# gcc malloc.cpp
malloc.cpp: In function `int main ()':
malloc.cpp:8: cannot convert `void *' to `char *' in assignment
[root@lin1 tmp]# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.2 2.96-108.1)
[root@lin1 tmp]#
 
A

akarl

ceo said:
hi,

why do i get the following error? do i need to explicitly typecast the
pointer returned by malloc to char *?

thanks,
ceo

[root@lin1 tmp]# cat malloc.cpp
#include <stdio.h>
#include <malloc.h>
#include <string.h>

int main() {

char *s = NULL;
s = malloc(sizeof( char) * 10);
strcpy(s, "blah blah");
printf("\n%s\n", s);
return 0;
}

[root@lin1 tmp]# gcc malloc.cpp
malloc.cpp: In function `int main ()':
malloc.cpp:8: cannot convert `void *' to `char *' in assignment
[root@lin1 tmp]# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.2 2.96-108.1)
[root@lin1 tmp]#

Hint: In which header file is malloc declared?
 
C

ceo

Hint: In which header file is malloc declared?

hello akarl,

i get the same error if i include stdlib.h if that's what you meant.

thanks,
ceo

[root@lin1 tmp]# cat malloc.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

char *s = NULL;
s = malloc(sizeof( char) * 10);
strcpy(s, "blah blah");
printf("\n%s\n", s);
return 0;
}

[root@lin1 tmp]# gcc malloc.cpp
malloc.cpp: In function `int main ()':
malloc.cpp:8: cannot convert `void *' to `char *' in assignment
[root@lin1 tmp]# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.2 2.96-108.1)
[root@lin1 tmp]#
 
N

Norihiro Kamae

Because it was compiled as an C++ code.

If you change the file name "malloc.cpp" to "malloc.c",
add typecast operator, or
use new[] operator, you can avoid that error.

But if you compile C++ code (if you try 2nd or 3rd one) with "gcc" or
"cc" command
you will get a link error. To avoid this error you should compile with
"g++" or "c++"

thanks,
Norihiro Kamae in Japan
 
R

Raymond Martineau

hi,

why do i get the following error? do i need to explicitly typecast the
pointer returned by malloc to char *?

thanks,
ceo

[root@lin1 tmp]# cat malloc.cpp

That's the problem right here: you're programming in C++, not C. As you
just discovered, C++ has strict typechecking, making it incompatable with
some C constructs.
#include <stdio.h>
#include <malloc.h>
#include <string.h>

int main() {

char *s = NULL;
s = malloc(sizeof( char) * 10);

In C, the statement is perfectly valid.
strcpy(s, "blah blah");
printf("\n%s\n", s);
return 0;
}

[root@lin1 tmp]# gcc malloc.cpp
malloc.cpp: In function `int main ()':
malloc.cpp:8: cannot convert `void *' to `char *' in assignment
[root@lin1 tmp]# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.2 2.96-108.1)
[root@lin1 tmp]#
 
J

Jirka Klaue

ceo:
please pardon my stupidity

1. no problem
2. it was a quote (google for "Dan Pop")
3. you deserved it (you are root without knowing what you are doing)
4. I answered question. Well, what else do you want? :)

Jirka
 
E

E. Robert Tisdale

ceo said:
Why do I get the following error?
Do I need to explicitly typecast the
pointer returned by malloc to char*?

Yes, if you compile with both C an C++ compilers.
> cat malloc.cpp
#include <stdio.h>
#include <malloc.h>
#include <string.h>

int main(int argc, char* argv[]) {

char* s = (char*)malloc(10*sizeof(char));
strcpy(s, "blah blah");
fprintf(stdout, "\n%s\n", s);
return 0;
}
> gcc -x c -Wall -std=c99 -pedantic -o malloc malloc.cpp
> ./malloc

blah blah
> g++ -Wall -ansi -pedantic -o malloc malloc.cpp
> ./malloc

blah blah
 
M

Martin Ambuhl

ceo said:
hi,

why do i get the following error?

Because you are using 'cpp' as the extension, telling the compiler that
it should compile C++ code, not C code.
do i need to explicitly typecast the
pointer returned by malloc to char *?

Not if you invoke your compiler as a C compiler.
[...]
[root@lin1 tmp]# gcc malloc.cpp
^^^
 
K

Keith Thompson

E. Robert Tisdale said:
Yes, if you compile with both C an C++ compilers.
[snip]

And there is rarely any good reason to do that.

No, you do not need to cast the result of malloc in a C program; doing
so can mask the error of forgetting to "#include <stdlib.h>". Some
compilers will diagnose this error anyway, but it's unwise to depend
on this.

If you're compiling with a C++ compiler, you're programming in C++,
and this is the wrong newsgroup.
 
E

E. Robert Tisdale

Keith said:
And there is rarely any good reason to do that.

You code is generally more valuable
if it will compile a either C or C++.
We do this regularly.

C++ is gradually replacing C.
If you follow Keith's advice,
your code will eventually become obsolete.
You will be obliged to re-write it or discard it.
discuss those options with your employer/customer
and decide accordingly.
 
C

CBFalconer

E. Robert Tisdale said:
You code is generally more valuable if it will compile a either
C or C++. We do this regularly.

C++ is gradually replacing C. If you follow Keith's advice, your
code will eventually become obsolete. You will be obliged to
re-write it or discard it. discuss those options with your
employer/customer and decide accordingly.

ERT is known as Trollsdale around here, and his advice is very
rarely worthwhile. If you make your code C and C++ compatible, you
give up the advantages of both. C++ has provisions for linking to
C code, so there is no advantage to taking Trollsdales bad advice.
 
J

John Bode

E. Robert Tisdale said:
You code is generally more valuable
if it will compile a either C or C++.
We do this regularly.

You mean you regularly write code that's a ridiculously small subset of
C and C++? You don't use classes, or templates, or any stdio routines?
What, exactly, is the value of such a truncated language?

Of course, that's not what you really meant. You meant that code was
more valuable if it compiled as C++, period.
C++ is gradually replacing C.

Then why bother writing code that will compile as C++ *and* C? Why not
just write C++ and be done with it? Why go through the heartburn of
shoehorning C code into an incompatible compiler?

A pox on Bjarne for calling it C++ and not P like he was supposed to;
and another pox on him for not making a cleaner break with C. At least
then there wouldn't be so much temptation to conflate the two
languages.
If you follow Keith's advice,
your code will eventually become obsolete.
You will be obliged to re-write it or discard it.

Paradigms come and go, but legacy code is forever. I've seen code
written while Nixon was President that's still in service.
 
K

Keith Thompson

John Bode said:
E. Robert Tisdale wrote: [...]
You code is generally more valuable
if it will compile a either C or C++.
We do this regularly.

You mean you regularly write code that's a ridiculously small subset of
C and C++? You don't use classes, or templates, or any stdio routines?
What, exactly, is the value of such a truncated language?

To be fair, C's <stdio.h> interface is included in C++ (I think the
header name is something like <cstdio>). The intersection of C90 and
C++ is actually pretty close to full C.

This doesn't imply that restricting yourself to that intersection is
worthwhile.
 
C

Christopher Benson-Manica

John Bode said:
Paradigms come and go, but legacy code is forever. I've seen code
written while Nixon was President that's still in service.

It must have been really great code :)
 
J

John Bode

Christopher said:
It must have been really great code :)

As Fortran IV goes, it really was well-written, although that
120-branch computed GOTO was kind of a shock.
 
R

REH

Keith said:
To be fair, C's <stdio.h> interface is included in C++ (I think the
header name is something like <cstdio>). The intersection of C90 and
C++ is actually pretty close to full C.
Both are included. The difference is <cstdio> puts everything in the
std namespace, while <stdio.h> does not. I believe that the later is
depricated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

malloc and maximum size 56
malloc 24
Undefined Reference to Main 2
array-size/malloc limit and strlen() failure 26
malloc() and implicit cast 7
bug in gcc? 17
Qry : Behaviour of fgets -- ? 345
A question on malloc() and calloc() 13

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top