invalid conversion from void* to int**

Discussion in 'C Programming' started by =?ISO-8859-1?Q?Martin_J=F8rgensen?=, May 14, 2006.

  1. Hi,

    I'm using this alloc_mem-function (by Michael Mair):

    - - - - - - - - - - - - - - - - - - - - - - - -

    void *alloc_mem (size_t num_elems, size_t elem_size,
    char *filename, int line,
    size_t *total_mem)
    {
    void *mem;
    size_t size = num_elems*elem_size;
    size += (sizeof (size_t) <= elem_size) ? elem_size
    : sizeof (size_t);
    mem = malloc(size);

    if (!mem)
    {
    fprintf(stderr, "%s: line %d, malloc(%lu) failed.\n",
    filename, line, (unsigned long) size);
    exit(EXIT_FAILURE);
    }

    /* save memory allocated for this pointer */
    memcpy(((char *)mem)+num_elems*elem_size,
    &size, sizeof size);
    *total_mem += size; /* update total memory allocated untill now */

    return mem;

    }
    - - - - - - - - - - - - - - - - - - - - - - - -

    But I then declared some arrays like:

    double **two_D_double;
    int **two_D_int;
    double *one_D_double;
    int *one_D_int;
    etc. etc...

    MS visual studio 2005 doesn't complain. But with g++ I get such an error as:

    "invalid conversion from void* to int**" (the same for double **)

    Is this a problem I can safely ignore? Because I have a strange error in
    my program somewhere and I'm wondering if this could be made any better?



    Best regards / Med venlig hilsen
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, May 14, 2006
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    Marc Thrun Guest

    Martin Jørgensen schrieb:
    > Hi,
    >
    > I'm using this alloc_mem-function (by Michael Mair):
    >
    > - - - - - - - - - - - - - - - - - - - - - - - -
    >
    > void *alloc_mem (size_t num_elems, size_t elem_size,
    > char *filename, int line,
    > size_t *total_mem)

    [snip]
    > - - - - - - - - - - - - - - - - - - - - - - - -
    >
    > But I then declared some arrays like:
    >
    > double **two_D_double;
    > int **two_D_int;
    > double *one_D_double;
    > int *one_D_int;
    > etc. etc...
    >
    > MS visual studio 2005 doesn't complain. But with g++ I get such an error
    > as:
    >
    > "invalid conversion from void* to int**" (the same for double **)
    >
    > Is this a problem I can safely ignore? Because I have a strange error in
    > my program somewhere and I'm wondering if this could be made any better?
    >
    >
    >
    > Best regards / Med venlig hilsen
    > Martin Jørgensen
    >


    g++ is a C++ compiler, C++ does not allow an implicit conversion from
    void* to any other pointer type. Try to use gcc.

    --
    Marc Thrun
    http://www.tekwarrior.de/
    Marc Thrun, May 14, 2006
    #2
    1. Advertising

  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    Flash Gordon Guest

    Martin Jørgensen wrote:

    <snip>

    > mem = malloc(size);


    <snip>

    > MS visual studio 2005 doesn't complain. But with g++ I get such an error
    > as:
    >
    > "invalid conversion from void* to int**" (the same for double **)
    >
    > Is this a problem I can safely ignore? Because I have a strange error in
    > my program somewhere and I'm wondering if this could be made any better?


    Try using a C (gcc) compiler instead of a C++ (g++) compiler. There are
    some significant differences between the two languages, and this is one
    of them.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc

    Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
    Flash Gordon, May 14, 2006
    #3
  4. Flash Gordon wrote:
    > Martin Jørgensen wrote:
    >
    > <snip>
    >
    >> mem = malloc(size);

    >
    >
    > <snip>
    >
    >> MS visual studio 2005 doesn't complain. But with g++ I get such an
    >> error as:
    >>
    >> "invalid conversion from void* to int**" (the same for double **)
    >>
    >> Is this a problem I can safely ignore? Because I have a strange error
    >> in my program somewhere and I'm wondering if this could be made any
    >> better?

    >
    >
    > Try using a C (gcc) compiler instead of a C++ (g++) compiler. There are
    > some significant differences between the two languages, and this is one
    > of them.


    Okay... I think it works... I assume there's no problem with the code
    then... I must have a bug some other place then... Thanks...


    Best regards / Med venlig hilsen
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, May 14, 2006
    #4
  5. Flash Gordon <> writes:
    > Martin Jørgensen wrote:
    >
    > <snip>
    >
    >> mem = malloc(size);

    >
    > <snip>
    >
    >> MS visual studio 2005 doesn't complain. But with g++ I get such an
    >> error as:
    >> "invalid conversion from void* to int**" (the same for double **)
    >> Is this a problem I can safely ignore? Because I have a strange
    >> error in my program somewhere and I'm wondering if this could be
    >> made any better?

    >
    > Try using a C (gcc) compiler instead of a C++ (g++) compiler. There
    > are some significant differences between the two languages, and this
    > is one of them.


    Or use g++, but ask about it in comp.lang.c++ rather than here.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, May 14, 2006
    #5
  6. Martin Jørgensen wrote:
    > Hi,
    >
    > I'm using this alloc_mem-function (by Michael Mair):
    >
    > - - - - - - - - - - - - - - - - - - - - - - - -
    >
    > void *alloc_mem (size_t num_elems, size_t elem_size,
    > char *filename, int line,
    > size_t *total_mem)
    > {
    > void *mem;
    > size_t size = num_elems*elem_size;
    > size += (sizeof (size_t) <= elem_size) ? elem_size
    > : sizeof (size_t);
    > mem = malloc(size);


    Why was it that we didn't just add sizeof(size_t) above (instead of
    doing this test: (sizeof (size_t) <= elem_size) ?

    > if (!mem)
    > {
    > fprintf(stderr, "%s: line %d, malloc(%lu) failed.\n",
    > filename, line, (unsigned long) size);
    > exit(EXIT_FAILURE);
    > }
    >
    > /* save memory allocated for this pointer */
    > memcpy(((char *)mem)+num_elems*elem_size,
    > &size, sizeof size);
    > *total_mem += size; /* update total memory allocated untill now */
    >
    > return mem;
    >
    > }
    > - - - - - - - - - - - - - - - - - - - - - - - -


    See the question above...


    Best regards / Med venlig hilsen
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, May 16, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    501
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    778
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    815
    S.Tobias
    Jul 22, 2005
  4. philwozza
    Replies:
    3
    Views:
    3,500
    philwozza
    May 13, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    invalid conversion from void* to int**

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, May 14, 2006, in forum: C++
    Replies:
    11
    Views:
    7,162
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 17, 2006
Loading...

Share This Page