A strange error occur about allocate array and rand() - arrays

I want to fill big array with rand() function, when I define my array by int h_in[N],the program crash in vs 2010,to my surprise, when I copy it to the online complier ideone a linkand everything is ok.Finally I define array by h_in = (int *)malloc(N * sizeof(int)) in VS 2010,the program works.I can't figure out that and hope somebody point out my error.
#include <stdio.h>
#include <stdlib.h>
const int N = 1024 * 1024;
int main()
{
//int *h_in = (int *)malloc(N * sizeof(int));
int h_in[N];
float sum = 0.0f;
srand(1);
for(unsigned int i = 0; i < N; i++) {
h_in[i] = (rand() & 0xFF);
}
return 0;
}

int h_in[N];
is allocated on the stack.
int * h_in = malloc(N * sizeof(int));
is allocated on the heap. [BTW: don't cast the result of malloc()]
The default stack size is 1MB, so you should use a linker option to increase it:
/F (Set Stack Size)

Related

C segmentation fault with very large array at specific indices

I have a program that deals with very large arrays, and when I'm trying to populate the array with random values, it always segfaults at a specific index. On Mac OSX 10.10 running XCode, it segfaults at index 1000448, and on GCC targeting LLVM version 6.1.0 it faults at 1001472.
Here is my code
#include <stdlib.h>
#include <stdio.h>
#define WIDTH 1000
#define HEIGHT 1000
/////////////////////////////////////////////////////////
// Program main
/////////////////////////////////////////////////////////
int main(int argc, char** argv) {
// set seed for rand()
srand(2006);
// 1. allocate host memory for matrices A and B
unsigned int length = WIDTH * HEIGHT;
unsigned int size = sizeof(int) * length;
printf("%i", size);
int* matrixA = (int*) malloc(size);
for(int i = 0; i < size; i++) {
printf("%i\n", i);
matrixA[i] = rand() % 10;
}
free(matrixA);
}
Why is this segfaulting? I checked the size allocated to matrixA, and it appears to be the correct size (4,000,000)
the following code
compiles cleanly
performs the appropriate error checking
#include <stdlib.h>
#include <stdio.h>
#define WIDTH (1000)
#define HEIGHT (1000)
/////////////////////////////////////////////////////////
// Program main
/////////////////////////////////////////////////////////
int main( void )
{
// set seed for rand()
srand(2006);
// 1. allocate host memory for matrices A and B
unsigned int length = WIDTH * HEIGHT;
unsigned int size = sizeof(int) * length;
printf("%u\n", size);
int* matrixA = NULL;
if( NULL == (matrixA = malloc(size) ) )
{// then malloc failed
perror( "malloc failed");
exit( EXIT_FAILURE );
}
// implied else, malloc successful
for(unsigned i = 0; i < length; i++)
{
printf("%i\n", i);
matrixA[i] = rand() % 10;
}
free(matrixA);
} // end function: main
Oops, I just realized what the problem was. I'm looping from 0 to size, instead of length. If someone could tell my why those particular values, though, it would be great

Trouble with RAND_MAX

This simple code:
int main()
{
srand(time(NULL));
const int size = rand() % RAND_MAX;
int numbers[size];
int i;
for (i = 0; i < size; i++)
numbers[i] = rand() % RAND_MAX;
for (i = 0; i < size; i++)
printf("numbers[%d]=%d\n", i, numbers[i]);
}
doesn't compile because the size of array isn't const value.
Why does that happen?
How can I fix it?
With ANSI C89 and C90 standard, you have to know the size of the array in advance at compile time. Only in C99 is it allowed for variable sized arrays.
You can either compile it with a c99 compiler or you could allocate memory on the heap:
int *numbers = malloc(size * sizeof(int));
On older compilers defining the length of an array with a variable i.e. not a constant is prohibited.
Use the malloc() function instead:
#include <stdlib.h>
...
const int size = rand() % RAND_MAX;
int *numbers = malloc(sizeof(*numbers)*size);

Make a 2D array in C with a variable

I have an integer variable x that I need to use to make two 2D arrays but I get an error of "cannot allocate an array of constant size 0". After doing some research I apparently need to use malloc but I have no idea how to apply it to my currently situation.
My two arrays I need:
int firMat[x][5];
int secMat[5][x];
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int x = 2;
int **firMat;//int firMat[x][5];
int **secMat;//secMat[5][x];
int i;
firMat = malloc(x * sizeof(int*));
for(i = 0; i< x; ++i)
firMat[i] = malloc(5 * sizeof(int));
secMat = malloc(5 * sizeof(int*));
for(i = 0; i< 5; ++i)
secMat[i] = malloc(x * sizeof(int));
//do stuff E.g. fir[2][1] = 21;
//release E.g.
//for(i = 0; i< x; ++i)
// free(firMat[i]);
//free(firMat);
return 0;
}
If you're using C99, this will work. It will create a "variable-length array", sadly VLAs have been reduced to "optional" in C11.
To use malloc for this, typically I'd abandon the double-array notation, and treat the memory as a flat one-dimensional array, then array[i][j] becomes ptr[ i*cols + j ].
Try to initialize x like in the example below
#define x 2 //outside the function
and then use x like this
int firMat[x][5];
int secMat[5][x];

Erros in dynamically allocated array in C

I am trying to dynamically allocate a 2D array, put some values, and print output. However it seems that I am making mistake in getting input to program in atoi() function.
Basically when we assign a static 2D array, we declare it as say int a [3][3]. So 3*3 units if int, that much memory gets allocated. Is same thing holds for allocating dynamic array as well?
Here is my code:
#include<stdio.h>
#include<stdlib.h>
int main(int arg,char* argv)
{
int rows = atoi(argv[1]);
int col = atoi(argv[2]);
int rows =3;
int col=3;
int i,j;
int (*arr)[col] = malloc(sizeof (*arr)*rows);
int *ptr = &(arr[0][0]);
int ct=1;
for (i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
arr[i][j]=ct;
ct++;
}
}
printf("printing array \n");
for (i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
printf("%d \t",arr[i][j]);
}
printf("\n");
}
free(arr);
return (0);
}
Program crashes in runtime. Can someone comment?
Try to change the third line to:
int main(int arg,char **argv)
The common method to use dynamic matrices is to use a pointer to pointer to something, and then allocate both "dimensions" dynamically:
int **arr = malloc(sizeof(*arr) * rows);
for (int i = 0; i < rows; ++i)
arr[i] = malloc(sizeof(**arr) * col);
Remember that to free the matrix, you have to free all "rows" in a loop first.
int rows = atoi(argv[1]);
int col = atoi(argv[2]);
int rows =3;
int col=3;
int i,j;
You are defining rows and col twice.... that would never work!
With traditional C, you can only have the array[][] structure for multiple dimension arrays work with compile time constant values. Otherwise, the pointer arithmetic is not correct.
For dynamically sized multi dimensional arrays (those where rows and cols are determined at runtime), you need to do additional pointer arithmetic of this type:
int *a;
int rows=3;
int cols=4;
a = malloc(rows * cols * sizeof(int));
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
a[i*rows + j] = 1;
free(a);
Alternatively, you can use double indirection and have an array of pointers each pointing to a one dimensional array.
If you are using GCC or any C99 compiler, dynamic calculation of multiple dimension arrays is simplified by using variable length arrays:
// This is your code -- simplified
#include <stdio.h>
int main(int argc, const char * argv[])
{
int rows = atoi(argv[1]);
int col = atoi(argv[2]);
// you can have a rough test of sanity by comparing rows * col * sizeof(int) < SIZE_MAX
int arr[rows][col]; // note the dynamic sizing of arr here
int ct=1;
for (int i=0;i<rows;i++)
for(int j=0;j<col;j++)
arr[i][j]=ct++;
printf("printing array \n");
for (int i=0;i<rows;i++)
{
for(int j=0;j<col;j++)
{
printf("%d \t",arr[i][j]);
}
printf("\n");
}
return 0;
} // arr automatically freed off the stack
With a variable length array ("VLA"), dynamic multiple dimension arrays in C become far easier.
Compare:
void f1(int m, int n)
{
// dynamically declare an array of floats n by m size and fill with 1.0
float *a;
a = malloc(m * n * sizeof(float));
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
a[i*n + j] = 1.0;
free(a);
}
With VLA you can write to do the same:
void f2(int m, int n)
{
// Use VLA to dynamically declare an array of floats n by m size and fill with 1.0
float a[m][n];
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
a[i][j] = 1.0;
}
Be aware that unlike malloc / free VLA's handling of requesting a size larger than what is available on the stack is not as easily detected as using malloc and testing for a NULL pointer. VLA's are essentially automatic variables and have similar ease and restrictions.
VLA's are better used for smaller data structures that would be on the stack anyway. Use the more robust malloc / free with appropriate detection of failure for larger data structures.
If you are not using a fairly recent vintage C compiler that supports C99 -- time to get one.

Memory allocation in my simple game [duplicate]

This question already has answers here:
Using malloc for allocation of multi-dimensional arrays with different row lengths
(8 answers)
Closed 9 years ago.
I'm writing the minesweeper game in C. I want be able to play games with different minefields with different sizes and number of mines
I've created such structures to describe my data:
typedef struct t_Place Place;
struct t_Place{
unsigned numberOfMinesNear;
int mine;
int state;
};
typedef struct t_Minefield Minefield;
struct t_Minefield{
int xSize;
int ySize;
unsigned minesNumber;
Place **places;
};
So, now I'm trying to initialize my minefield. I do the following:
void makeGame(Minefield *minefield, unsigned x, unsigned y, unsigned mines){
int i, j;
minefield->places = malloc(x * y * sizeof(Place));
for(i = 0; i < x; i++)
for(j = 0; j < y; j++){
minefield->places[i][j].mine = EMPTY;
minefield->places[i][j].state = HIDDEN;
minefield->places[i][j].numberOfMinesNear = 0;
}
minefield->xSize = x;
minefield->ySize = y;
unsigned minesToPlace = mines;
srand(time(NULL));
while(minesToPlace > 0){
i = rand() % x;
j = rand() % y;
if(minefield->places[i][j].mine)
continue;
minefield->places[i][j].mine = MINE;
minesToPlace--;
}
minefield->minesNumber = mines;
// here will be call of play(minefield) function to start the game
}
int main(){
Minefield *gameField = (Minefield *) malloc(sizeof(Minefield));
makeGame(gameField, DEFAULT_X, DEFAULT_Y, DEFAULT_MINES);
// DEFAULT_X = DEFAULT_Y = DEFAULT_MINES = 10
free(gameField);
return 0;
}
I'm getting segfault at first line of code in makeGame function. What i'm doing wrong? I want allocate memory for my minefield dynamically, not statically.
minefield->places = malloc(x * y * sizeof(Place));
The above memory allocation might be the source of the problem , places is a two star pointer , so there must be two malloc() calls , one to allocate the row number **place pointers , and then another malloc() , to allocate column number of *place pointers of type place.
Here is a SSCCE of allocating/initializing a two star pointer contained inside a structure.
#include <stdio.h>
#include <stdlib.h>
#define ROW_SZ 5
#define COL_SZ 25
typedef struct demo{
char **str;
}demo;
int main()
{
demo *d = malloc( sizeof(demo) );
d->str = malloc(ROW_SZ * sizeof(char*) ); //d->str is assigned char**
for ( i = 0; i < ROW_SZ; i++ )
d->str[i] = malloc(COL_SZ * sizeof(char) ); //d-str[i] is assigned char*
// code here to use d->str[ROW_SZ][COL_SZ]
for ( i = 0; i < ROW_SZ; i++ )
free(d->str[i]);
free(d->str);
free(d);
return 0;
}
This is how I usually see 2D arrays allocated:
minefield->places = malloc(x * sizeof(Place *));
for(i = 0; i < x; i++)
{
minefield->places[i] = malloc(x * sizeof(Place));
}
Try this and see if it makes you segfault vanish.

Resources