what is causing this error?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    #1

    what is causing this error?

    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

  • aleona3@gmail.com

    #2
    Re: what is causing this error?

    > list = realloc(list, length * sizeof(int));

    you need to cast this to int*.

    Comment

    • Manish

      #3
      Re: what is causing this error?

      realloc returns a void pointer to the reallocated (and possibly moved)
      memory block.

      http://msdn.microsoft.com/library/de...rt_realloc.asp

      Try it with the following change:

      list = (int * ) realloc(list, length * sizeof(int));

      Comment

      • Mike Wahler

        #4
        Re: what is causing this error?


        "Chris" <chrispatton@gm ail.com> wrote in message
        news:1137799393 .540444.25220@f 14g2000cwb.goog legroups.com...[color=blue]
        > 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++.[/color]

        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


        Comment

        • Default User

          #5
          Re: what is causing this error?

          Mike Wahler wrote:
          [color=blue]
          >
          > "Chris" <chrispatton@gm ail.com> wrote in message
          > news:1137799393 .540444.25220@f 14g2000cwb.goog legroups.com...[/color]
          [color=blue][color=green]
          > > void append(int value) {
          > > length += 1;
          > > list = realloc(list, length * sizeof(int));
          > > list[length-1] = value; } };[/color][/color]
          [color=blue]
          > 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).[/color]

          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.
          [color=blue]
          > When you do need to dynamically allocate memory, you
          > should wrap any raw pointers or use smart pointers.[/color]

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




          Brian

          Comment

          Working...