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[i] = 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?
--
Pushkar Pradhan 3 6048
Pushker Pradhan wrote: 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[i] = rand();
You meant: (*x)[i] = rand();
--
Richard Heathfield : bi****@eton.powernet.co.uk
"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
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" <do******@address.co.uk.invalid> wrote in message
news:bk**********@sparta.btinternet.com... Pushker Pradhan wrote:
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[i] = rand();
You meant: (*x)[i] = rand();
-- Richard Heathfield : bi****@eton.powernet.co.uk "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
Please ignore this post, Richard's reply was helpful. Thank you
--
Pushkar Pradhan
"Pushker Pradhan" <pu*****@erc.msstate.edu> wrote in message
news:bk**********@nntp.msstate.edu... 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" <do******@address.co.uk.invalid> wrote in message news:bk**********@sparta.btinternet.com... Pushker Pradhan wrote:
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[i] = rand();
You meant: (*x)[i] = rand();
-- Richard Heathfield : bi****@eton.powernet.co.uk "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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Franklin Lee |
last post by:
Hi All,
I use new to allocate some memory,even I doesn't use delete to release them.
When my Application exit, OS will release them.
Am I right?
If I'm right, how about Thread especally on...
|
by: lixiaoyao |
last post by:
hi all
I use matrix & vector function to allocate the space myself in c,
typedef struct matrix_array newdata;
struct matrix_array{
float **sy,*sxx;
};
newdata ndata;//new data struct...
|
by: gc |
last post by:
I am writing a function that given nx1 vector a and a nx1 b solves a
system of equations f(a,c)=b for a nx1 c.
While writing the function:
1] Should I allocate the memory for c within the...
|
by: M. Moennigmann |
last post by:
Dear all:
I would like to write a function that opens a file, reads and stores
data into an 2d array, and passes that array back to the caller
(=main). The size of the array is not known before...
|
by: Peter Hickman |
last post by:
I have a program that requires x strings all of y length. x will be in the range
of 100-10000 whereas the strings will all be < 200 each.
This does not need to be grown once it has been created....
|
by: shsingh |
last post by:
I have a class A containing some map as data variables. I creat an
object of class A on heap by allocatiing memory by using "malloc". This
will return me the required memory but the object is not...
|
by: lovecreatesbea... |
last post by:
If the built-in operator keyword new doesn't allocate memory on heap
and it calls global operator new (::operator new) or class member
operator new to do that. What are the two kinds of operator...
|
by: dtschoepe |
last post by:
Hi,
I have a homework project I am working on, so be forwarned, I'm new to
C programming. But anyway, having some trouble with a memory
allocation issue related to a char * that is a variable...
|
by: Bryan Parkoff |
last post by:
I want to know how much static memory is limited before execution
program starts. I would write a large array. The large array has 65,536
elements. The data size is double word. The static...
|
by: Steven Powers |
last post by:
Imagine the following setup
class Parent
{
virtual void doStuff();
}
class Child : public Parent
{
virtual void doStuff();
}
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |