Code: Select all
char* fileName = malloc((int)strlen(strcat(stripFileExtension(argv[1]), ".c")) * sizeof(char));
strcpy(fileName, strcat(stripFileExtension(argv[1]), ".c"));
...
system(strcat("gcc -std=c99 ", fileName));
Moderator: Coders of Rage
Code: Select all
char* fileName = malloc((int)strlen(strcat(stripFileExtension(argv[1]), ".c")) * sizeof(char));
strcpy(fileName, strcat(stripFileExtension(argv[1]), ".c"));
...
system(strcat("gcc -std=c99 ", fileName));
I don't really know if C and C++ differentiate in terms of their arguments. My assumption though, is that you want "argv[0]" not "argv[1]" as you're probably indexing a cell in the argv array that doesn't exist by doing the latter.lotios611 wrote:I am trying to code a brainfuck compiler thingy, and am almost done. However, I have ran into a problem. For some reason, the following code segfaults when ran.Code: Select all
char* fileName = malloc((int)strlen(strcat(stripFileExtension(argv[1]), ".c")) * sizeof(char)); strcpy(fileName, strcat(stripFileExtension(argv[1]), ".c")); ... system(strcat("gcc -std=c99 ", fileName));
The first part works AFAIK because I can use the filename before the call to system(). Also, my application requires you to at least pass in the name of the file to compile, which would be argv[1]JesseGuarascia wrote:I don't really know if C and C++ differentiate in terms of their arguments. My assumption though, is that you want "argv[0]" not "argv[1]" as you're probably indexing a cell in the argv array that doesn't exist by doing the latter.lotios611 wrote:I am trying to code a brainfuck compiler thingy, and am almost done. However, I have ran into a problem. For some reason, the following code segfaults when ran.Code: Select all
char* fileName = malloc((int)strlen(strcat(stripFileExtension(argv[1]), ".c")) * sizeof(char)); strcpy(fileName, strcat(stripFileExtension(argv[1]), ".c")); ... system(strcat("gcc -std=c99 ", fileName));
Code: Select all
system(strcat("gcc -std=c99 ", fileName));
Code: Select all
char cmd[64]; // Arbitrary
sprintf(cmd, "gcc -std=c99 %s", fileName);
system(cmd);
Code: Select all
char *cmd = (char *)malloc(sizeof(char) * (strlen("gcc -std=c99 ") + strlen(fileName) + 1)); // The sizeof(char) may look a little redundant, but I have a habit of writing my C-style allocations to be ready to upgrade to wide characters with just a few project-wide find/replaces.
sprintf(cmd, "gcc -std=c99 %s", fileName);
system(cmd);
free(cmd);
Code: Select all
const char compiler[] = "gcc -std=c99 ";
char *cmd = (char *)malloc(sizeof(char) * (strlen(compiler) + strlen(fileName) + 1));
sprintf(cmd, "%s%s", compiler, fileName);
system(cmd);
free(cmd);
Is the +1 to account for the null character? This is my first C program by the way.Krolgar wrote:Code: Select all
char *cmd = (char *)malloc(sizeof(char) * (strlen("gcc -std=c99 ") + strlen(fileName) + 1)); // The sizeof(char) may look a little redundant, but I have a habit of writing my C-style allocations to be ready to upgrade to wide characters with just a few project-wide find/replaces. sprintf(cmd, "gcc -std=c99 %s", fileName); system(cmd); free(cmd);
Yes. When allocating a string, you should always add one extra character for the null-terminator.lotios611 wrote:Is the +1 to account for the null character? This is my first C program by the way.Krolgar wrote:Code: Select all
char *cmd = (char *)malloc(sizeof(char) * (strlen("gcc -std=c99 ") + strlen(fileName) + 1)); // The sizeof(char) may look a little redundant, but I have a habit of writing my C-style allocations to be ready to upgrade to wide characters with just a few project-wide find/replaces. sprintf(cmd, "gcc -std=c99 %s", fileName); system(cmd); free(cmd);
Alright, thanks for the help.Krolgar wrote:Yes. When allocating a string, you should always add one extra character for the null-terminator.lotios611 wrote:Is the +1 to account for the null character? This is my first C program by the way.Krolgar wrote:Code: Select all
char *cmd = (char *)malloc(sizeof(char) * (strlen("gcc -std=c99 ") + strlen(fileName) + 1)); // The sizeof(char) may look a little redundant, but I have a habit of writing my C-style allocations to be ready to upgrade to wide characters with just a few project-wide find/replaces. sprintf(cmd, "gcc -std=c99 %s", fileName); system(cmd); free(cmd);