在 c/c++ 中使用 malloc 在另一个函数中填充 char*
Fill a char* in another function using malloc in c/c++
我正在编写 Arduino (ESP8266),并且必须从文件中读取字符串才能使用它。我不知道那个文件有多长,所以我必须创建一个 char*
并将其传递给 readConf
函数,以便 malloc
决定内存大小。
void readConf(char path[], char **buff){
SPIFFS.begin();
if (SPIFFS.exists(path))
{
File file = SPIFFS.open(path, "r");
int size = file.size();
Serial.print("File size: ");
Serial.println(size);
char *bu;
bu = (char*) malloc((size+1) * sizeof(char));
file.read((uint8_t*) bu, size);
bu[size] = '[=11=]';
Serial.print("Data: ");
for (int i = 0; i < size; i++)
Serial.print(bu[i]);
Serial.println("");
//Everything is OK. It is printed correctly.
buff = &bu; //!This is the problem!
file.close();
}
SPIFFS.end();
}
#define file_path "/file"
void setup(){
if(WiFi.getMode() != WIFI_STA)
WiFi.mode(WIFI_STA);
char* username;
readConf(file_path, &username);
char* password;
readConf(file_path, &password);
/*The same with password. */
WiFi.begin(username, password);
Serial.print("username: ");
Serial.println(username); //Here I sometimes get Exception, and sometimes prints non-sense characters
free(username); //My memory is limited. I'm doing all this stuff for this line!
//...
}
我也在Whosebug等网站上搜索了很多,也用过char *
,char **
,直接在readConf
函数中填充指针,还有很多但是没有的他们工作了。我该如何处理?我完全可以做到吗?
注意:我不应该使用字符串 class。
函数参数buff
是函数的局部变量
void readConf(char path[], char **buff){
所以在函数内改变它
buff = &bu; //!This is the problem!
对函数setup
中声明的变量password
没有影响
char* password;
readConf(file_path, &password);
你需要写
*buff = bu;
那就是你需要在函数readConf
中改变函数setup
中声明的指针password
的值
所以你通过指向它的指针
通过引用将指针传递给了函数readConf
readConf(file_path, &password);
现在要直接访问原始指针 password
,您需要取消引用由表达式 &password
.
初始化的参数 buff
此函数将 return 文件大小(以字节为单位)。
#include <fstream>
std::ifstream::pos_type filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}
char* 增益; - 这是指向 char 数组的指针。不是 char** buff;
我正在编写 Arduino (ESP8266),并且必须从文件中读取字符串才能使用它。我不知道那个文件有多长,所以我必须创建一个 char*
并将其传递给 readConf
函数,以便 malloc
决定内存大小。
void readConf(char path[], char **buff){
SPIFFS.begin();
if (SPIFFS.exists(path))
{
File file = SPIFFS.open(path, "r");
int size = file.size();
Serial.print("File size: ");
Serial.println(size);
char *bu;
bu = (char*) malloc((size+1) * sizeof(char));
file.read((uint8_t*) bu, size);
bu[size] = '[=11=]';
Serial.print("Data: ");
for (int i = 0; i < size; i++)
Serial.print(bu[i]);
Serial.println("");
//Everything is OK. It is printed correctly.
buff = &bu; //!This is the problem!
file.close();
}
SPIFFS.end();
}
#define file_path "/file"
void setup(){
if(WiFi.getMode() != WIFI_STA)
WiFi.mode(WIFI_STA);
char* username;
readConf(file_path, &username);
char* password;
readConf(file_path, &password);
/*The same with password. */
WiFi.begin(username, password);
Serial.print("username: ");
Serial.println(username); //Here I sometimes get Exception, and sometimes prints non-sense characters
free(username); //My memory is limited. I'm doing all this stuff for this line!
//...
}
我也在Whosebug等网站上搜索了很多,也用过char *
,char **
,直接在readConf
函数中填充指针,还有很多但是没有的他们工作了。我该如何处理?我完全可以做到吗?
注意:我不应该使用字符串 class。
函数参数buff
是函数的局部变量
void readConf(char path[], char **buff){
所以在函数内改变它
buff = &bu; //!This is the problem!
对函数setup
password
没有影响
char* password;
readConf(file_path, &password);
你需要写
*buff = bu;
那就是你需要在函数readConf
中改变函数setup
password
的值
所以你通过指向它的指针
通过引用将指针传递给了函数readConf
readConf(file_path, &password);
现在要直接访问原始指针 password
,您需要取消引用由表达式 &password
.
buff
此函数将 return 文件大小(以字节为单位)。
#include <fstream>
std::ifstream::pos_type filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}
char* 增益; - 这是指向 char 数组的指针。不是 char** buff;