sherrellbc
sherrellbc

Reputation: 4853

Array of char* and how to allocate memory for each

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

Upvotes: 0

Views: 135

Answers (5)

Keroronsk
Keroronsk

Reputation: 120

Try something like this:

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

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

Upvotes: 0

John Bode
John Bode

Reputation: 123458

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++.

Upvotes: 0

juanchopanza
juanchopanza

Reputation: 227400

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);

Upvotes: 1

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));

Upvotes: 0

haccks
haccks

Reputation: 106012

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.

Upvotes: 2

Related Questions