please help a beginner : c++
This is a discussion on please help a beginner within the c++ forums in Programming Languages category; 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; } ...
| c++ comp.lang.c++ usenet archive |
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| 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[i] = (char *) malloc(MAX_CHAR_LINE); fgets(servers[i], MAX_CHAR_LINE, file); printf("%s",servers[i]); //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[i] ); 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; } |
|
#2
| |||
| |||
| Actually, you did pretty good. This is a level-of-indirection issue. Your main problem is that you're trying to return the array of pointers back to the main program. If I have an integer scalar that I want to pass into a subroutine, I just code - void f(int X)Right? Obviously. But if I want that routine to obtain a value for that integer and pass it back to the caller, I must code - void f(int *X)Now what about if the thing I'm trying to assign in that subroutine is itself a pointer? I still need to add one more level of indirection, like this:{ /* ... */ *X = 42; /* ... */ }/*And it must be invoked by the caller like this: */ void f(char **v)(notice that you de-reference only ONE level of pointer when assigning the address to pass back to the caller){ *v = malloc(/* (whatever) */); /*... */ } Now what about your case? In your case, the thing we're trying to assign is already a level-2 pointer (a pointer to an array of pointers, which you seem to understand in every other respect). If we were merely passing in its value obtained prior to invocation, we would declare it void f(char **servers)But we want the subroutine to actually obtain the address and pass it back to the caller! So we need yet another (a 3rd) level of indirection: void f(char ***servers)What you had actually would have worked if either:{ /* ... */ *servers = malloc(<whatever>); /*...*//* And in your main(), it should be invoked as... */.
|
![]() |
| Thread Tools | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Beginner | usenet | Java | 12 | 02-25-2008 04:36 PM |
| Beginner. | usenet | Websphere | 1 | 10-19-2007 06:02 AM |
| 3DS beginner | usenet | Graphics | 2 | 04-27-2007 02:29 PM |
| help for a BEGINNER | usenet | Adobe Tools | 3 | 01-02-2007 03:42 PM |
| XML Beginner | usenet | XML SOAP | 3 | 11-27-2006 09:04 AM |


