Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typecasting of pointers in C

Tags:

I know a pointer to one type may be converted to a pointer of another type. I have three questions:

  1. What should kept in mind while typecasting pointers?
  2. What are the exceptions/error may come in resulting pointer?
  3. What are best practices to avoid exceptions/errors?
like image 893
user1871762 Avatar asked Jan 17 '13 07:01

user1871762


People also ask

What is cast pointer?

Pointer casting is usually used when you want to access the bit stream pointed by one type of pointer and read it as something else. The requirements have to be specific enough and you have to be really careful when doing this. When the destination pointer type points to character type, it's okay to use it.

Can we type cast pointers?

We saw that pointer values may be assigned to pointers of same type. However, pointers may be type cast from one type to another type. In the following code lines, A is an int type variable, D is variable of type double, and ch is a variable of type char.

What happens when you cast a pointer?

A pointer is an arrow that points to an address in memory, with a label indicating the type of the value. The address indicates where to look and the type indicates what to take. Casting the pointer changes the label on the arrow but not where the arrow points.

How do I cast a pointer to int?

An object pointer (including void* ) or function pointer can be converted to an integer type using reinterpret_cast . This will only compile if the destination type is long enough. The result is implementation-defined and typically yields the numeric address of the byte in memory that the pointer pointers to.


1 Answers

A program well written usually does not use much pointer typecasting. There could be a need to use ptr typecast for malloc for instance (declared (void *)malloc(...)), but it is not even necessary in C (while a few compilers may complain).

  int *p = malloc(sizeof(int)); // no need of (int *)malloc(...)

However in system applications, sometimes you want to use a trick to perform binary or specific operation - and C, a language close to the machine structure, is convenient for that. For instance say you want to analyze the binary structure of a double (that follows thee IEEE 754 implementation), and working with binary elements is simpler, you may declare

  typedef unsigned char byte;
  double d = 0.9;
  byte *p = (byte *)&d;
  int i;
  for (i=0 ; i<sizeof(double) ; i++) { ... work with b ... }

You may also use an union, this is an exemple.

A more complex utilisation could be the simulation of the C++ polymorphism, that requires to store the "classes" (structures) hierarchy somewhere to remember what is what, and perform pointer typecasting to have, for instance, a parent "class" pointer variable to point at some time to a derived class (see the C++ link also)

  CRectangle rect;
  CPolygon *p = (CPolygon *)&rect;
  p->whatami = POLY_RECTANGLE; // a way to simulate polymorphism ...
  process_poly ( p );

But in this case, maybe it's better to directly use C++!

Pointer typecast is to be used carefully for well determined situations that are part of the program analysis - before development starts.

Pointer typecast potential dangers

  • use them when it's not necessary - that is error prone and complexifies the program
  • pointing to an object of different size that may lead to an access overflow, wrong result...
  • pointer to two different structures like s1 *p = (s1 *)&s2; : relying on their size and alignment may lead to an error

(But to be fair, a skilled C programmer wouldn't commit the above mistakes...)

Best practice

  • use them only if you do need them, and comment the part well that explains why it is necessary
  • know what you are doing - again a skilled programmer may use tons of pointer typecasts without fail, i.e. don't try and see, it may work on such system / version / OS, and may not work on another one
like image 131
Déjà vu Avatar answered Sep 19 '22 19:09

Déjà vu