Why is the following code causing segmentation fault 11? - c

The following code produces segmentation fault 11 when run, I couldn't figure out why. Could anyone offer some insight? Thanks in advance.
#include <stdio.h>
int main() {
int s, e;
int i,j;
typedef struct coordinate {
int x;
int y;
} coordinate;
typedef struct cell {
int altitude;
coordinate lowest_neighbor;
int visited;
int basin;
} cell;
cell cells[1000][1000];
for (i = 0; i < 1000; i++){
for (j = 0; j < 1000; j++){
cells[i][j].altitude = 9;
}
}
printf("%d", cells[0][0].altitude);
return 0;
}

Because this:
cell cells[1000][1000];
is blowing out your stack. Assuming a four-byte int with no padding in the structures, that array takes up twenty million bytes, quite a lot for a stack.
As a quick fix (for this program anyway), try out:
static cell cells[1000][1000];
which, for many implementations, will take it off the stack.
There are downsides to this approach in general as that means there is only one copy of the array, not one per function call or thread, but that won't matter here since your program only creates it once anyway.

Related

C Segmentation Fault from function that returns the maximum of an array

I just have a function that finds out the maximum value of an array of integers, but I get a segmentation fault, which I can't find because the compiler doesn't show me the line of the error.
This is my C code:
#include <stdlib.h>
#include <stdio.h>
//Funktion ermittelt den größten Wert eines Arrays
int groesstesElement(int **arrayPointer){
int max = 0;
for (int i = 0; i < 3; i++) {
if (*arrayPointer[i]>max) {
max = *arrayPointer[i];
}
}
return max;
}
int main (int argc, char **argv) {
int array[4]={1,2,3,4};
int *ptr = array;
int z = groesstesElement(&ptr);
printf("%d\n", z);
return EXIT_SUCCESS;
}
I use macOS and VSC.
In C, array indexing [] has higher precedence than pointer de-referencing *: https://en.cppreference.com/w/c/language/operator_precedence
Some parentheses fix the segfault.
if ((*arrayPointer)[i]>max) {
max = (*arrayPointer)[i];
}
Due to the operators precedence, with
*arrayPointer[i]
you are telling your program:
Take the i-th element of the array arrayPointer and dereference it.
But arrayPointer is a pointer to int *, so all you get is the address of ptr (the int ** pointer defined from main) with an offset. When you finally dereference it you are likely accessing an invalid address, causing segmentation fault.
As already suggested by the main answer, the fix is done using parenthesis in order to apply the operators in the order you want:
(*arrayPointer)[i]
Secondary issues in the code
Even though with the correction above the program won't crash anymore, it won't work. Searching through the array with the loop
for (i = 0; i < 3; i++)
you won't include the last item in the search (index 3), skipping precisely the index that, in your example, contains the maximum value. You probably meant for (i = 0; i < 4; i++) or for (i = 0; i <= 3; i++).
Anyway, using magic numbers in the code (in this case the dimension of the array) is considered bad practice. A better solution would have been using a #define, but an even better solution would have been passing to the function the size of the array:
#include <stdlib.h>
#include <stdio.h>
#define ARR_SIZE 4
//Funktion ermittelt den größten Wert eines Arrays
int groesstesElement(int *arrayPointer, unsigned int arrSize){
int max = 0, i;
for (i = 0; i < arrSize; i++) {
if (arrayPointer[i]>max) {
max = arrayPointer[i];
}
}
return max;
}
int main (int argc, char **argv) {
int array[ARR_SIZE]={1,2,3,4};
int z = groesstesElement(array, ARR_SIZE);
printf("%d\n", z);
return EXIT_SUCCESS;
}
Please note how there's no need to use a double pointer anymore.

struct segmentation fault gcc on raspberry pi

I'm struggling with a small amount of C code trying to get to run on a RPI2b.
The code is about creating a struct including a integer array with a dynamic size.
The struct file has to be declared as a pointer in my main function.
This is because: The bigger purpose of my program will be executing three Array iterations in parallel by the use of pthread.
As far as I know pthread needs a pointer in function call.
I don't want to change anything by using pthread (never change a winning team).
This is what my code snipping should do:
define a STEP number
declare a static int array with defined amount of STEP
declare a struct including a dynamic array
fill up the static int array
allocate memory for dynamic array
fill up dynamic array with values of static int array
print both on console
No errors while compiling with gcc!
The Problem is that when it Comes to allocate memory for the dynamic array program simply pints a "Segmentation fault" on the console.
The fun fact is: I've copied paste the whole code to Visual Studio 2017 and it works perfectly fine!
Here is my code:
#include<stdio.h>
#include<stdlib.h>
#define STEPS 50 //defined steps
typedef struct file File;
struct file {
int type;
int* array; //dynamic array
};
int main(void) {
int i = 0;
int mainarray[STEPS] = { 0 };
struct file *f = (File*)malloc(sizeof(File)); //allocate memory for struct
printf("Fill up static array\n");
for (i = 0; i <= STEPS; i++) {
mainarray[i] = i;
}
printf("static array values:\n");
for (i = 0; i <= STEPS; i++) {
printf("%d\n", mainarray[i]);
}
printf("allocate memory for dynamic array\n");
f->array = (int*)malloc(sizeof(int) * STEPS); //Here I receive my segmentation fault
if (f->array == NULL) {
printf("allocating error\n");
}
for (i = 0; i <= STEPS; i++) {
f->array[i] = 20000 * mainarray[i];
}
printf("compare of arrays\n");
for (i = 0; i <= STEPS; i++) {
printf("i:=\t%d\tMainarray:\t%d\tStructarray:\t%d\t\n", i, mainarray[i], f->array[i]);
}
free(f->array);
free(f);
}
Your problem is (see comment from Some Programmer Dude) that you go out of bounds of your array. That causes undefined behavior, in your case resulting in a seg fault.
for (i = 0; i <= STEPS; i++)
should be:
for (i = 0; i < STEPS; i++)
because STEPS-1 is the last valid array element.

memcpy() and memmove() not working as expected

I ran into this problem while working with a struct array within a struct. I'm trying to set the entry array in the dict using memcpy. I'm getting a mixture of the expected values and some seemingly random integers as output.
Oddly enough, I tried this code with some online compilers, and it worked fine. I thought that it might have to do with overlapping memory regions, so I tried memmove() but the result was the same.
I think I might be using malloc improperly for the dict, but I'm not sure. It seems I can't use malloc for the internal array, or for the individual elements. I'd appreciate any help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
int i;
} entry;
typedef struct{
entry e[10];
int i;
} dict;
dict* d;
void test(dict* di){
printf("%d\n", di->i);
int k;
for (k = 0; k < 10; k ++){
printf(("%d\n"),di->e[k].i);
}
}
int main(){
entry en[10];
d = malloc(sizeof(d));
int k;
for (k = 0; k < 10; k++){
en[k].i = k;
}
d->i = 50;
memcpy(d->e, en, 10*sizeof(entry));
test(d);
return 0;
}
d = malloc(sizeof(d));
d is a dict*; sizeof d is sizeof(dict*). Looks like you meant sizeof *d.

Beginner Here Rand in c

im a 1st grader when it comes to c and need help with storing 5 random values in an array and outputting them. Heres where am at.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
struct score_card {int A_ones; int B_twos; int C_threes; int D_fours; int E_fives; int F_sixes; int G_chance;};
int dice_rolls[5];
int randomize(void);
int value;
int main(void) {
struct score_card test;
randomize;
int i;
for(i = 0; i <= 4; i++){
printf("%d\n", dice_rolls[i]);
}
printf("\n");
return 0;
}
int randomize(void){
int i;
srand(time(0));
for(i = 0; i <= 4; i++){
value = rand() % 6 + 1;
dice_rolls[i] = value;
}
}
The output is :
6294304
6294308
6294312
6294316
6294320
the goal was to use modular division to get values from 1 -->6 and store them in the dicerolls array.
I see two immediate problems.
First. you're not terminating your random numbers with a newline. That's why they're all strung together in a big sequence. Change your output line to:
printf("%d\n", &dice_rolls[i]);
Secondly, you're not actually calling randomize. The correct way to call it is with:
randomize();
The statement randomize; is simply an expression giving you the address of the function. It's as useless in this case as the expression 42; which also does nothing. However it's valid C so the compiler doesn't necessarily complain.

Random matrix struct creation

I'm trying to make a struct that generates a random matrix and am getting "error: expected â=â, â,â, â;â, âasmâ or â_attribute_â before âmatrixâ" when compiling. How can I get this to work effectively and efficiently?
I guess expected errors usually are caused by typos but I don't see any.
I'm very new to C so pointers and malloc are quite foreign to me. I really appreciate your help.
/* It's called RandomMatrixMaker.c */
#include <stdio.h>
#include <stdlib.h>
typdef struct {
char* name;
int MID;
int MRows;
int MCols;
long[][]* MSpace;
} matrix;
matrix makeRIDMatrix(char* name, int MID, int MRows, int MCols) {
matrix m;
static int i, j, r;
m.name = name;
m.MID = MID;
m.MRows = MRows;
m.MCols = MCols;
for (i=0; i<m.MRows; i++) {
for (j=0; i<m.MCols; j++) {
r = random(101);
*(m.MSpace[i][j]) = r;
}
}
return m;
}
int main(void) {
makeRIDMatrix("test", 1, 10, 10);
return 0;
}
There is indeed a typo. You misspelled typedef:
typdef struct {
should be:
typedef struct {
EDIT:
Also, there's no reason to use static here:
static int i, j, r;
You can just get rid of the static modifier.
int i, j, r;
As another poster mentioned, there's a typo, but even with that corrected, it wouldn't compile, due to the definition of matrix.MSpace.
Let's begin in makeRIDMatrix(). You've declared an automatic (stack) variable of type "matrix". At the end of the function, you return that object. Whilst this is permissible, it's not advisable. If the struct is large, you will be copying a lot of data unnecessarily. Better to pass a pointer to a matrix into makeRIDMatrix(), and have makeRIDMatrix() fill in the contents.
The test in the inner loop is against i, but should be against j.
Next, let's look at the definition of "matrix". The definition of "MSpace" is a mess, and wouldn't even compile. Even if it did, because you haven't defined the length of a row, the compiler would not be able to calcuate the offset to any given item in the array. You want a two-dimensional array without giving the row length, but you can't do that in C. You can in other languages, but not C.
There's a lot more I could point out, but I'd be missing the real point. The real point is this:
C Is Not Java.
(It's also not one of the interpreted languages such as JavaScript, PHP, Python, Ruby and so on.)
You don't get dynamically-expanding arrays; you don't get automatic allocation of memory; you don't get garbage collection of unreferenced memory.
What you need is something more like this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
char* name;
int MID;
unsigned int MRows;
unsigned int MCols;
long *MSpace;
} matrix;
void makeRIDMatrix(matrix *pmx, char* name, int MID,
unsigned int MRows, unsigned int MCols) {
int i, j;
long *MSpace = malloc(sizeof(*MSpace)*MRows*MCols);
if (MSpace == NULL) {
return;
}
pmx->name = name;
pmx->MID = MID;
pmx->MRows = MRows;
pmx->MCols = MCols;
pmx->MSpace = MSpace;
srandom((unsigned int)time(NULL));
for (i=0; i<MRows; i++) {
for (j=0; i<MCols; j++) {
long int r = random() % 101L;
*(MSpace++) = r;
}
}
}
inline long * item_addr(const matrix *pmx,
unsigned int row, unsigned int col) {
if (pmx == NULL || pmx->MSpace == NULL
|| row >= pmx->MRows || col >= pmx->MCols) {
return NULL;
}
return &(pmx->MSpace[row * pmx->MCols + col]);
}
long get_item(const matrix *pmx, unsigned int row, unsigned int col) {
long *addr = item_addr(pmx, row, col);
return addr == NULL ? 0L : *addr;
}
void set_item(matrix *pmx,
unsigned int row, unsigned int col,
long val) {
long *addr = item_addr(pmx, row, col);
if (addr != NULL) {
*addr = val;
}
}
int main(void) {
matrix m;
makeRIDMatrix(&m, "test", 1, 10, 10);
return 0;
}
Note a few things here. Firstly, for efficiency, I fill the array as if it were one-dimensional. All subsequent get/set of array items should be done through the getter/setter functions, for safety.
Secondly, a hidden nasty: makeRIDMatrix() has used malloc() to allocate the memory - but it's going to be job of the calling function (or its successors) explciitly to free() the allocated pointer when it's finished with.
Thirdly, I've changed the rows/cols variables to unsigned int - there's little sense in definining an array with negative indices!
Fourthly: little error checking. For example, makeRIDMatrix() neither knows nor cares whether the parameter values are sensible (e.g. the matrix pointer isn't checked for NULLness). That's an exercise for the student.
Fifthly, I've fixed your random number usage - after a fashion. Another exercise for the student: why is the way I did it not good practice?
However - all of this is moot. You need to get yourself a good C textbook, or a good online course, and work through the examples. The code you've given here shows that you're punching above your weight at the moment, and you need to develop some more C muscles before going into that ring!
In relation to your question about "variable sized arrays", you could have something like:
/* can stick this into your struct, this is just an example */
size_t rows, cols;
long **matrix;
/* set the values of rows, cols */
/* create the "array" of rows (array of pointers to longs) */
matrix = (long**)malloc(rows * sizeof(long*));
/* create the array of columns (array of longs at each row) */
for (i = 0; i < rows; i++)
matrix[i] = (long*)malloc(cols * sizeof(long));
/* ... */
/* free the memory at the end */
for (i = 0; i < rows; i++)
free(matrix[i]);
free(matrix);
Then you can just access the dynamically allocated matrix similar to any other array of arrays.
ie. to set element at the first row (row 0) and fourth column (column 3) to 5:
matrix[0][3] = 5;

Resources