[英]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. 因此,我在堆栈上有10个char*
指针。 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 . 但是,它在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*
. 在array
每个索引处都有一个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 用g ++ -g -Wall编译
What am I doing wrong? 我究竟做错了什么? Compiling with g++ -g -Wall 用g ++ -g -Wall编译
g++ always compile a .c
file as .cpp
. g ++始终将.c
文件编译为.cpp
。 Compile it with a C compiler (like GCC). 使用C编译器(例如GCC)进行编译。 In C++, you must have to cast the return value of malloc
. 在C ++中,必须malloc
的返回值。 In case of C, do not cast return value of malloc
. 在C的情况下, 请勿malloc
返回值 。
Your code is valid C, but you are compiling your code as C++, which, unike C, has no implicit conversion from void*
to char*
. 您的代码是有效的C,但是您正在将代码编译为C ++,与C一样,它没有从void*
到char*
隐式转换。
If you intended to compile the code as C (in which case you do not require the cast), use gcc
, instead of g++
. 如果打算将代码编译为C(在这种情况下不需要强制转换),请使用gcc
而不是g++
。 Also make sure you your file does not end with an extension that gcc
interprets as C++ ( .cpp
, .C
, .cxx
or .cc
). 还要确保您的文件没有以gcc
解释为C ++的扩展名结尾( .cpp
, .C
, .cxx
或.cc
)。 Or play it safe and use the .c
extension. 或安全播放并使用.c
扩展名。
If you want to make the code valid C++, you need to cast to char*
: 如果要使代码有效为C ++,则需要将其强制转换为char*
:
array[0] = (char*)malloc(sizeof(char)*6);
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. 这可能是C和C ++之间最明显的区别:C可以将malloc()
返回的void*
隐式转换为其他任何类型,C ++则不能。
Now, by compiling with g++
, or by using a .cpp
file name extension, you are compiling your code as C++ code, not C code. 现在,通过使用g++
进行编译,或者通过使用.cpp
文件扩展名,您正在将代码编译为C ++代码,而不是C代码。 Use gcc
instead and make sure that your source file ends with .c
, and your code will compile fine. 请改用gcc
并确保您的源文件以.c
结尾,并且您的代码可以正常编译。
An alternative solution is to add the cast that C++ requires: array[0] = static_cast<char*>(malloc(sizeof(char)*6));
另一种解决方案是添加C ++所需的array[0] = static_cast<char*>(malloc(sizeof(char)*6));
: array[0] = static_cast<char*>(malloc(sizeof(char)*6));
As others have pointed out, C++ does not allow an implicit conversion from void *
to char *
. 正如其他人指出的那样,C ++不允许从void *
到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 *
: 如果确实是C ++代码,我建议使用new
而不是malloc
进行动态内存分配,对于此特定代码,我建议使用string
vector
而不是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. string
和vector
实现为您完成所有内存管理。
If this is really supposed to be C code, simply compile it using gcc
instead of g++
. 如果确实应该使用C代码,则只需使用gcc
而不是g++
对其进行gcc
。
Try something like this: 尝试这样的事情:
array[0] = static_cast<char *>(malloc(sizeof(char)*6));
How should I cast the result of malloc in C++? 如何在C ++中强制转换malloc的结果?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.