multidimensional very large array












0















Hi i want to use multidimensional very large array. I tried following code. It compiles but when i execute it it gives me segmentation fault error.



'int NT = 35; int NX = 25; int NY = 25; int NZ = 25;
double dt = 0.1; double dx = 0.5; double dy = 0.5; double dz = 0.5;

double PosT[NT];
double PosX[NX]; double PosY[NY]; double PosZ[NZ];

for(int i=0;i<NT;i++)
PosT[i] = i*dt+dt;
for(int i=0; i<NX;i++)
PosX[i] = dx*i;
for(int i=0; i<NY;i++)
PosY[i] = dy*i;
for(int i=0; i<NZ;i++)
PosZ[i] = dz*i;

double* b_x=(double*)malloc(NX*NY*NZ*sizeof(double));
double* b_y=(double*)malloc(NX*NY*NZ*sizeof(double));
double** B=(double**)malloc(NX*NY*NZ*NT*sizeof(double*));

if(b_x==NULL||b_y==NULL){
cout<<"Malloc space error!"<<endl;
return 0;
}
for(int ix=0;ix<NX;ix++){
for(int iy=0;iy<NY;iy++){
for(int iz=0;iz<NZ;iz++){
int position=ix*NY*NZ+iy*NZ+iz;
b_x[position] =0.;
b_y[position] =0.;
}
}
}'


but when i work in below part then i got segmentation error, my codes next part is following lines which include 2d arrays. and this 2d array is very large ,
perhaps due to this i am getting segmentation error



    'if(B==NULL){
cout<<"Malloc space error!"<<endl;
return 0;
}
cout<<"work"<<endl;
for(int ix=0;ix<NX;ix++){
for(int iy=0;iy<NY;iy++){
for(int iz=0;iz<NZ;iz++){
int position=ix*NY*NZ+iy*NZ+iz;
for(int it=0;it<NT;it++){
B[position][it]=0.;
}
}
}
}
cout<<"not working"<<endl;'


so code between work and not working has problem which causes segmentation error. Any solutions for this.










share|improve this question



























    0















    Hi i want to use multidimensional very large array. I tried following code. It compiles but when i execute it it gives me segmentation fault error.



    'int NT = 35; int NX = 25; int NY = 25; int NZ = 25;
    double dt = 0.1; double dx = 0.5; double dy = 0.5; double dz = 0.5;

    double PosT[NT];
    double PosX[NX]; double PosY[NY]; double PosZ[NZ];

    for(int i=0;i<NT;i++)
    PosT[i] = i*dt+dt;
    for(int i=0; i<NX;i++)
    PosX[i] = dx*i;
    for(int i=0; i<NY;i++)
    PosY[i] = dy*i;
    for(int i=0; i<NZ;i++)
    PosZ[i] = dz*i;

    double* b_x=(double*)malloc(NX*NY*NZ*sizeof(double));
    double* b_y=(double*)malloc(NX*NY*NZ*sizeof(double));
    double** B=(double**)malloc(NX*NY*NZ*NT*sizeof(double*));

    if(b_x==NULL||b_y==NULL){
    cout<<"Malloc space error!"<<endl;
    return 0;
    }
    for(int ix=0;ix<NX;ix++){
    for(int iy=0;iy<NY;iy++){
    for(int iz=0;iz<NZ;iz++){
    int position=ix*NY*NZ+iy*NZ+iz;
    b_x[position] =0.;
    b_y[position] =0.;
    }
    }
    }'


    but when i work in below part then i got segmentation error, my codes next part is following lines which include 2d arrays. and this 2d array is very large ,
    perhaps due to this i am getting segmentation error



        'if(B==NULL){
    cout<<"Malloc space error!"<<endl;
    return 0;
    }
    cout<<"work"<<endl;
    for(int ix=0;ix<NX;ix++){
    for(int iy=0;iy<NY;iy++){
    for(int iz=0;iz<NZ;iz++){
    int position=ix*NY*NZ+iy*NZ+iz;
    for(int it=0;it<NT;it++){
    B[position][it]=0.;
    }
    }
    }
    }
    cout<<"not working"<<endl;'


    so code between work and not working has problem which causes segmentation error. Any solutions for this.










    share|improve this question

























      0












      0








      0








      Hi i want to use multidimensional very large array. I tried following code. It compiles but when i execute it it gives me segmentation fault error.



      'int NT = 35; int NX = 25; int NY = 25; int NZ = 25;
      double dt = 0.1; double dx = 0.5; double dy = 0.5; double dz = 0.5;

      double PosT[NT];
      double PosX[NX]; double PosY[NY]; double PosZ[NZ];

      for(int i=0;i<NT;i++)
      PosT[i] = i*dt+dt;
      for(int i=0; i<NX;i++)
      PosX[i] = dx*i;
      for(int i=0; i<NY;i++)
      PosY[i] = dy*i;
      for(int i=0; i<NZ;i++)
      PosZ[i] = dz*i;

      double* b_x=(double*)malloc(NX*NY*NZ*sizeof(double));
      double* b_y=(double*)malloc(NX*NY*NZ*sizeof(double));
      double** B=(double**)malloc(NX*NY*NZ*NT*sizeof(double*));

      if(b_x==NULL||b_y==NULL){
      cout<<"Malloc space error!"<<endl;
      return 0;
      }
      for(int ix=0;ix<NX;ix++){
      for(int iy=0;iy<NY;iy++){
      for(int iz=0;iz<NZ;iz++){
      int position=ix*NY*NZ+iy*NZ+iz;
      b_x[position] =0.;
      b_y[position] =0.;
      }
      }
      }'


      but when i work in below part then i got segmentation error, my codes next part is following lines which include 2d arrays. and this 2d array is very large ,
      perhaps due to this i am getting segmentation error



          'if(B==NULL){
      cout<<"Malloc space error!"<<endl;
      return 0;
      }
      cout<<"work"<<endl;
      for(int ix=0;ix<NX;ix++){
      for(int iy=0;iy<NY;iy++){
      for(int iz=0;iz<NZ;iz++){
      int position=ix*NY*NZ+iy*NZ+iz;
      for(int it=0;it<NT;it++){
      B[position][it]=0.;
      }
      }
      }
      }
      cout<<"not working"<<endl;'


      so code between work and not working has problem which causes segmentation error. Any solutions for this.










      share|improve this question














      Hi i want to use multidimensional very large array. I tried following code. It compiles but when i execute it it gives me segmentation fault error.



      'int NT = 35; int NX = 25; int NY = 25; int NZ = 25;
      double dt = 0.1; double dx = 0.5; double dy = 0.5; double dz = 0.5;

      double PosT[NT];
      double PosX[NX]; double PosY[NY]; double PosZ[NZ];

      for(int i=0;i<NT;i++)
      PosT[i] = i*dt+dt;
      for(int i=0; i<NX;i++)
      PosX[i] = dx*i;
      for(int i=0; i<NY;i++)
      PosY[i] = dy*i;
      for(int i=0; i<NZ;i++)
      PosZ[i] = dz*i;

      double* b_x=(double*)malloc(NX*NY*NZ*sizeof(double));
      double* b_y=(double*)malloc(NX*NY*NZ*sizeof(double));
      double** B=(double**)malloc(NX*NY*NZ*NT*sizeof(double*));

      if(b_x==NULL||b_y==NULL){
      cout<<"Malloc space error!"<<endl;
      return 0;
      }
      for(int ix=0;ix<NX;ix++){
      for(int iy=0;iy<NY;iy++){
      for(int iz=0;iz<NZ;iz++){
      int position=ix*NY*NZ+iy*NZ+iz;
      b_x[position] =0.;
      b_y[position] =0.;
      }
      }
      }'


      but when i work in below part then i got segmentation error, my codes next part is following lines which include 2d arrays. and this 2d array is very large ,
      perhaps due to this i am getting segmentation error



          'if(B==NULL){
      cout<<"Malloc space error!"<<endl;
      return 0;
      }
      cout<<"work"<<endl;
      for(int ix=0;ix<NX;ix++){
      for(int iy=0;iy<NY;iy++){
      for(int iz=0;iz<NZ;iz++){
      int position=ix*NY*NZ+iy*NZ+iz;
      for(int it=0;it<NT;it++){
      B[position][it]=0.;
      }
      }
      }
      }
      cout<<"not working"<<endl;'


      so code between work and not working has problem which causes segmentation error. Any solutions for this.







      arraylist multidimensional-array segmentation-fault malloc dynamic-arrays






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 27 '18 at 9:49









      IrfanSIrfanS

      122




      122
























          1 Answer
          1






          active

          oldest

          votes


















          0















          int NT = 35; int NX = 25; int NY = 25; int NZ = 25;




          For simplicity, let's change all of these to NT=NX=NY=NZ=2. This line:



          double** B=(double**)malloc(NX*NY*NZ*NT*sizeof(double*));


          would then allocate space for 16 pointers. On the first iteration through the loops, this line:



          B[position][it]=0.;


          would be equivalent to:



          double *tmp = B[0];  // Load uninitialized pointer from B[0]
          tmp[0] = 0.0; // Dereference uninitialized pointer to store something.


          It shouldn't be at all surprising that this code results in a SIGSEGV.



          What you probably meant:



          double *B = malloc(NX*NY*NZ*NT*sizeof(double));
          for(int ix = 0; ix < NX; ix++) {
          for(int iy = 0; iy < NY; iy++) {
          for(int iz = 0; iz < NZ; iz++) {
          for(int it = 0; it < NT; it++) {
          int position = NT * (NZ * (NY * ix + iy) + iz) + it;
          B[position] = 0.0;
          }
          }
          }
          }





          share|improve this answer
























          • but then my position in b_x[position] , b_y[position] and B[position][it] are not same. i want position to be same in b_x, b_y and B , but B further decays with time so [position][it]

            – IrfanS
            Nov 29 '18 at 4:45











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53496862%2fmultidimensional-very-large-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0















          int NT = 35; int NX = 25; int NY = 25; int NZ = 25;




          For simplicity, let's change all of these to NT=NX=NY=NZ=2. This line:



          double** B=(double**)malloc(NX*NY*NZ*NT*sizeof(double*));


          would then allocate space for 16 pointers. On the first iteration through the loops, this line:



          B[position][it]=0.;


          would be equivalent to:



          double *tmp = B[0];  // Load uninitialized pointer from B[0]
          tmp[0] = 0.0; // Dereference uninitialized pointer to store something.


          It shouldn't be at all surprising that this code results in a SIGSEGV.



          What you probably meant:



          double *B = malloc(NX*NY*NZ*NT*sizeof(double));
          for(int ix = 0; ix < NX; ix++) {
          for(int iy = 0; iy < NY; iy++) {
          for(int iz = 0; iz < NZ; iz++) {
          for(int it = 0; it < NT; it++) {
          int position = NT * (NZ * (NY * ix + iy) + iz) + it;
          B[position] = 0.0;
          }
          }
          }
          }





          share|improve this answer
























          • but then my position in b_x[position] , b_y[position] and B[position][it] are not same. i want position to be same in b_x, b_y and B , but B further decays with time so [position][it]

            – IrfanS
            Nov 29 '18 at 4:45
















          0















          int NT = 35; int NX = 25; int NY = 25; int NZ = 25;




          For simplicity, let's change all of these to NT=NX=NY=NZ=2. This line:



          double** B=(double**)malloc(NX*NY*NZ*NT*sizeof(double*));


          would then allocate space for 16 pointers. On the first iteration through the loops, this line:



          B[position][it]=0.;


          would be equivalent to:



          double *tmp = B[0];  // Load uninitialized pointer from B[0]
          tmp[0] = 0.0; // Dereference uninitialized pointer to store something.


          It shouldn't be at all surprising that this code results in a SIGSEGV.



          What you probably meant:



          double *B = malloc(NX*NY*NZ*NT*sizeof(double));
          for(int ix = 0; ix < NX; ix++) {
          for(int iy = 0; iy < NY; iy++) {
          for(int iz = 0; iz < NZ; iz++) {
          for(int it = 0; it < NT; it++) {
          int position = NT * (NZ * (NY * ix + iy) + iz) + it;
          B[position] = 0.0;
          }
          }
          }
          }





          share|improve this answer
























          • but then my position in b_x[position] , b_y[position] and B[position][it] are not same. i want position to be same in b_x, b_y and B , but B further decays with time so [position][it]

            – IrfanS
            Nov 29 '18 at 4:45














          0












          0








          0








          int NT = 35; int NX = 25; int NY = 25; int NZ = 25;




          For simplicity, let's change all of these to NT=NX=NY=NZ=2. This line:



          double** B=(double**)malloc(NX*NY*NZ*NT*sizeof(double*));


          would then allocate space for 16 pointers. On the first iteration through the loops, this line:



          B[position][it]=0.;


          would be equivalent to:



          double *tmp = B[0];  // Load uninitialized pointer from B[0]
          tmp[0] = 0.0; // Dereference uninitialized pointer to store something.


          It shouldn't be at all surprising that this code results in a SIGSEGV.



          What you probably meant:



          double *B = malloc(NX*NY*NZ*NT*sizeof(double));
          for(int ix = 0; ix < NX; ix++) {
          for(int iy = 0; iy < NY; iy++) {
          for(int iz = 0; iz < NZ; iz++) {
          for(int it = 0; it < NT; it++) {
          int position = NT * (NZ * (NY * ix + iy) + iz) + it;
          B[position] = 0.0;
          }
          }
          }
          }





          share|improve this answer














          int NT = 35; int NX = 25; int NY = 25; int NZ = 25;




          For simplicity, let's change all of these to NT=NX=NY=NZ=2. This line:



          double** B=(double**)malloc(NX*NY*NZ*NT*sizeof(double*));


          would then allocate space for 16 pointers. On the first iteration through the loops, this line:



          B[position][it]=0.;


          would be equivalent to:



          double *tmp = B[0];  // Load uninitialized pointer from B[0]
          tmp[0] = 0.0; // Dereference uninitialized pointer to store something.


          It shouldn't be at all surprising that this code results in a SIGSEGV.



          What you probably meant:



          double *B = malloc(NX*NY*NZ*NT*sizeof(double));
          for(int ix = 0; ix < NX; ix++) {
          for(int iy = 0; iy < NY; iy++) {
          for(int iz = 0; iz < NZ; iz++) {
          for(int it = 0; it < NT; it++) {
          int position = NT * (NZ * (NY * ix + iy) + iz) + it;
          B[position] = 0.0;
          }
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 29 '18 at 4:29









          Employed RussianEmployed Russian

          125k20168238




          125k20168238













          • but then my position in b_x[position] , b_y[position] and B[position][it] are not same. i want position to be same in b_x, b_y and B , but B further decays with time so [position][it]

            – IrfanS
            Nov 29 '18 at 4:45



















          • but then my position in b_x[position] , b_y[position] and B[position][it] are not same. i want position to be same in b_x, b_y and B , but B further decays with time so [position][it]

            – IrfanS
            Nov 29 '18 at 4:45

















          but then my position in b_x[position] , b_y[position] and B[position][it] are not same. i want position to be same in b_x, b_y and B , but B further decays with time so [position][it]

          – IrfanS
          Nov 29 '18 at 4:45





          but then my position in b_x[position] , b_y[position] and B[position][it] are not same. i want position to be same in b_x, b_y and B , but B further decays with time so [position][it]

          – IrfanS
          Nov 29 '18 at 4:45




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53496862%2fmultidimensional-very-large-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks