allocate memory in function

P

Pushker Pradhan

This is similar to my post a few days ago, I'm now doing it correctly but
somehow I get Segmentation errors:
My function gen_matrix allocates memory for x and returns x with some
content:
void gen_matrix(int **x, int numRows, int numCols)
{
long i;
*x = (int *)calloc( (numRows*numCols), sizeof(int) );
for(i = 0; i < numRows*numCols; i++) {
*x = rand();
}
}

Call in main.c:
int *mat = NULL;
int numrows, numcols;
numrows = atoi(argv[1]);
numcols = atoi(argv[2]);
gen_matrix(&mat, numrows, numcols);

This is the error (the memory allocation: calloc fails!):
psp1@leto:~/pdc$ ./main 4 4
Bus Error (core dumped)

Can somebody tell what's wrong?
 
R

Richard Heathfield

Pushker said:
This is similar to my post a few days ago, I'm now doing it correctly but
somehow I get Segmentation errors:
My function gen_matrix allocates memory for x and returns x with some
content:
void gen_matrix(int **x, int numRows, int numCols)
{
long i;
*x = (int *)calloc( (numRows*numCols), sizeof(int) );

Unnecessary cast. May be hiding a failure to #include <stdlib.h>

No check for success of call.

*x = calloc(numRows * numCols, sizeof **x);
if(*x != NULL)
{
for(i = 0; i < numRows*numCols; i++) {
*x = rand();


You meant: (*x) = rand();
 
P

Pushker Pradhan

Actually I tried without the cast but still get the same errors, yes I have
included stdlib.h
I now check for *x == NULL but the calloc fails for some other reason it
seems.

--
Pushkar Pradhan
Richard Heathfield said:
Pushker said:
This is similar to my post a few days ago, I'm now doing it correctly but
somehow I get Segmentation errors:
My function gen_matrix allocates memory for x and returns x with some
content:
void gen_matrix(int **x, int numRows, int numCols)
{
long i;
*x = (int *)calloc( (numRows*numCols), sizeof(int) );

Unnecessary cast. May be hiding a failure to #include <stdlib.h>

No check for success of call.

*x = calloc(numRows * numCols, sizeof **x);
if(*x != NULL)
{
for(i = 0; i < numRows*numCols; i++) {
*x = rand();


You meant: (*x) = rand();

--
Richard Heathfield : (e-mail address removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
P

Pushker Pradhan

Please ignore this post, Richard's reply was helpful. Thank you

--
Pushkar Pradhan
Pushker Pradhan said:
Actually I tried without the cast but still get the same errors, yes I have
included stdlib.h
I now check for *x == NULL but the calloc fails for some other reason it
seems.

--
Pushkar Pradhan
Richard Heathfield said:
Pushker said:
This is similar to my post a few days ago, I'm now doing it correctly but
somehow I get Segmentation errors:
My function gen_matrix allocates memory for x and returns x with some
content:
void gen_matrix(int **x, int numRows, int numCols)
{
long i;
*x = (int *)calloc( (numRows*numCols), sizeof(int) );

Unnecessary cast. May be hiding a failure to #include <stdlib.h>

No check for success of call.

*x = calloc(numRows * numCols, sizeof **x);
if(*x != NULL)
{
for(i = 0; i < numRows*numCols; i++) {
*x = rand();


You meant: (*x) = rand();

--
Richard Heathfield : (e-mail address removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top