problem in allocatin memory in linux

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
Last edited on
Let's take
 
sprintf(name, "%s\x00", in_name);
How is load called? How can you be sure name is large enough to hold in_name + 1 byte? Why are you writing null on the end? Why are you not using snprintf? These apply to all string parameters.
Topic archived. No new replies allowed.