Question : 3d array with pointer to pointer.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joonshik Kim

    #1

    Question : 3d array with pointer to pointer.

    I was trying to define 3d array with pointer to pointer. I wrote like
    following.

    int ***d;
    nx = 3;
    ny = 5;
    nz = 4;

    d = (int ***)malloc((int ) nx*sizeof(int **));
    *d = (int **)malloc((int) nx*ny*sizeof(in t*));
    **d = (int *)malloc((int) nx*ny*nz*sizeof (int));
    for(i = 1; i < nx ; i++) d[i] = d[i-1] + ny;
    for(i = 0; i < nx ; i++)
    for(j = 1; j < ny ; j++) d[i][j] = d[i][j-1] + nz;


    for(i=0 ; i < nx; i ++)
    for(j = 0; j < ny; j++)
    for(k = 0 ; k < nz; k++)
    d[i][j][k] = 100*(i+1) + 10*(j+1) + k;


    it compiled ok. but when I ran it, it shows error message like
    "segementat ion fault.."

    What did I do wrong ? can someone help me ?

    thank you.



  • Richard Heathfield

    #2
    Re: Question : 3d array with pointer to pointer.

    Joonshik Kim said:
    I was trying to define 3d array with pointer to pointer. I wrote like
    following.
    >
    int ***d;
    nx = 3;
    ny = 5;
    nz = 4;
    d = malloc(nx * sizeof *d);
    if(d == NULL) { HANDLE_THE_ERRO R; }

    for(i = 0; i < nx; i++)
    {
    d[i] = malloc(ny * sizeof *d[i]);
    if(d[i] == NULL) { CLEAN_UP; HANDLE_THE_ERRO R; }

    for(j = 0; j < ny; j++)
    {
    d[i][j] = malloc(nz * sizeof *d[i][j]);
    if(d[i][j] == NULL) { CLEAN_UP; HANDLE_THE_ERRO R; }
    for(k = 0; k < nz; k++)
    {
    d[i][j][k] = 42;


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: normal service will be restored as soon as possible. Please do not
    adjust your email clients.

    Comment

    • Joonshik Kim

      #3
      Re: Question : 3d array with pointer to pointer.

      Oh..Thank you..
      It works nice..

      "Richard Heathfield" <invalid@invali d.invalidwrote in message
      news:pqOdnbUa1p pRyMrYnZ2dnUVZ8 qadnZ2d@bt.com. ..
      Joonshik Kim said:
      >
      I was trying to define 3d array with pointer to pointer. I wrote like
      following.

      int ***d;
      nx = 3;
      ny = 5;
      nz = 4;
      >
      d = malloc(nx * sizeof *d);
      if(d == NULL) { HANDLE_THE_ERRO R; }
      >
      for(i = 0; i < nx; i++)
      {
      d[i] = malloc(ny * sizeof *d[i]);
      if(d[i] == NULL) { CLEAN_UP; HANDLE_THE_ERRO R; }
      >
      for(j = 0; j < ny; j++)
      {
      d[i][j] = malloc(nz * sizeof *d[i][j]);
      if(d[i][j] == NULL) { CLEAN_UP; HANDLE_THE_ERRO R; }
      for(k = 0; k < nz; k++)
      {
      d[i][j][k] = 42;
      >
      >
      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: normal service will be restored as soon as possible. Please do not
      adjust your email clients.

      Comment

      • plodger@gmail.com

        #4
        [Horribly OT -- Richard Heathfield please read urgently] Was: Re: Question : 3d array with pointer to pointer.

        Richard Heathfield wrote:
        [snip]
        --
        Richard Heathfield
        "Usenet is a strange place" - dmr 29/7/1999

        email: normal service will be restored as soon as possible. Please do not
        adjust your email clients.
        Hi Richard, it's Sarah Thompson here. I've been trying to contact you
        urgently, but haven't succeeded through usual channels, so many
        apologies for posting here but I am not aware of any alternative.

        You need to contact your brother Robert urgently in connection with a
        family matter. Feel free to email me directly and I'll fill you in on
        the details.

        Best regards,
        Sarah (thompson a t email dt arc dt nasa d t gov)

        Comment

        • mark_bluemel@pobox.com

          #5
          Re: Question : 3d array with pointer to pointer.


          Joonshik Kim wrote:
          I was trying to define 3d array with pointer to pointer. I wrote like
          following.
          >
          int ***d;
          nx = 3;
          ny = 5;
          nz = 4;
          >
          d = (int ***)malloc((int ) nx*sizeof(int **));
          You store one address into d.
          *d = (int **)malloc((int) nx*ny*sizeof(in t*));
          You store one address at *d.
          **d = (int *)malloc((int) nx*ny*nz*sizeof (int));
          You store one address at **d.
          for(i = 1; i < nx ; i++) d[i] = d[i-1] + ny;
          for(i = 0; i < nx ; i++)
          for(j = 1; j < ny ; j++) d[i][j] = d[i][j-1] + nz;
          You now try to access 60 addresses. What is wrong with this picture?

          Comment

          Working...