0

I have a very simple problem that I cannot seem to figure out. I have this:

char* array[10];

So, I then have 10 char* pointers on the stack. Now all I want to do is allocate memory for each pointer. As in:

array[0] = malloc(sizeof(char)*6);

And then store some characters at this location:

strncpy(array[0], "hello", sizeof("hello")); 

Yet, I am getting a compile-time error at the first step of allocating the memory:

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

But it works as expected at Ideone.

What am I doing wrong? I understand what I am trying to do, but I do not understand why it does not work. At each index in array there is a char*. By using the = symbol I am trying to assign each pointer to a block of memory allocated to it.

What am I doing wrong? Compiling with g++ -g -Wall

sherrellbc
  • 4,650
  • 9
  • 48
  • 77
  • 3
    Your question should be tagged c++. g++ is used with c++ files. Your code is working if you try to compile it as pure c code. – Igor Pejic Aug 27 '14 at 16:34
  • @Igor, What is the difference between using gcc and g++ for this snippet of code? – sherrellbc Aug 27 '14 at 16:35
  • 2
    G++ is a C++ compiler and C++ compilers demand a cast to convert from `void *` to an `AnythingElse *` (such as `char *`). C compilers don't demand the cast though they do accept one if it is written as a C style cast. – Jonathan Leffler Aug 27 '14 at 16:36
  • C promotes a pointer to the needed type automatically. C++ does not. Take a look at [this](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) popular question. – Igor Pejic Aug 27 '14 at 16:37
  • It is at least somewhat tempting to retitle the question along the lines of "Why does a C++ compiler reject code that a C compiler accepts?" – Jonathan Leffler Aug 27 '14 at 16:38

5 Answers5

2

What am I doing wrong? Compiling with g++ -g -Wall

g++ always compile a .c file as .cpp. Compile it with a C compiler (like GCC). In C++, you must have to cast the return value of malloc. In case of C, do not cast return value of malloc.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
1

Your code is valid C, but you are compiling your code as C++, which, unike C, has no implicit conversion from void* to char*.

If you intended to compile the code as C (in which case you do not require the cast), use gcc, instead of g++. Also make sure you your file does not end with an extension that gcc interprets as C++ (.cpp, .C, .cxx or .cc). Or play it safe and use the .c extension.


If you want to make the code valid C++, you need to cast to char*:

array[0] = (char*)malloc(sizeof(char)*6);
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

This is probably the most visible difference between C and C++: C can implicitely convert the void* returned by malloc() to any other type, C++ can't.

Now, by compiling with g++, or by using a .cpp file name extension, you are compiling your code as C++ code, not C code. Use gcc instead and make sure that your source file ends with .c, and your code will compile fine.

An alternative solution is to add the cast that C++ requires: array[0] = static_cast<char*>(malloc(sizeof(char)*6));

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
0

As others have pointed out, C++ does not allow an implicit conversion from void * to char *.

If this is really supposed to be C++ code, I'd advise using new instead of malloc for dynamic memory allocation, and for this particular code I'd advise using a vector of string instead of an array of char *:

#include <vector>
#include <string>
...
std::vector< std::string > array;
...
array[0] = "hello";  // literal is implicitly converted to an instance of string

The string and vector implementations do all the memory management for you.

If this is really supposed to be C code, simply compile it using gcc instead of g++.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

Try something like this:

array[0] = static_cast<char *>(malloc(sizeof(char)*6));

How should I cast the result of malloc in C++?

Community
  • 1
  • 1
Keroronsk
  • 120
  • 1
  • 5