please help a beginner

E

erbm

Hey people... I'm new in C programing, and i'm having some problem.
What's wrong with the code below. I got no error compiling the file,
but when executing i get a *** glibc detected *** ./test: double free
or corruption (out): 0xbfe83468 *** error:

It's a simple code:

//file config.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"

int read_server_list(char *config_file, char **servers)
{
FILE *file;

int i, status;
file = fopen(config_file, "r+");

if(file != NULL)

{
printf("File opened.\n");
status = SUCCESS;

servers = (char **) malloc(MAX_SERVERS * sizeof(char *));

if(servers == NULL)

{
printf("Memory out.\n");
status = OUT_OF_MEM;
}

else
{
for(i = 0; i < MAX_SERVERS; i++)
{
servers = (char *) malloc(MAX_CHAR_LINE);
fgets(servers, MAX_CHAR_LINE, file);
printf("%s",servers); //prints OK
}
}

fclose(file);
}
else
{
printf("Error openig the file.\n");
status = FILE_ERROR;
}

return status;
}


void cleanup_server_list(char **servers)
{
int i = 0;
do {
free( servers );
servers[i++] = NULL;
} while( i < MAX_SERVERS );

free( servers );
servers = NULL;
}


and then

//file main.c

#include <stdio.h>
#include "config.h"

int main()
{

int status;
char *file = "text.txt";
char **servers;
status = read_server_list(file, servers);

cleanup_server_list(servers); // HERE I GET THE ERROR

return 0;
}
 
E

Eric Sosman

erbm said:
Hey people... I'm new in C programing, and i'm having some problem.
What's wrong with the code below. I got no error compiling the file,
but when executing i get a *** glibc detected *** ./test: double free
or corruption (out): 0xbfe83468 *** error:

It's a simple code:

... from which you have omitted just enough to prevent
anyone who might want to help you from compiling and running
it ...
//file config.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"

We don't know what's in this file.
int read_server_list(char *config_file, char **servers)
{
FILE *file;

int i, status;
file = fopen(config_file, "r+");

if(file != NULL)

{
printf("File opened.\n");
status = SUCCESS;

SUCCESS is undefined.
servers = (char **) malloc(MAX_SERVERS * sizeof(char *));

MAX_SERVERS is undefined.
if(servers == NULL)

{
printf("Memory out.\n");
status = OUT_OF_MEM;

OUT_OF_MEM is undefined.
}

else
{
for(i = 0; i < MAX_SERVERS; i++)
{
servers = (char *) malloc(MAX_CHAR_LINE);


MAX_CHAR_LINE is undefined.
fgets(servers, MAX_CHAR_LINE, file);
printf("%s",servers); //prints OK


Mustbesomethingofamess,allthosestringsjammedtogether.
}
}

fclose(file);
}
else
{
printf("Error openig the file.\n");

You've misspled "opening."
status = FILE_ERROR;

FILE_ERROR is undefined.
}

return status;
}


void cleanup_server_list(char **servers)
{
int i = 0;
do {
free( servers );
servers[i++] = NULL;
} while( i < MAX_SERVERS );

free( servers );
servers = NULL;
}


and then

//file main.c

#include <stdio.h>
#include "config.h"

int main()
{

int status;
char *file = "text.txt";
char **servers;
status = read_server_list(file, servers);


... and here, at last, is the "real" error. Fortunately
for you, others have asked this same question before you, enough
of them so that it is Question 4.8 in the comp.lang.c Frequently
Asked Questions (FAQ) list at <http://www.c-faq.com/>.

Also, it's silly to go to a lot of work to return a status
code and then simply ignore it ...
 
E

erbm

oh, sorry, here is the .h

#ifndef _CONFIG_H_

#define _CONFIG_H



#define MAX_SERVERS 20
// max number of servers
#define MAX_CHAR_LINE 40
// 40 characters per line - maximum
#define SUCCESS 0

#define FILE_ERROR -1

#define OUT_OF_MEM -2





int read_server_list(char *config_file, char **servers);



void cleanup_server_list(char **servers);



#endif




the file contains a list of server's Ip's...

//text.txt

192.168.1.0
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17
192.168.1.18
192.168.1.19


And i ignores the returned value because main.c is just a test...
 
B

Ben Bacarisse

erbm said:
oh, sorry, here is the .h

That's good.

A tip: you should not have removed (or allowed your newsreader to
remove) the attribution. You left the sig (which normally one would
cut) so I suppose they cancel out!

What did you think of this bit? You might have follow-up questions.

(left for attribution)
 
E

erbm

thank... i Read the faq, and I think i understood, so I changed the
code, but the problem still se same. Does anyone have an ideia?

//file config.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"

int read_server_list(char *config_file, char ***servers)
{
FILE *file;
char** serv = *servers;

int i, status;
file = fopen(config_file, "r+");

if(file != NULL)

{
printf("File opened.\n");
status = SUCCESS;

serv = (char **) malloc(MAX_SERVERS * sizeof(char *));

if(serv == NULL)


{
printf("Memory out.\n");
status = OUT_OF_MEM;
}

else
{
for(i = 0; i < MAX_SERVERS; i++)
{
serv = (char *) malloc(MAX_CHAR_LINE);
fgets(serv, MAX_CHAR_LINE, file);
printf("%s",serv); //prints OK

}
}

fclose(file);
}
else
{
printf("Error openig the file.\n");
status = FILE_ERROR;
}

return status;
}


void cleanup_server_list(char **servers)
{
char** serv = *servers;
int i = 0;
do {
free( serv );
serv = NULL;
i++;
} while( i < MAX_SERVERS );

free( serv );
serv = NULL;


free( servers );
servers = NULL;
}


e finalmente...

//file main.c

#include <stdio.h>
#include "config.h"

int main()
{

int status;
char *file = "text.txt";
char **servers;
status = read_server_list(file, &servers);

cleanup_server_list(&servers); // ERROR

return 0;
}
 
E

erbm

thanks richard. It worked =)
Is there a way I could use a char** on both functions instead of a
char***

like
int read_server_list(char *config_file, char **servers) ;
void cleanup_server_list(char **servers);

what should I change to make that work?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top