0

I'm new here and don't speak very well english. I've a question concerning my code (in C).

What the code should do: There exists a file named "g.txt", that should be opened. Then it reads it line by line and copies each line in a buffer (zpuffer[200]), after that the content of the buffer should be copied with strcpy to **Dfile. **Dfile points to a char*, its space was first allocated with malloc. If **Dfile hasn't enough space for saving, the code executes realloc, that shoud make more free space.

The first time realloc have been called, I actually got more space! But the second time, it didn't work; the OS, Ubuntu, says (in German): "Bus-Zugriffsfehler (Speicherabzug geschrieben)". English: "Bus access error (written dump)"

Here is my code

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

#define OBJS 10 //new Lines a one time for **Dfile

int main(void){
FILE *datei;
datei=fopen("g.txt", "r");
if (datei == NULL){
    fprintf(stderr, "\nFEHLER\n");
    return 1;
}

char zpuffer[200]; //buffer
char **Dfile; //save content of g.txt line by line, every line has its own index

int zeilen = 0; //the actual amount of lines
int speicher = 0; //total indices in **Dfile    
Dfile = (char**)malloc(sizeof(char**)*(speicher + OBJS));
speicher += OBJS;   

while (fgets(zpuffer, 199, datei) != NULL){ 
    if (speicher <= zeilen){//speicher <= zeilen --> allocate memory
        Dfile = (char**)realloc(*Dfile, sizeof(char**)*(speicher + OBJS)); //!!ERROR!! but just the second time!!
        if (Dfile == NULL){
            fprintf(stderr, "\nFEHLER Alloc\n");
            return 1;
        }           
        speicher += OBJS;
    }

    Dfile[zeilen]=malloc(strlen(zpuffer)+1);
    strcpy(Dfile[zeilen++], zpuffer);       
    printf("%s", Dfile[zeilen - 1]);    
}
return 0;
}

May someone help me, please??

MicroVirus
  • 5,324
  • 2
  • 28
  • 53
  • 2
    `Dfile = realloc(Dfile, sizeof *Dfile * (speicher+OBJS));` (and `Dfile = malloc(sizeof(*Dfile *(speicher + OBJS));` ) OR: `Dfile = malloc(sizeof(char*) * (speicher + OBJS));` if you want, but that is more error-prone ... Plus: you don't need the initial malloc(); `ptr = realloc(NULL, new_size);` works as expected. – joop Oct 12 '15 at 15:49
  • first - test the result of mallic. second run the program under a debugger, you dont say what platform so I cant say what debugger you need – pm100 Oct 12 '15 at 15:49
  • 1
    Additional `fgets(zpuffer, 199, datei)` -> `fgets(zpuffer, 200, datei)` . – ameyCU Oct 12 '15 at 15:50
  • I use ubuntu. Actually I tested the code, the error comes from the realloc – Fragender99 Oct 12 '15 at 15:51
  • Even if your english is bad, try to write all comments and especially **variable identifiers** in english. The next best thing would be to do it consistently in german. Just an advise ;) –  Oct 12 '15 at 15:51
  • @joop small change `Dfile = malloc(sizeof *Dfile *(speicher + OBJS));` { () do not balance in your comment } – chux - Reinstate Monica Oct 12 '15 at 15:55
  • @Fragender99 `fgets` will terminate it with `'\0'` so no need to leave additional space for it . It will write `199` characters and add a `'\0'` at end . – ameyCU Oct 12 '15 at 15:56
  • Aha, I needed to snip yet another '(' : Too late for edit, now! – joop Oct 12 '15 at 15:57
  • Thanks a lot!! It worked... :-) – Fragender99 Oct 12 '15 at 16:05
  • Please also indent your code properly. – alk Oct 12 '15 at 16:09

1 Answers1

2

You pass *Dfile to realloc().

Did you meant to pass Dfile instead?

alk
  • 69,737
  • 10
  • 105
  • 255
PineForestRanch
  • 473
  • 2
  • 11
  • ***Dfile is a pointer to char*. So I wish to "add" more char*-Pointers to it. – Fragender99 Oct 12 '15 at 15:58
  • 1
    Dfile is a pointer to pointers which point to strings, right? You've got too many strings, so you need to add more pointers, not more chars. -- Instead what you do is re-allocating the first line and then assigning the resulting pointer to Dfile, which, most likely will cause some sort of crash down the line. – PineForestRanch Oct 12 '15 at 16:02