Solved

reinterpret_cast

Posted on 2003-12-10
5
966 Views
Last Modified: 2012-08-13
I was wondering why this casting does'nt work ...
Is there an other way to cast this... in C++
I know that it's working with
char ** p1 = (char **) elem1;    but this is C casting...

Help me


int compare(const void* elem1, const void* elem2)
{
      

      
      const char **p1 = reinterpret_cast< const char**>(elem1);

...

0
Comment
Question by:Prog_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 9911589
static_cast should work in almost any case. Not completly sure whether the const specifier doesn't allow for reinterpret_cast. If so you can remove the const'ness with const_cast.
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 125 total points
ID: 9911842
I think you need this:
--------8<--------
int compare(const void* elem1, const void* elem2)
{
      const char * const *p1 = reinterpret_cast<const char* const *>(elem1);
--------8<--------

const void* elem1 means that elem1 is a pointer to something const (as opposed to a const pointer). If you reinterpret that as an array of char pointers to something const, you are casting off the const-ness of elem1. You have to say that the contents of the array are const.

If you want to cast off const-ness, you need to use a const cast.
--------8<--------
int compare(const void* elem1, const void* elem2)
{
      const char** p1 = reinterpret_cast<const char**>(const_cast<void*>(elem1));
--------8<--------
0
 
LVL 1

Expert Comment

by:travd
ID: 9914592
By trying the cast from <const void *> to <const char **> you are trying to cast away the constness since it is the value pointed to be the first dereference of elem1 that is const.  Instead you should be casting to <char * const *>.  Also you should probably use static_cast, since the semantics of this cast are more well defined and guaranteed to be portable (although in this case reinterpret_cast is probably equivalent).

const char * const *p1 = static_cast< char * const * >(elem1);

Try that.
0
 

Expert Comment

by:MAYURNATH
ID: 9970747

reinterpret_cast - internally interpret the pointers ( source pointer and target pointer ) and while compile time it embeds extra code if neccessary.

Here your treatment on pointers is converting from void* to char **. Which are not compatable

You are trying to copy only the 4 bytes of pointer data ( ex. 0x023FD2F) to the target pointer.

 const char **p1 = reinterpret_cast< const char**>(elem1);  - this casting should not be used here

reinterpret_cast is used when some situations like virtual pointer hirerchy casting
For only pointer copy it is not neccessary an extra interpretation is neccessary by the compiler then this casting is supportable

0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9971142
void* is a strange beast. It is a pointer to something unspecified. There is nothing wrong with saying that the unspecified thing is a char**, in fact we'd be lost if we weren't able to typecast void* in thread code.

You can do the following with a static_cast per the standard. I was surprised that the standard does not guarantee reinterpret_cast to work for void* too, but it does indeed say that it is implementation specific. Neither static nor reinterpret casts cast off constness though.

---------8<--------
#include <cstdio>
#include <memory>

int main()
{
      // malloc() - more comfortable in C code, but
      // what the hell. It is a simple example of
      // something that returns a void*, when you
      // really mean it to return a pointer to something
      // different
      void *ptr = malloc(sizeof(char*) * 2);

      // However, we were allocating an array of 2 char* s
      char **arr = static_cast<char**>(ptr);

      // Just to show it works
      arr[0] = "one";
      arr[1] = "two";
      for (int i = 0;i < 2;i++)
            printf("String %d is \"%s\"\n",i+1,arr[i]);

      free(ptr);
}
---------8<--------

NB: it looks odd written like this, because you are more used to seeing a C cast in C-ish C++.

      char** arr = (char**)malloc(sizeof(char*) * 2);
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java inner class, for the sole use of parameter passing 21 78
Compile GLUT with Visual Studio 2015 1 205
IdTCPClient1->Disconnect(); not working 3 86
boost::uuid crashes 17 36
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question