473,836 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_FAILU RE);
}

/* save memory allocated for this pointer */
memcpy(((char *)mem)+num_elem s*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 22103
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_FAILU RE);
}

/* save memory allocated for this pointer */
memcpy(((char *)mem)+num_elem s*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_FAILU RE);
}

/* save memory allocated for this pointer */
memcpy(((char *)mem)+num_elem s*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_FAILU RE);
}

/* save memory allocated for this pointer */
memcpy(((char *)mem)+num_elem s*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_memsiz e (void *mem, size_t num_elems,
size_t elem_size)
{
size_t size = 0;
if (mem) {
memcpy(&size, ((char *)mem)+num_elem s*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_memsiz e(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

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

Similar topics

30
5080
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 () { srand(time(0)); int array_length; int count;
6
4382
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 errors occur. In function `LRESULT WndProc(HWND__*, unsigned int, unsigned int, long int)': line 48: error: invalid conversion from `void*' to `HBRUSH__*' line 49: error: invalid conversion from `void*' to `HPEN__*'
2
6877
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 static explicit System.IntPtr (long);
3
30947
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 Start(void* = NULL); // other public functions
5
4065
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, size_t *total_mem) {
2
13456
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: ../configure --with-as=/usr/ccs/bin/as --with-ld=/usr/ccs/bin/ld --disable-nls
3
7178
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
16068
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 elements such that they are equal to the addresses of the elements, and (3) prints out the content of the elements. The program should call a user-defined function value_address with the following function prototype: void value_address( int ) My...
3
2567
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 complier) with the following error: error: invalid conversion from `int' to `Literal*' Here is my code: class BooleanExp { public: virtual ~BooleanExp();
0
9666
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10544
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10585
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9369
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7788
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6977
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.