A
Hallo,
ich habe ein Problem mit folgenden Programm. Es geht auch um eine verkettete Liste.
Das Problem ist, dass beim allerersten Allocate-Aufruf das Programm hängt und den Speicher "frisst".
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// File Pointer
FILE *fil;
// Chained List
struct list_element {
int ship;
struct list_element *next;
};
int print_usage() {
printf("usage: container [filename]\n");
exit(-1);
}
// Returns the next character from file with error handling
int fget_next_char() {
printf("fget_next_char()");
int temp = fgetc(fil);
if (temp == EOF) {
if (ferror(fil)) {
printf("File Error");
fclose(fil);
exit(-1);
}
if (feof(fil)) {
printf("EOF reached");
}
fclose(fil);
}
return temp;
}
int main(int argc, char* argv[]) {
struct list_element* firstnode;
struct list_element* lastnode;
struct list_element* node;
firstnode = NULL;
lastnode = NULL;
// Temp var for read data from file
int read_input = 0;
int i = 1;
// If less or more than one paramter is given, print usage and quit
if ( argc != 2) {
print_usage();
}
// Try to open the given file
fil = fopen(argv[argc-1], "r");
if (fil == NULL) {
printf("Error opening file\n");
exit(-1);
}
printf("File opened successfully\n");
while (fil != NULL) {
read_input = fget_next_char();
if (fil != NULL) {
printf("%d ",i); i++;
// Allocate Memory for a node
node = (struct list_element*) malloc(sizeof(struct list_element));
if (node == NULL) {
// TODO output to stderr
printf("Out of memory");
fclose(fil); exit(-1);
}
// Create 1st node
node->ship = read_input;
node->next = NULL;
firstnode = node;
lastnode = node;
read_input = fget_next_char();
}
while (fil != NULL || read_input != '\n') {
if (read_input != ',') {
// Allocate Memory for a node
node = (struct list_element*) malloc(sizeof(struct list_element));
if (node == NULL) {
// TODO output to stderr
printf("Out of memory");
fclose(fil); exit(-1);
}
node->ship = read_input;
lastnode->next = node;
lastnode = node;
node->next = NULL;
}
}
node = firstnode;
/*while (firstnode != NULL) {
if (node->ship >= node->next->ship) {
}
*/
while (node->next != NULL) {
printf("%c|",node->ship);
node = node->next;
}
}
return(0);
}
gdb:
[Session started at 2007-09-16 19:52:19 +0200.]
GNU gdb 6.3.50-20050815 (Apple version gdb-573) (Fri Oct 20 15:50:43 GMT 2006)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".
Loading program into debugger…
tty /dev/ttyp3
Program loaded.
sharedlibrary apply-load-rules all
run
[Switching to process 1227 local thread 0xf03]
Running…
Pending breakpoint 1 - ""container.c:65" resolved
File opened successfully
1
(gdb) continue
container(1227) malloc: *** vm_allocate(size=1069056) failed (error code=3)
container(1227) malloc: *** error: can't allocate region
container(1227) malloc: *** set a breakpoint in szone_error to debug
Out of memory
malloc() hat schon beim ersten Aufruf Probleme (siehe die Ausgabe "1" von Zeile 72).
Hat jemand ne Idee, woran es liegen könnte ?
Vielen Dank schon mal im Voraus
MfG, abc-man