invalid conversion from void* to int**

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
?

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

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
 
M

Marc Thrun

Martin said:
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.
 
F

Flash Gordon

Martin Jørgensen wrote:

mem = malloc(size);

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
 
?

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

Flash said:
Martin Jørgensen wrote:







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
 
K

Keith Thompson

Flash Gordon said:
Martin Jørgensen wrote:





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.
 
?

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

Martin said:
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
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top