0

I'm not entirely experienced with C, but I am trying to write a 2D char array to a file, but am having some issues. Any and all help is greatly appreciated. Please let me know if any additional information/context is needed.

Error

malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. Aborted (core dumped)

My Array Declaration

#define MAX_LINE 80 
#define HISTORY_SIZE 10

char history[HISTORY_SIZE][MAX_LINE];

My Write Function

void writeHistory(){
char *fname = getHistoryFileName();

FILE *fp = fopen(fname, "wb");
if (!fp){
    printf("Unable to write to history file");
    exit(1);
}

//Write to the file
fwrite(history, sizeof(history), 1, fp);

fclose(fp);
}
Chris
  • 191
  • 2
  • 12
  • 1
    You might want to take a look at this fine answer: https://stackoverflow.com/a/2987222 – Stephan Schlecht Apr 16 '20 at 20:12
  • It seems that your code is OK, And I guess you should looking for the problem elsewhere. – HamidReza Apr 16 '20 at 20:25
  • 1
    Doesn't produce an error for me, have you tried this [sysmalloc-failed-error](https://stackoverflow.com/questions/19476627/mysterious-malloc-sysmalloc-assertion-failed-error)? – joepol Apr 16 '20 at 20:26
  • Also, look at `fwrite()`, it's not the problem here but the signature is different from your call [fwrite-manual](https://linux.die.net/man/3/fwrite) – joepol Apr 16 '20 at 20:41
  • Does this answer your question? [Why do I get a C malloc assertion failure?](https://stackoverflow.com/questions/2987207/why-do-i-get-a-c-malloc-assertion-failure) – Dharman Apr 16 '20 at 22:42

0 Answers0