linux 内存分配问题

发布于 2024-11-02 20:16:02 字数 1004 浏览 1 评论 0原文

我在造成问题的两行上加了星号。

第一行为日志文件分配内存,该内存将在第二个签名行中使用。 在第二个带符号的行处存在分段错误问题。 这是由于“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-11-09 20:16:02

我认为您正在尝试对 in_logfilein_rcon 进行空终止,

这不适用于 printf,因为 printf 需要空终止字符串作为 %s 的参数首先。

charptr[known_length] = 0

反而

I think you are trying to nullterminate in_logfile and in_rcon

This won't work with printf because printf requires null-terminated string as arguments to %s in the first place.

charptr[known_length] = 0

instead

倾听心声的旋律 2024-11-09 20:16:02

这绝对不是答案,但使用 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.

夏の忆 2024-11-09 20:16:02
  1. 我没有看到服务器类的析构函数。你有释放内存的析构函数吗?

  2. 我没有看到创建和使用服务器对象的代码。难道是您创建了服务器对象,然后复制了它,并且由于没有正确实现复制语义而出现了问题?

  1. I don't see the destructor of the server class. Do you have a destructor that frees the memory?

  2. 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文