allocate memory in function

Discussion in 'C Programming' started by Pushker Pradhan, Sep 16, 2003.

  1. 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?
    --
    Pushkar Pradhan
     
    Pushker Pradhan, Sep 16, 2003
    #1
    1. Advertising

  2. 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 = rand();


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

    --
    Richard Heathfield :
    "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
     
    Richard Heathfield, Sep 17, 2003
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:bk85n6$ejn$...
    > 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 = rand();

    >
    > You meant: (*x) = rand();
    >
    > --
    > Richard Heathfield :
    > "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
     
    Pushker Pradhan, Sep 17, 2003
    #3
  4. Please ignore this post, Richard's reply was helpful. Thank you

    --
    Pushkar Pradhan
    "Pushker Pradhan" <> wrote in message
    news:bk87f2$siv$...
    > 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" <> wrote in message
    > news:bk85n6$ejn$...
    > > 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 = rand();

    > >
    > > You meant: (*x) = rand();
    > >
    > > --
    > > Richard Heathfield :
    > > "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

    >
    >
     
    Pushker Pradhan, Sep 17, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green
    Replies:
    3
    Views:
    3,290
    Roedy Green
    Aug 14, 2003
  2. sks_cpp
    Replies:
    7
    Views:
    400
    Jerry Coffin
    Jul 4, 2003
  3. Curt
    Replies:
    37
    Views:
    1,207
    Alexander Terekhov
    Jul 26, 2003
  4. OlgaM
    Replies:
    1
    Views:
    436
    Mike Wahler
    Oct 10, 2003
  5. Alan Gifford
    Replies:
    4
    Views:
    400
    tom_usenet
    Oct 27, 2003
Loading...

Share This Page