[英]Return 2D char array from function in c++ and print it
This is my code and I want to return 2D dimension array [10][8] and [10][20] from my function, but i get an error.!这是我的代码,我想从我的 function 中返回二维数组 [10][8] 和 [10][20],但我得到一个错误。! (Segmentation fault). (分段故障)。
Please help me.. I need this for my project.请帮助我..我的项目需要这个。 Finally i want to print this array and I cant do this because of the error.最后我想打印这个数组,但由于错误我不能这样做。
Can someone help me fix this and print that?有人可以帮我解决这个问题并打印出来吗?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
char **getWords(int level)
{
if (level == 1)
{
char **words = new char *[8];
strcpy(words[0], "Pakistan");
strcpy(words[1], "Portugal");
strcpy(words[2], "Tanzania");
strcpy(words[3], "Thailand");
strcpy(words[4], "Zimbabwe");
strcpy(words[5], "Cameroon");
strcpy(words[6], "Colombia");
strcpy(words[7], "Ethiopia");
strcpy(words[8], "Honduras");
strcpy(words[9], "Maldives");
return words;
}
//For Hard Level
else if (level == 2)
{
char **words = (char **)malloc(sizeof(char *) * 20);
strcpy(words[0], "Tajikistan");
strcpy(words[1], "Uzbekistan");
strcpy(words[2], "Azerbaijan");
strcpy(words[3], "Bangladesh");
strcpy(words[4], "Luxembourg");
strcpy(words[5], "Madagascar");
strcpy(words[6], "Mauritania");
strcpy(words[7], "Montenegro");
strcpy(words[8], "Mozambique");
strcpy(words[9], "New Zealand");
return words;
}
}
int main()
{
getWords(1);
return 0;
}
By doing通过做
char **words = new char *[10];
You are only allocating memory for the pointers, not for the memory blocks where the actual strings are to be stored, you need to allocate memory for that too:您只为指针分配 memory ,而不是为要存储实际字符串的 memory 块分配 memory :
char **words = new char *[10]; //space for 10 pointers
for(int i = 0; i < 10; i++){
words[i] = new char[10]; // space for 10 characters each line, 8 is not enough
} // you need at least 9 because of the ending nul byte
strcpy(words[0], "Pakistan");
strcpy(words[1], "Portugal");
//...
In main, assing them to a pointer to pointer and print them as if it was an array of strings:在 main 中,将它们分配给指向指针的指针并打印它们,就好像它是一个字符串数组:
char** words = getWords(1);
for(int i = 0; i < 10; i++){
std::cout << words[i] << std::endl;
}
The same goes for the second part which uses malloc
.使用malloc
的第二部分也是如此。
char **words = (char**)malloc(sizeof *words * 10); //space for 10 pointers
for(int i = 0; i < 10; i++){
words[i] = (char*) malloc(20); //space for 20 characters each line
}
In a normal situation, when the program doesn't end right away you would have to free the memory:在正常情况下,当程序没有立即结束时,您必须释放 memory:
For memory allocated with new
:对于分配了new
的 memory :
for (int i = 0; i < 10; i++)
{
delete words[i];
}
delete words;
For the memory allocated with malloc
:对于分配有 malloc 的malloc
:
for(int i = 0; i < 10; i++)
{
free(words[i]);
}
free(words);
This can be tricky in your case because you return 2 types of memory allocation deppending on the option you pass as parameter, my advice is that you use that same option to choose how to deallocate the memory.这在您的情况下可能很棘手,因为您返回 2 种类型的 memory 分配取决于您作为参数传递的选项,我的建议是您使用相同的选项来选择如何解除分配 memory。
PS: Using C++ containers like std::vector
and std::string
would make your job easier, you wouldn't need to handle the memory yourself. PS:使用像std::vector
和std::string
这样的 C++ 容器会让你的工作更轻松,你不需要自己处理 memory。
Use of用于
char **words =new char*[8];
strcpy(words[0],"Pakistan");
is a problem since you have not allocated memory for words[0]
.是一个问题,因为您没有为words[0]
分配 memory 。
It's analogous to它类似于
char* cp; /// Uninitialized pointer
strcpy(cp,"Pakistan");
You'll have to allocate memory for words[0]
, words[1]
, etc. before you use them in the call to strcpy
.在调用strcpy
之前,您必须为words[0]
、 words[1]
等分配 memory 等。
More importantly, change your strategy, and use std::vector<std::sting>
instead.更重要的是,改变你的策略,改用std::vector<std::sting>
。 That makes your code simpler and removes the burden of allocating and dealloating memory from application code.这使您的代码更简单,并消除了从应用程序代码中分配和取消分配 memory 的负担。
std::vector<std::string> getWords(int level)
{
std::vector<std::string> words;
if (level==1)
{
words.push_backl("Pakistan");
// etc.
return words;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.