linux 内存分配问题
我在造成问题的两行上加了星号。
第一行为日志文件分配内存,该内存将在第二个签名行中使用。 在第二个带符号的行处存在分段错误问题。 这是由于“logfile”未分配造成的。 我确信这一点,因为如果我在 load() 中分配内存,它就会起作用。 但是我想在类的构造函数中分配内存,而不是在方法 load() 中分配内存。
我不明白为什么它不起作用! 这是我第一次使用 Linux,所以也许我做错了什么!
谢谢你, 马可
server::server(){
port = 0;
serverup = 0;
loaded = 0;
logfile = (char *) malloc(SERVER_PATHS_SIZE*sizeof(char)); //****************************
}
int server::load(int in_id, char *in_name, char *in_ip, int in_port,
char *in_rcon, char *in_logfile){
int err;
sprintf(name, "%s\x00", in_name);
sprintf(ip, "%s\x00", in_ip);
port = in_port;
sprintf(rcon, "%s\x00", in_rcon);
sprintf(logfile,"%s\x00", in_logfile); //**********************************
err = urt.set(ip, port, rcon);
if(err < 1){
printf("server::load(): error from urt.set()\n");
return 0;
}
printf("server::load(): server %d loaded!\n", id);
loaded = 1;
return 1;
}
i signed with stars the 2 rows that are creating the problem.
the first row allocates memory for logfile, that will be used in the second signed row.
at the second signed row there is a problem of segmentation fault.
This is caused by the fact that "logfile" is not allocated.
I'm am sure of this because if i allocate the memory in load() it works.
However I want to allocate the memory in the constructor of the class and not in the method load().
I cannot understand why it does not work!
This is my first time on linux and so maybe i'm doing something wrong!
Thank you,
Marco
server::server(){
port = 0;
serverup = 0;
loaded = 0;
logfile = (char *) malloc(SERVER_PATHS_SIZE*sizeof(char)); //****************************
}
int server::load(int in_id, char *in_name, char *in_ip, int in_port,
char *in_rcon, char *in_logfile){
int err;
sprintf(name, "%s\x00", in_name);
sprintf(ip, "%s\x00", in_ip);
port = in_port;
sprintf(rcon, "%s\x00", in_rcon);
sprintf(logfile,"%s\x00", in_logfile); //**********************************
err = urt.set(ip, port, rcon);
if(err < 1){
printf("server::load(): error from urt.set()\n");
return 0;
}
printf("server::load(): server %d loaded!\n", id);
loaded = 1;
return 1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您正在尝试对
in_logfile
和in_rcon
进行空终止,这不适用于 printf,因为 printf 需要空终止字符串作为 %s 的参数首先。
反而
I think you are trying to nullterminate
in_logfile
andin_rcon
This won't work with printf because printf requires null-terminated string as arguments to %s in the first place.
instead
这绝对不是答案,但使用 C++ 进行开发将帮助您避免使用 C-with-classes 代码遇到的内存问题。
使用 std::strings,那么复制它们将是微不足道的(与 sprintf 相比),而且会更安全。使用已弃用的 char* 会使事情变得混乱。
一个很好的副作用是您不需要手动分配内存(使用 malloc 或 new),并消除任何内存泄漏的风险。
This is definitely not an answer, but developping in C++ will help you to avoid the memory problems you get with your C-with-classes code.
Use std::strings, then copying them will be trivial (compared to sprintf), and it will be way more safe. Using the deprecated char* makes things way to confusing.
A nice side effect is that you won't need to do manual allocation of the memory (with malloc or new), and eliminating any risk of memory leak.
我没有看到服务器类的析构函数。你有释放内存的析构函数吗?
我没有看到创建和使用服务器对象的代码。难道是您创建了服务器对象,然后复制了它,并且由于没有正确实现复制语义而出现了问题?
I don't see the destructor of the server class. Do you have a destructor that frees the memory?
I don't see the code which creates and uses the server object. Could it be that you create the server object but then make a copy of it, and the problem occurs because you do not properly implement copying semantics?