Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!

I have following code in my C++ project:
object *d = reinterpret_cast<object *>(it -> second);
d -> type = MINIGAMEZ_TYPE_OBJECT;
d -> value = map<string, void *>();


where it -> second is memory allocated with the help of malloc().
After that this memory is being used by another functions in my code. When testing with valgrind it says that I am using uninitialized values. Since this is only error in my project I would like to get rid of it.
Posted
Updated 16-Aug-10 4:53am
v3

Quote: "Since this is only error in my project I would like to get rid of it. ".

And what exactly do you expect us to do to make that happen? If you are using a value or values that have not been initialised then you need to fix your program to correct it. We cannot guess where the error may be.
 
Share this answer
 
Comments
Nyarost 16-Aug-10 3:54am    
Wait wait wait!

I initialize values:
d -> type = MINIGAMEZ_TYPE_OBJECT;
d -> value = map<string, void *>();


Without this really doesn't work. Segmentation fault appears and so on. With this two lines everything works just great. No errors, no leaks, no faults. Only valgrind report.
The following answer was posted looking at the original code the user posted. It's pretty meaningless as he's changed the code.


It wouldn't surprise me if the problem was in the first line of the snippet you've posted. You're casting a pointer to an object. Try replacing the reinterpret_cast with a static_cast and then fixing what the compiler tells you is wrong.

[Actually on most of the compiler I use I doubt the first line would compile on high warnings and treat warnings as errors (/W4 /WX). Try cranking the warning level up a bit and see what happens.]

Oh, and don't use malloc unless a C library you're linking with requires the memory to be malloced.

Cheers,

Ash
 
Share this answer
 
v4
Comments
Nyarost 16-Aug-10 4:34am    
Thanks for your answer!

About all the things you've mentioned:
1. I have -Wall and -Werror flags set in gcc. It compiles.
2. malloc is only choise here :(

Konstantin
Aescleal 16-Aug-10 9:40am    
I doubt malloc is the only choice - if it is how're std::string and std::map working?

Did fixing the reinterpret_cast do anything?

PS: Just tested the code you presented on gcc 3.4.4 and got the error:

invalid reinterpret_cast from type `void*' to type `object'

at the first line which I expected from reading your code.
Nyarost 16-Aug-10 10:41am    
I use gcc 4.4.3 and it is ok for me. If i use memcpy, the memory is said to be initialixed. If I init object members `by hand` valgrind gives an error. I have std::map which contains a lot of objects of mixed type: so i use void *. When I need to place an object in this map, I allocate memory and memcpy the data or in case of `object` type initialize values.
Nyarost 16-Aug-10 10:45am    
Actually it is strange that gcc doesn't compile cause reinterpret_cast changes one data type into another. It should be used to cast between incompatible pointer types.

http://www.cppreference.com/wiki/keywords/reinterpret_cast
Nyarost 16-Aug-10 10:53am    
Noticed incorrectness in posted code. There should be object * instead of object. That's why your gcc throws an error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900