I'm fairly new to C, and today I was introduced to Valgrind. I installed it and ran it on my C calculator/equation parser that I'm working on to figure out why I was having a segmentation fault (core dumped), and I got this:
==20== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==20== General Protection Fault
==20== at 0x4008E27: _dl_map_object (dl-load.c:2317)
==20== by 0x40014DD: map_doit (rtld.c:642)
==20== by 0x4010193: _dl_catch_error (dl-error.c:187)
==20== by 0x4002169: do_preload (rtld.c:831)
==20== by 0x4002169: handle_ld_preload (rtld.c:929)
==20== by 0x4004DEE: dl_main (rtld.c:1667)
==20== by 0x40176F4: _dl_sysdep_start (dl-sysdep.c:249)
==20== by 0x4001BB7: _dl_start_final (rtld.c:347)
==20== by 0x4001BB7: _dl_start (rtld.c:573)
==20== by 0x4001267: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==20== by 0x1: ???
==20== by 0x1FFF0008AE: ???
==20== by 0x1FFF0008BB: ???
Of course, I have no idea what it means, and the other things I've found about similar errors haven't made much sense to me. Can someone explain this in a relatively simple way that someone like me can understand?
EDIT: I tried running it through gdb (ass suggested by @pm100), and only got this:
Program received signal SIGSEGV, Segmentation fault.
0x000000000040067b in ?? ()
EDIT: Since my code was asked for, here it is. I'm probably doing a lot wrong.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void write(char** dest, char* src, int index) {
int i = 0;
for (i = 0; src[i] != '\0'; i++) {
dest[index][i] = src[i];
}
dest[index][i] = '\0';
return;
}
void crite(char** dest, char src, int index) {
int i = 0;
dest[index][0] = src;
dest[index][1] = '\0';
return;
}
void evaluate(char* args) {
int i = 0;
int j = 0;
const char* numbers = "1234567890";
const char* operators = "+-*/";
int chunk = 0;
char** chunks = calloc(24, sizeof(char*));
char* current = calloc(24, sizeof(char));
for (i = 0; strchr("\0\n", args[i]) == NULL; i++) {
//printf("Args[i]:%c\n\n", args[i]);
if (strchr(numbers, args[i]) != NULL) {
//printf("Number added to current: %c\n\n", args[i]);
current[j] = args[i];
//printf("\nCurrent: %s\n", current);
j++;
} else if (strchr(operators, args[i]) != NULL) {
write(chunks, current, chunk);
chunk++;
crite(chunks, args[i], chunk);
chunk++;
j = 0;
free(current);
current = calloc(24, sizeof(char));
//printf("Terminated with operator and operator added.\n\n");
} else {
printf("ERROR: Encountered invalid token.\n\n");
return;
}
}
for (i = 0; chunks[i] != NULL; i++)
//printf("\n-Chunk: %s\n\n", chunks[chunk]);
return;
}
int main(int argc, char** argv) {
evaluate(argv[1]);
}
The command I used to compile it was gcc calculator.c -g -o calculator
Sample command: ./calculator 1*2
UPDATE: The issue with Valgrind was caused by the Windows subsystem I was using, so as long as you're running Valgrind on linux it should be fine. I tried it in a VM and it worked.
Also, thanks for helping me fix my Segmentation Fault even though that wasn't what the question was originally about:)