Leave these fields empty (spam trap):
Name
You can leave this blank to post anonymously, or you can create a Tripcode by using the float Name#Password
Comment
[*]Italic Text[/*]
[**]Bold Text[/**]
[~]Taimapedia Article[/~]
[%]Spoiler Text[/%]
>Highlight/Quote Text
[pre]Preformatted & Monospace text[/pre]
1. Numbered lists become ordered lists
* Bulleted lists become unordered lists
File

Sandwich


WEED IS LEGAL IN CANADA! Live 420chan Q&A and Site Merchandise Giveaways on Stream

Live 420chan Q&A, 420chan merchandise giveaways, Logitech hardware giveaways, partying on Twitch tonight!
G502 Giveaway Ends @ Midnight     Q&A Discussion Thread
HOLY SHIT I'M ABOUT TO KILL MYSELF (SEGFAULT AT INDEX 0) by Lillian Puddlesture - Thu, 04 Jan 2018 10:17:51 EST ID:kvqggUwM No.37252 Ignore Report Quick Reply
File: 1515079071468.png -(47908B / 46.79KB, 625x429) Thumbnail displayed, click image for full size. 47908
Has anyone ever had a segfault while accessing an array A at A[0][0]?
So, this is making me go insane. I have a good understanding of C, but I stopped doing anything with it after completing my exam about it (which was about simple client/server applications and implementing a petite GNU find).

Now I have to use it again for a BLOODY SIMPLE SHIT, WHICH IS GENERATING A FUCKING MATRIX. I've been banging my head on the wall for the past hour and nothing seemed to help.

My code segfaults at index (0,0) of the matrix. I isolated the problem by reducing it to a simple function that should print the contents of the matrix, which you can see in the image but I'm going to paste it here anyway:
[Note: not event matrix[0][0] is printed.]

#define RADIUS 7
int ** mask;

void printMatrix(int ** matrix, int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
//segfaults at i = 0, j = 0.
printf("%d",matrix[j]);
}
printf("\n");
}
}

int main(int argc, char ** args) {
printf("%d\n", RADIUS * RADIUS);
mask = (int **) calloc(RADIUS * RADIUS, sizeof(int));
//int mask[RADIUS][RADIUS] doesn't work either
printMatrix(mask,RADIUS);
exit(0);
}
>>
Ian Bingerspear - Thu, 04 Jan 2018 15:09:48 EST ID:5/K3jMSW No.37253 Ignore Report Quick Reply
Obviously the matrix isn't what you think it is. Debugging with print statements like that doesn't help you much. Print more interesting information about matrix or better yet learn to use a debugger.
>>
Ernest Pimmledale - Thu, 04 Jan 2018 20:44:52 EST ID:4Jf4geC2 No.37254 Ignore Report Quick Reply
Okay so I ran your code through a debugger and figured out what happened. Also as one minor thing I think you forgot your #include's for printf and calloc (which I believe are <stdio.h> and <stdlib.h> respectively).

So you're using calloc() which allocates you cleaned memory that is set to all zeros. The NULL pointer is also represented by all zeroes. You should consider how the constructs you are invoking actually work here. What you're allocating with calloc() is a block of memory (in this case, a block of memory of size "RADIUS * RADIUS * sizeof(int)" ) and setting that memory to all zeroes. Then your code, outside of calloc, is creating an int** and pointing it to that newly formed block of zeroed memory.

If you were to access this block of memory as a regular int* array, like this:
int* memory = calloc(RADIUS * RADIUS, sizeof(int) );
for (int x = 0; x < RADIUS * RADIUS; x++) printf("%d,", memory[x]);
Then that would print out RADIUS * RADIUS zeroes, since all of the ints in your block of memory are zero.

Now what you did instead was you made a pointer-array pointing to that block of all zeroed memory. We can indeed print out the pointer values of each element in your array like this:
mask = (int**)calloc(RADIUS * RADIUS, sizeof(int) );
for (int x = 0; x < RADIUS; ++x)
{
int* thisPointer = mask[x];
printf("%p,", thisPointer);
}

The code will now print RADIUS NULL pointers in a row. The reason that all of the pointers in this array are NULL is because in almost all C or C++ compiler implementations, the NULL pointer is equivalent to a pointer to address zero (which is then mapped in most CPU hardware to cause an access violation exception, which stops your program with a crash). What your original crashing code was doing was trying to dereference the NULL pointers in the array of pointers that you made.

Now you can fix your crashing code in several different ways. I'll show you two of them here:

// Fix solution #1
// Allocate arrays of arrays
/* The only change is to how you allocate the arrays in main(), so I won't bother rewriting your printMatrix() function here */
nt main(int argc, char** args)
{
printf("%d\n", RADIUS * RADIUS);
// mask = (int**) calloc(RADIUS * RADIUS, sizeof(int) );
mask = (int**)calloc(RADIUS, sizeof(int*) );
for (int x = 0; x < RADIUS; x++)
{
mask[x] = (int*)calloc(RADIUS, sizeof(int) ); /* Initialize our pointers rather than leaving them all pointing at NULL */
}
// createMask(mask, RADIUS);
printMatrix(mask, RADIUS);
exit(0);
}

// Fix solution #2
// Keep the original block allocation the same, but use a different addressing scheme for access
#define RADIUS 7
int* mask; // Changed mask from an int** to an int*, you'll see why in a sec...
void printMatrix(int* matrix, int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
printf("%d", matrix[j + i * size]); // Change how we're addressing things
}
printf("\n");
}
}

int main(int argc, char** args)
{
printf("%d\n", RADIUS * RADIUS);
mask = (int*)calloc(RADIUS * RADIUS, sizeof(int) ); // Changed mask from an int** to an int*
printMatrix(mask, RADIUS);
exit(0);
return 0;
}

I hope that all of this helps, and if you have any further questions, please don't hesitate to ask and I'll try to explain it the best I can! :)


Report Post
Reason
Note
Please be descriptive with report notes,
this helps staff resolve issues quicker.