Liste



  • cursor wird doch in der Schleife initialisiert??
    gut das mit fscanf ist nicht optimal sollte aber doch korrekt funktionieren oder?



  • Wenn deine fscanf-Schleife nicht durchlaufen wird, beginnt die nächste Schleife mit uninitialisiertem cursor.
    Mache mal ein paar Testausgaben oder debugge, damit du die Crashzeile ermitteln kannst.



  • Ok hab das ganze jetzt auf so was umgeschrieben, fuer k<1000 funktionierts nur wenn ich ne hoehere zahl eingebe k<10000: Segmentation Fault?!?!
    P.S. Dass File hat ca. 400000 Eintraege also sollte fuer k<400000 gelten koennen...

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct word {
    	char *w;
    	int spam;
    	int ham;
    	struct word *next;
    };
    
    int main() {
    
    	 FILE *fp;
    			  char *file = "files.txt";
    
    			  fp = fopen(file, "r");
    			  if (fp == NULL) {
    			    printf("ERROR opening file");
    			    exit(1);
    			  }
    
    	struct word *begin = NULL;
    	struct word *cursor;
    	char *tmp = malloc(sizeof(char) * 30);
    
    	int a, b;
    	int k = 0;
    	while(k<1000) {
    
    			 cursor = malloc(sizeof(struct word));
    			 fscanf(fp, "%s %d %d", tmp, &a, &b);
    			 cursor->w = malloc(sizeof(char) * 30);
    
    			 strcpy(cursor->w, tmp);
    			 cursor->spam = a;
    			 cursor->ham = b;
    
    			 cursor->next = begin;
    			 begin = cursor;
    			 k++;
    	 }
    
    	 while (cursor != NULL) {
    		 printf("%s\n", cursor->w);
    		 cursor = cursor->next;
    	 }
    
    	fclose(fp);
    	return 0;
    }
    


  • wenn du das cursor objekt erzeugst mußt du noch next mit null initialisieren. also cursor->next = NULL;



  • Was passiert denn wenn malloc mal keinen Speicher besorgt?

    Bei welchem k steigt das Programm den aus?

    Ist evtl. ein Fehler in "files.txt"?



  • aber noch wichtiger ist das du nicht courser->next = begin machst sonder
    begin->next = cursor
    und dann
    begin = cursor

    dazu muß natürlich begin vor dem schleifen eintritt schon mit dem ersten element initialisiert werden, also die schleife beginnt dann auch k = 1



  • Ersetze die fscanf-Zeile mal durch

    int c;
    ...
    if( (c=fscanf(fp, "%29s%d%d", tmp, &a, &b))==EOF ) break;
    else if( c!=3 ) continue;
    

    Die malloc-Rückgabewerte prüfst du auch nicht, ebenso gibst du die mallocs nicht wieder frei.



  • ist das eig. ein next oder prev pointer 😕



  • Zu DirkB:
    Das Program steigt bei k<2106 aus
    Nein im File.txt ist kein Fehler steht immer: <string> <int> <int>

    Zu edi2002:
    Wie ich das ganze anhaenge ist mir egal in meinem Fall wirds halt umgekehrt gemacht... und so funktionierts ja auch bis k<2106 ??

    zu Wutz:
    hab die Zeile ersetzt aendert sich aber nix??



  • Prüfe die mallocs und ermittle die Crashzeile!



  • Fuer die Mallocs hab ich jetzt ueberall ne Fehlerabfrage drinnen aber die werden nicht ausgegeben??

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct word {
    	char *w;
    	int spam;
    	int ham;
    	struct word *next;
    };
    
    int main() {
    
    	 FILE *fp;
    			  char *file = "file.txt";
    
    			  fp = fopen(file, "r");
    			  if (fp == NULL) {
    			    printf("ERROR opening file");
    			    exit(1);
    			  }
    
    	struct word *begin = NULL;
    	struct word *cursor;
    	char *tmp = malloc(sizeof(char) * 100);
    
    	int a, b;
    	int k = 0;
    	while(k<2105) {
    
    			 cursor = malloc(sizeof(struct word));
    			 if (cursor == NULL) {
    				 printf("NO MEMORY");
    				 exit(1);
    			 }
    
    			 cursor->next = NULL;
    			 fscanf(fp, "%s %d %d", tmp, &a, &b);
    
    			 cursor->w = malloc(sizeof(char) * 100);
    			 if (cursor->w == NULL) {
    				 printf("NO MEMORY");
    				 exit(1);
    			 }
    
    			 strcpy(cursor->w, tmp);
    			 cursor->spam = a;
    			 cursor->ham = b;
    
    			 cursor->next = begin;
    			 begin = cursor;
    			 k++;
    	 }
    
    	 while (cursor != NULL) {
    		 printf("%s\n", cursor->w);
    		 cursor = cursor->next;
    	 }
    
    	fclose(fp);
    	return 0;
    }
    


  • Sry funktioniert immer noch nicht...



  • Wahrscheinlich Überlauf beim printf. Schreibe statt printf mal mit fprintf in eine Datei und lasse die Ausgabe auf stdout komplett weg.



  • Den Teil hab ich schon mal ausgeklammert hat aber nix gebracht...



  • Stürzt das Programm immer in Zeile 2106 ab oder auch in anderen? (du hast jetzt bei malloc 100, vorher 30)

    Welches Betriebssystem und Compiler verwendest du?
    Probier mal strncpy aus.



  • Ja das mit Malloc hab ich mal kurz geaendert und jetzt laeufts nur mehr bis ca 1122...
    Betriebssystem Linux Ubuntu 11.04, Compiler gcc

    strncpy gibt nur das gleiche Ergebnis...



  • An der ersten Schleife liegt es nicht, es liegt wie gesagt am Überlauf von stdout durch zu viele printf.

    http://www.ideone.com/fb2gU



  • Das kann glaub ich nicht sein denn wenn ich die printf ausklammere krieg ich immer noch nen Segmentation Fault...

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int count;
    
    struct word {
    	char *w;
    	int spam;
    	int ham;
    	struct word *next;
    };
    
    int main() {
    
    	count = 0;
    	 FILE *fp;
    			  char *file = "file.txt";
    
    			  fp = fopen(file, "r");
    			  if (fp == NULL) {
    			    printf("ERROR opening file");
    			    exit(1);
    			  }
    
    	struct word *begin = NULL;
    	struct word *cursor;
    	char *tmp = malloc(sizeof(char) * 50);
    
    	int a, b;
    	int k = 0;
    	while(k<100000) {
    
    			 cursor = malloc(sizeof(struct word));
    			 cursor->w = malloc(sizeof(char) * 50);
    			 if (cursor == NULL) {
    				 printf("NO MEMORY");
    				 exit(1);
    			 }
    
    			 cursor->next = NULL;
    			 fscanf(fp, "%s %d %d", tmp, &a, &b);
    
    			 strncpy(cursor->w, tmp, strlen(tmp));
    
    			 cursor->spam = a;
    			 cursor->ham = b;
    			 count++;
    
    			 cursor->next = begin;
    			 begin = cursor;
    			 k++;
    	 }
    
    /*
    	 while (cursor != NULL) {
    		 printf("%d %d\n", cursor->spam, cursor->ham);
    		 cursor = cursor->next;
    
    	 }
    	 */
    
    	fclose(fp);
    	return 0;
    }
    


  • Benutze das strncpy richtig.

    strncpy(cursor->w, tmp, 50);
    

    wenn cursor->w Platz für 50 Zeichen hat.

    Das strlen bekommt strncpy schon hin. Du mußt angeben wie viel Platz in deinem Puffer ist. Ich weiß allerdings gerade nicht wie das mit dem abschließenden '\0' ist,wenn die Pufferlänge erreicht wird.

    Sinnvoller wäre auf alle Fälle das tmp richtig groß zu machen und dann

    cursor->w = malloc(strlen(tmp)+1);
    


  • Ok habs jetzt so gemacht nur krieg ich jetzt folgenden Fehlercode:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int count;
    
    struct word {
    	char *w;
    	int spam;
    	int ham;
    	struct word *next;
    };
    
    int main() {
    
    	count = 0;
    	 FILE *fp;
    			  char *file = "file.txt";
    
    			  fp = fopen(file, "r");
    			  if (fp == NULL) {
    			    printf("ERROR opening file");
    			    exit(1);
    			  }
    
    	struct word *begin = NULL;
    	struct word *cursor;
    	char *tmp = malloc(sizeof(char) * 200);
    
    	int a, b;
    	int k = 0;
    	while(fscanf(fp, "%s %d %d", tmp, &a, &b) != EOF) {
    
    			cursor = malloc(sizeof(struct word));
    			cursor->w = malloc(strlen(tmp)+1);
    
    			 if (cursor == NULL) {
    				 printf("NO MEMORY");
    				 exit(1);
    			 }
    
    			 cursor->next = NULL;
    
    			 strncpy(cursor->w, tmp, 200);
    
    			 cursor->spam = a;
    			 cursor->ham = b;
    			 count++;
    
    			 cursor->next = begin;
    			 begin = cursor;
    
    	 }
    
    	 while (cursor != NULL) {
    		 printf("%d %d\n", cursor->spam, cursor->ham);
    		 cursor = cursor->next;
    
    	 }
    
    	fclose(fp);
    	return 0;
    }
    

    Fehler:

    SPAM: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char 😉 &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.


Anmelden zum Antworten