what is causing this error?

C

Chris

This is the code:
######################
#include <stdlib.h>
using namespace std;

class intlist {
unsigned int length;
int * list;
public:
intlist() {
length = 0;
list = (int *) malloc(length * sizeof(int)); }
~intlist() {
free(list); }

void append(int value) {
length += 1;
list = realloc(list, length * sizeof(int));
list[length-1] = value; } };

int main() {
intlist a;
return 0; }
######################
This is the Error:
In member function `void intlist::append(int)':
invalid conversion from `void*' to `int*'

Can anyone tell me why i am getting this error? I compiled it with
Bloodshed Dev-C++.

Thanks for any help!
-Chris
 
M

Mike Wahler

Chris said:
This is the code:
######################
#include <stdlib.h>
using namespace std;

class intlist {
unsigned int length;
int * list;
public:
intlist() {
length = 0;
list = (int *) malloc(length * sizeof(int)); }
~intlist() {
free(list); }

void append(int value) {
length += 1;
list = realloc(list, length * sizeof(int));
list[length-1] = value; } };

int main() {
intlist a;
return 0; }
######################
This is the Error:
In member function `void intlist::append(int)':
invalid conversion from `void*' to `int*'

Can anyone tell me why i am getting this error? I compiled it with
Bloodshed Dev-C++.

C++ (unlike C) does not allow implicit conversions from
type 'void *'. A cast is required. But let me add a
few points:

If your 'realloc()' call fails, you have a memory leak
(the pointer to the originally 'malloc()'-d memory will
be overwritten with NULL, so now you cannot 'free' it.
(IOW you should use a temporary pointer for 'realloc()'
and only overwrite the original pointer if it succeeds.)

With C++, memory should be dynamically alocated with the
'new' operator, not 'malloc()' (malloc() will not invoke
constructors, nor will 'free()' invoke destructors). This
might not make any difference right now with your 'int'
allocations, but if this type later gets changed to one
that depends upon ctor/dtor, things will go awry (and
not always in an obvious way).

When you do need to dynamically allocate memory, you
should wrap any raw pointers or use smart pointers.


-Mike
 
D

Default User

Mike said:
void append(int value) {
length += 1;
list = realloc(list, length * sizeof(int));
list[length-1] = value; } };
With C++, memory should be dynamically alocated with the
'new' operator, not 'malloc()' (malloc() will not invoke
constructors, nor will 'free()' invoke destructors). This
might not make any difference right now with your 'int'
allocations, but if this type later gets changed to one
that depends upon ctor/dtor, things will go awry (and
not always in an obvious way).

That would of course lead to more design changes, as he would have to
copy the data in some way. There's no equivalent to realloc() for
new'ed memory.
When you do need to dynamically allocate memory, you
should wrap any raw pointers or use smart pointers.

Yes, or just use an appropriate standard container, like vector.




Brian
 

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

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top