0

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;
}
5
  • 2
    Please reduce your code to a working (i.e. compilable, runnable) example that demonstrates the problem. Otherwise we can only guess where the problem is... Commented Apr 21, 2011 at 12:06
  • We cannot see where logfile is declared. Is it a member variable of class server? Is it global? Making your examples stand-alone compilable is crucial for debugging. Commented Apr 21, 2011 at 12:20
  • when programming c++, new is usually preferred over malloc Commented Apr 21, 2011 at 12:26
  • 1
    Any particular reason you're not using std::string? Commented Apr 21, 2011 at 12:39
  • Consider removing the "linux" tag from this topic as it has nothing to do with Linux. Commented Apr 21, 2011 at 14:00

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

To eleborate a little, the segmentation fault occurs because the lack of null-termination in the original input to sprintf causes data way past the end of the original input to be read, causing invalid reads. Further, you're then trying to assign lots more data to the output arrays than you think, causing invalid writes.
1

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.

1 Comment

Using char* to handle character arrays is not deprecated. (Though binding string literals to char* rather than char const* is.)
0
  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?

3 Comments

(2) No, it couldn't, because of (1). :)
Dark Falcon, you are a genius!! That was the problem!! Many thanks!
Basically the constructor was not called... probably i was creating the server object in a wrong way! thanks to all for the replies!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.