1

Is there a difference in performance for casting to and from a void * instead of casting to some base struct? This is in part to achieve a semi-dynamic typing system.

Here is my code:

#include "stdlib.h"

typedef struct
{
  unsigned char type;
} Dyn;

typedef struct
{
  unsigned char type;
  int number;
} Num;

typedef struct
{
  unsigned char type;
  char letter;
} Lett;

int main(void) {
  Num* n = (Num *)malloc(sizeof(Num));

  void* hold = n; // to void * casting
  Num* n_g = (Num *)hold;

  Dyn* dhold = (Dyn *)n; // to base struct casting
  Num* n_d = (Num *)dhold;
  return 0;
}

So in terms of pointers to structs, is there any performance difference between using void * or a base type struct?

Stargateur
  • 24,473
  • 8
  • 65
  • 91
Josh Weinstein
  • 2,788
  • 2
  • 21
  • 38
  • Performance considerations necessarily appertain to a particular compiler, platform and settings , you would need to include more details. And maybe you could answer your question by comparing generated assembly between the different methods – M.M Jul 03 '17 at 04:18
  • Ah this would be for gcc, but i see what your saying. – Josh Weinstein Jul 03 '17 at 04:31
  • I agree with M.M, to talk about performance about cast you need to provide the operating system and materiel, most of the time this operation is free. By the way, use `void *` to build this kind of feature is anti pattern in C. – Stargateur Jul 03 '17 at 04:38
  • This would ideally meant for cross-platform usage as it's part of an interpreter, but if there isn't a significant difference between say Linux or OSX then it's fine. I thought this might have some overheard since converting int's to floats can be expensive. – Josh Weinstein Jul 03 '17 at 04:41
  • @JoshWeinstein: avoid commenting your own question, but do **edit your question** to improve it – Basile Starynkevitch Jul 03 '17 at 04:42
  • @M.M Considering that the whole C is essentially - in practice - an asm macro language with compile-time type checking, I would say - except *very* exotic architectures - having a pointer to *anything* is the same from the asm / performance view. – peterh Jul 03 '17 at 04:48
  • @peterh I would also expect identical performance, but that's a hypothesis, I'm suggesting OP test it out – M.M Jul 03 '17 at 04:51
  • You shouldn't cast the return value of `malloc`! https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Toby Jul 03 '17 at 09:32

1 Answers1

3

is there a difference in performance for casting to and from a void* instead of casting to some base struct?

Let's assume that:

  • your target platform is some common processor like x86, x86-64, ARM, Aarch, PowerPC (either 32 or 64 bits)

  • you are using a good optimizing compiler (like GCC or Clang/LLVM)

  • you have enabled some optimizations (so you compile with -O1 or -O2 at least)

Then pointers are internally represented as addresses, and you should not have any difference in performance. Study the generated assembler code (with gcc -O1 -S -fverbose-asm ....) to be sure.

However, you could consider using some union (of pointers, or better yet a pointer to union of struct).

BTW, I would recommend studying the source code of some existing free software interpreters (e.g. guile, lua, siod) and reading a good book about them (e.g. Lisp In Small Pieces by Queinnec)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547