0

I have a quick question regarding updating an array in C. I'm including the code I have written, along with the debug/print statement output.

//struct
typedef struct Server {
char* name;
pid_t pid;
} Server;

//global vars
int num_servers; //the number of servers (num_servers-1 is index in  array)
Server* servers; //the array of servers
int size; //size of the array

//instantiation inside main method
num_servers = 0;
servers = NULL;  //null instantiation allows us to use realloc all time 
size = 0;

//inside main function and a while loop
//check if we need to resize the array
                if(num_servers >= size){
                    resizeArray(true);
                }
                //create struct
                Server s = createServer(args[3], id);
                //add to array
                insertServer(s);
                printf("Main: last server has name: %s\n", servers[num_servers-1].name); //debug
                ++num_servers;

/*
 * creates/returns a server with the specified characteristics
 * name - the name of server]
 * id - the id of the server
 */
Server createServer(char* name, pid_t id){
    Server s;
    s.name = malloc(strlen(name) * sizeof(char));
    strcpy(s.name, name);
    s.pid = id;
    printf("Created server with name: %s\n", s.name); //debug
    return s;
}

/*
 * appends server to end of array
 * serv - the server struct to insert
 */
int insertServer(Server serv){
    printf("Inserting server with name: %s\n", serv.name); //debug
    //allocate memory
    servers[num_servers-1].name = malloc(strlen(serv.name) * sizeof(char));
    //actually copy
    strcpy(servers[num_servers-1].name, serv.name);
    servers[num_servers-1].pid = serv.pid;
    printf("The last server in array now has id of: %s\n",   servers[num_servers-1].name);
    return 0;
}

When I run the while loop for the first time and insert a server, the program runs correctly (all the print statements output the name of the server). However, as soon as the while loop runs again, I get a seg fault. Using GDB I found that while the memory for the array appears to be allocated, the actual structs (and their information) do not appear in the array. Any idea as to why the information is present in the heap during the while loop, yet dissapears when it runs again? Thank you.

Connor Butch
  • 648
  • 1
  • 10
  • 28
  • The code posted does not have a `while` loop. It is not even compilable code. Please post the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that shows the problem. – Weather Vane Oct 01 '17 at 22:43
  • Please read up how to ask questions here and provide a minimal example: http://stackoverflow.com/help/mcve – Jens Gustedt Oct 01 '17 at 22:43
  • 1
    You have an off-by-one error and will write out of bounds of allocated memory (don't forget that `char` strings in C are really called ***null terminated** character strings*). – Some programmer dude Oct 01 '17 at 22:44
  • Oh, you have *two* off-by-one errors really. For the first "server", when `num_servers` is zero, what is then `num_servers - 1`? And you have a memory leak. – Some programmer dude Oct 01 '17 at 22:45
  • Lastly, I recommend you read [this old question](https://stackoverflow.com/q/9127246/440558) about copying structures. It's much simpler than you think. – Some programmer dude Oct 01 '17 at 22:47
  • 'int num_servers; //the number of servers (num_servers-1 is index in array)'...........'num_servers = 0;'.... ahem.... – Martin James Oct 01 '17 at 22:47

1 Answers1

0

In this code partical you allocate not enough memory for c-string (forgotten byte for '\0'-char aka char of terminate):

s.name = malloc(strlen(name) * sizeof(char));
strcpy(s.name, name);

Try to use strdup:

s.name = strdup(name);