I’ve got code that compiles well enough, but when I try to run it, I get a segmentation fault and I can’t figure out what’s wrong.
The point of the program is to piece together the text of fragmentet ascii art files of varying dimensions and number of fragments.
The fragments are named part_xx-yy where xx is from 00 to 11 and yy is from 00 to 05.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
int width;
int height;
int xPieces;
int yPieces;
int xTens=0;
int xOnes=0;
int yTens=0;
int yOnes=0;
printf("Fragment width: ");
scanf("%d", &width);
printf("Fragment height: ");
scanf("%d", &height);
printf("Number of fragments on x axis: ");
scanf("%d", &xPieces);
printf("Number of fragments on y axis: ");
scanf("%d", &yPieces);
printf("wtf0");
char *line=malloc(sizeof(width) * sizeof(char));
printf("wtf1");
char array[xPieces][yPieces][height][width];
printf("wtf2");
char fileName[50];
printf("wtf3");
for(int x = 0; x<xPieces;)
{
printf("%d", x);
for(int y = 0; y<yPieces;)
{
printf("%d", y);
if(xOnes>=10)
{
xOnes=0;
xTens++;
}
if(yOnes>=10)
{
yOnes=0;
yTens++;
}
snprintf(fileName, sizeof fileName, "part_%i%i-%i%i", xTens, xOnes, yTens, yOnes);
FILE *file=fopen(fileName, "r");
char buffer[(width) * (height)];
fread(buffer, 1, (width) * (height), file);
for(int i = 0; i<height; i++)
{
printf("%d", i);
for(int j = 0; j<width; j++)
{
printf("%d", j);
array[x][y][i][j] = buffer[j + (i * (width))];
}
}
fclose(file);
y++;
yOnes++;
}
x++;
xOnes++;
}
FILE *newFile=fopen("newFile", "w");
for(int y = 0; y<yPieces; y++)
{
for(int i = 0; i<height; i++)
{
for(int x = 0; x<xPieces; x++)
{
for(int j = 0; j<width; j++)
{
fwrite(&array[x][y][i][j], 1, 1, newFile);
}
}
}
}
fclose(newFile);
free(line);
}
I figured out how to use the debugger, and that indicated there was something wrong with fread(), which I think was caused by my fileName array, but I changed a few things and now all I get from the debugger is this:
Program received signal SIGSEGV, Segmentation fault.
0x0018d68c in fread () from /lib/tls/i686/cmov/libc.so.6
I thought perhaps fread() tried to read into a too small buffer, so I increased the buffer to 10000 (which SHOULD be dramatic overkill), but alas, to no avail.
I have researched quite a bit now, struggling with this problem for a couple of hours, but still have no idea how to go further from here as what I find of similar problems doesn’t make much sense to me or isn’t similar enough.
I think at this point I need someone else to look at my code, so any help will be greatly appreciated.
.
.
Update: I’ve updated my code with a few changes, and now I get a segmentation fault here instead:
Program received signal SIGSEGV, Segmentation fault.
0x08049058 in main () at innlev3.c:50
50 *array[x][y][i][j] = buffer[j + (i * (*width))];
I thought this part was pretty good… What have I done wrong, here?
Update 2: Code updated again. I’ve found something I think is very strange… Neither of those printf’s after my scanf’s work… Aand I’m back to the good old fread() segmentation fault. I guess it’s a good thing I didn’t make a new question out of this… 😛
Program received signal SIGSEGV, Segmentation fault.
0x0018d68c in fread () from /lib/tls/i686/cmov/libc.so.6
(gdb) backtrace
#0 0x0018d68c in fread () from /lib/tls/i686/cmov/libc.so.6
#1 0x08048fc6 in main ()
I’m guessing
fileisNULL, probably indicating that*fileNamedoesn’t exist.Note that statements like this:
don’t do what you probably expected. That statement is equivalent to:
And that should have given you a compiler warning, since you’re assigning an
intto achar*.Instead, you probably meant to use
snprintfto useprintf-style formatting to construct the filename.For your second crash: You have an extra layer of pointers that you don’t need on
array. Declare it aschar array...and remove the*when you access it. As it stands now, you’ve told the compiler that the elements will be pointers, but you haven’t made them point anywhere, and then you asked the compiler to go look where they’re pointing using*. Boom!Your final use of
arraythen needs to make a pointer to each character to pass tofwrite. You’d do that with the&operator, called “address-of”, used like&array....The address-of operator is the opposite of
*. Once you have the program working, you can use&other places to simplify your code. For example, instead of declaringint *widthand allocating it off the heap usingmalloc, you can remove the*everywhere and pass&widthtoscanf.Since we’re back to the
freadsegfault: Check the return value offopenbefore you use it. If it’sNULL, print an error message.That will probably tell you what’s wrong. However, this isn’t debugging code. You should generally check for error returns from the functions you call.