473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

invalid conversion from void* to int**

Hi,

I'm using this alloc_mem-function:

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

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 + gcc doesn't complain. But with g++ I get such an
error as:

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

I was told I should ask about the g++ compiler here, although the code
is actually implemented in a C-program. So what's the language
difference - why can't I do an "implicit conversion from void* to int**"
in C++ ?
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 14 '06 #1
11 22020
Martin Jørgensen wrote:
Hi,

I'm using this alloc_mem-function:

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

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 + gcc doesn't complain. But with g++ I get such an
error as:

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

I was told I should ask about the g++ compiler here, although the code
is actually implemented in a C-program.
Don't use g++ for C programs. g++ is the C++ compiler.
So what's the language difference -
You didn't really think C and C++ were exactly the same, did you?
why can't I do an "implicit conversion
from void* to int**" in C++ ?


Because that's how the language is defined. Conversions from void* to any
other object pointer always need a cast.

May 14 '06 #2
Rolf Magnus wrote:
Martin Jørgensen wrote: -snip-
Don't use g++ for C programs. g++ is the C++ compiler.


That wasn't the question.
So what's the language difference -

You didn't really think C and C++ were exactly the same, did you?


That's a ridiculous comment.
why can't I do an "implicit conversion
from void* to int**" in C++ ?

Because that's how the language is defined. Conversions from void* to any
other object pointer always need a cast.


So void* isn't any generic pointer type in C++.
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 14 '06 #3
Martin Jorgensen wrote:
So void* isn't any generic pointer type in C++.


The best way to think of void* is "a thing that can safely store any
pointer's value". It's not really a pointer because it doesn't point to a
type, so you can't dereference it.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 14 '06 #4
Martin Jørgensen wrote:

So void* isn't any generic pointer type in C++.

It is, but not in the same way is in C. C++ doesn't support the
implicit conversion from void* to other pointer types.

--
Ian Collins.
May 15 '06 #5
Martin Jørgensen wrote:
Rolf Magnus wrote:
Martin Jørgensen wrote:

-snip-
Don't use g++ for C programs. g++ is the C++ compiler.


That wasn't the question.


My advice was just an extra give-away for free. Since you wrote that the
code is C, you shouldn't use a C++ compiler to compile it.
So what's the language difference -

You didn't really think C and C++ were exactly the same, did you?


That's a ridiculous comment.


From what you wrote, it looked to me as if you thought there shouldn't be
any differences between C and C++.
why can't I do an "implicit conversion
from void* to int**" in C++ ?

Because that's how the language is defined. Conversions from void* to any
other object pointer always need a cast.


So void* isn't any generic pointer type in C++.


Wrong. It is, though it's needed far less than in C. For type safety, you
must explicitly tell the compiler, which type you want it to be converted
to. I fail to see how that could mean it's not a generic pointer type
anymore.

May 15 '06 #6
Phlip wrote:
Martin Jorgensen wrote:

So void* isn't any generic pointer type in C++.

The best way to think of void* is "a thing that can safely store any
pointer's value". It's not really a pointer because it doesn't point to a
type, so you can't dereference it.


But it points to something, so it's a pointer... But I know that about
the dereferencing stuff. Ok, so there isn't really much else to say
about that I guess...
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 15 '06 #7
Martin Jørgensen wrote:
But it points to something, so it's a pointer...


Yes, and The Standard calls it a pointer, etc. Just don't think of it as
one. ;-)

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 15 '06 #8
Martin Jørgensen wrote:

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);
Why not just use 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);
What is the purpose of storing that size there?
How are you planning to access it in future?
*total_mem += size; /* update total memory allocated untill now */
You should check here , and in the mutliplication above,
that you don't overflow a size_t .

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;
Those are pointers, not arrays.
etc. etc...

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

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


You didn't post any code that would give that error.

May 16 '06 #9
Old Wolf wrote:
Martin Jørgensen wrote:
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);

Why not just use sizeof(size_t) ?


Actually I just implemented it after reading the suggestion from
somebody else. But the point is to store the allocated memory in the end
of each block. I think it has some technical explanation, something to
do with "off-numbered" memory address of whatever... I think somebody
else can explain it - nobody complained about it in comp.lang.c so I
think it's okay.

If we think about it, the least extra memory occupation must be sizeof
(size_t). If elem_size is larger than that, we reserve memory for
elem_size and I think it's got to do with hitting some memory boundary
address - anyone?
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);

What is the purpose of storing that size there?
How are you planning to access it in future?


size_t retrieve_memsize (void *mem, size_t num_elems,
size_t elem_size)
{
size_t size = 0;
if (mem) {
memcpy(&size, ((char *)mem)+num_elems*elem_size,
sizeof size);
}
return size;
}

void free_mem (void *mem, size_t num_elems,
size_t elem_size, size_t *total_mem)
{
if (mem) {
size_t size = retrieve_memsize(mem, num_elems, elem_size);
free(mem);
*total_mem -= size;
}
}
*total_mem += size; /* update total memory allocated untill now */

You should check here , and in the mutliplication above,
that you don't overflow a size_t .


What do you mean? How should it overflow?
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;

Those are pointers, not arrays.


You're right. I was thinking of what they pointed to instead.
etc. etc...

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

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

You didn't post any code that would give that error.


It sounds like that, from the other replies in this thread...
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 16 '06 #10
Martin Jørgensen wrote:
*total_mem += size; /* update total memory allocated untill now */


You should check here , and in the mutliplication above,
that you don't overflow a size_t .


What do you mean? How should it overflow?


Well, if *total_mem is currently 0xFFFFFF00, and you try to add a
size of 0x200 to it, then *total_mem will now be 0x00000100 .

Also, the allocation function performs a multiplication. If someone
requests 0x80000002 * 2 bytes, then your function will happily
return them an allocation of 4 bytes.

(All this assumes we're using a 32-bit size_t).

May 16 '06 #11
Old Wolf wrote:
Martin Jørgensen wrote:

*total_mem += size; /* update total memory allocated untill now */

You should check here , and in the mutliplication above,
that you don't overflow a size_t .


What do you mean? How should it overflow?

Well, if *total_mem is currently 0xFFFFFF00, and you try to add a
size of 0x200 to it, then *total_mem will now be 0x00000100 .

Also, the allocation function performs a multiplication. If someone
requests 0x80000002 * 2 bytes, then your function will happily
return them an allocation of 4 bytes.

(All this assumes we're using a 32-bit size_t).


I don't understand the point. I've used up to about 1,4 GB of memory
with this code. The machine had 2 GB of RAM total. I don't need anymore
at this moment.

However, I would like to see how else this size_t should/could be
defined because it's probably only a matter of time before computers are
shipped with 4 GB of memory instead of 1-2 GB.
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 17 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
6
by: Thomas Barth | last post by:
Hi, I'm new to windows programming and still reading a book about windows-programming with C++. I copied the following code from the book into my ide (Eclipse/CDT) to comprehend the code, but two...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
3
by: philwozza | last post by:
Im trying to implement a THREAD class that encapsulates a posix thread. Here is an outline of my THREAD class. class THREAD { public: // returns 1 if thread sucessfully started int...
5
by: Martin Jørgensen | last post by:
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,...
2
by: tkirankumar | last post by:
Hi all, uname -a SunOS cbmrsd1a1 5.10 Generic_118833-17 sun4us sparc FJSV,GPUZC-M g++ -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.10/3.3.2/specs Configured with:...
3
by: fazulu deen | last post by:
Hi all, For the following code : file_ptr = fopen("pass_fail.txt", "a"); // error line 393 fdisplay(file_ptr, "Test Passed"); fclose(file_ptr);
1
by: Nozdormu | last post by:
Hi guys, I have a quick question. The language I'm using is C. The question I'm tackling: Write a program that (1) defines a 1D array with ten int elements, and (2) sets the values of the...
3
by: Tex08 | last post by:
I have hit a roadblock on a class project. Trying to implement the composite pattern with separate classes to represent a boolean expression. My literal class will not compile (g++, required...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.