-4

This my code.

struct MemoryStruct {
  char *memory;
  size_t size;
};

 struct MemoryStruct *mem = &state->characters;

  mem->memory = realloc(mem->memory, mem->size + len + 1);

When I run this code in 'C' and compile it by gcc, I didn't get any error.

But same code, I run in C++ and compile it by g++, I am getting error as

error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]

Why It did give error in C++ and not giving error in C

CinCout
  • 9,486
  • 12
  • 49
  • 67
Smith Dwayne
  • 2,675
  • 8
  • 46
  • 75
  • 12
    Because `C` is not the same as `C++` – jboockmann May 03 '16 at 08:07
  • 2
    Please search within SO, this topic is rather well covered [here](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) and [here](http://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc). In C casting `malloc` "family" result is _type redundant_, whereas in C++ it isn't. – user3078414 May 03 '16 at 09:00

4 Answers4

9

Why It did give error in C++ and not giving error in C

Because the rules of these languages are different. In this particular case, the difference is that C++ does not allow a conversion from void* to char*, whereas C does. That is what the compiler error is telling you.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
3

C++ give an error on this because there is no implicit conversion from void* to char* (at least in recent compilers).

If you want this code to compile in C++, you must cast to the right type:

mem->memory = static_cast<char*>(realloc(mem->memory, mem->size + len + 1));

or, if you want to keep C compatibility:

mem->memory = (char*)realloc(mem->memory, mem->size + len + 1);

But if you switch to C++, it is better to use C++ objects, like std::vector which will do the realloc for you.

PierreL
  • 63
  • 4
2

In C++ you need to cast the values returned by malloc and friends:

 mem->memory = (char*)realloc(mem->memory, mem->size + len + 1);
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 2
    No, in C++ you shouldn't use malloc to begin with, because it isn't compatible with `new` and `delete`. Preferably you should use a container class which handles the allocation internally. And if you for some reason still use `realloc`, you also need to cast the first parameter, if it is not `void*`, which it isn't in this case. – Lundin May 03 '16 at 08:54
  • @Lundin I don't agree with your last statement. Explicitly casting a pointer to `void*` would be pointless. – eerorika May 03 '16 at 09:02
  • @user2079303 Tough luck then, because C++ requires it. – Lundin May 03 '16 at 09:03
  • 1
    @Lundin no it doesn't. See `[conv.ptr]/2` (of n4582 in case the numbering has changed) – eerorika May 03 '16 at 09:05
1

Simple is that C and C++ are both different Languages C inspire structs however C++ inspire struct as well as Object oriented paradigm in your case c have ability to convert void* to char* builtin property but c++ dont have this type of property c++ cant convert void* to char* so thats why compiler throw an error.

Tara
  • 692
  • 5
  • 23