C Malloc 3D array












-1















I am trying to run the code below, but I receive an error (during the for loop). It seems that my "3D array" is defined (for i, j, k max) as c[nL_thread][nL][nL] while I am trying to set the indices as c[nL][nL][nL_thread], hence the error.



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<omp.h>
#include<mpi.h>

double ***alloc3(int x, int y, int z){

int i, j;
double *p = (double *) malloc(x*y*z*sizeof(double));
double ***array = (double ***) malloc(x*sizeof(double **));

for (i=0; i<x; i++){
array[i] = (double **) malloc(y*sizeof(double *));
for (j=0; j<y; j++){
int idx = x*j + x*y*i;
array[i][j] = &p[idx];}}

return array;
}

int main(int argc, char *argv){

MPI_Init(&argc, &argv);

int i, j, k;
int nL;
int nL_thread;

double ***c;
int nbr, rank;

MPI_Comm_size(MPI_COMM_WORLD, &nbr);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

nL = 10;
nL_thread = 4;

c = alloc3(nL+1, nL+1, nL_thread+1);

for(k=0; k<=nL_thread; k++){
for(j=0; j<=nL; j++){
for(i=0; i<=nL; i++){
c[i][j][k] = 0;}}}

MPI_Finalize();
}


Thank you in advance for your help.










share|improve this question

























  • Note: x*y*z in x*y*z*sizeof(double) can overflow int math. sizeof(double)*x*y*z has the advantage of using size_t math, certainly wider than int. Important if your x,y,z values are large.

    – chux
    Nov 18 '18 at 1:47











  • "I receive an error (during the for loop)" --> Please describe the error indication and provide any error message.

    – chux
    Nov 18 '18 at 1:48











  • int idx = x*j + x*y*i; should be int idx = z*j + y*z*i;?

    – Osiris
    Nov 18 '18 at 2:11
















-1















I am trying to run the code below, but I receive an error (during the for loop). It seems that my "3D array" is defined (for i, j, k max) as c[nL_thread][nL][nL] while I am trying to set the indices as c[nL][nL][nL_thread], hence the error.



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<omp.h>
#include<mpi.h>

double ***alloc3(int x, int y, int z){

int i, j;
double *p = (double *) malloc(x*y*z*sizeof(double));
double ***array = (double ***) malloc(x*sizeof(double **));

for (i=0; i<x; i++){
array[i] = (double **) malloc(y*sizeof(double *));
for (j=0; j<y; j++){
int idx = x*j + x*y*i;
array[i][j] = &p[idx];}}

return array;
}

int main(int argc, char *argv){

MPI_Init(&argc, &argv);

int i, j, k;
int nL;
int nL_thread;

double ***c;
int nbr, rank;

MPI_Comm_size(MPI_COMM_WORLD, &nbr);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

nL = 10;
nL_thread = 4;

c = alloc3(nL+1, nL+1, nL_thread+1);

for(k=0; k<=nL_thread; k++){
for(j=0; j<=nL; j++){
for(i=0; i<=nL; i++){
c[i][j][k] = 0;}}}

MPI_Finalize();
}


Thank you in advance for your help.










share|improve this question

























  • Note: x*y*z in x*y*z*sizeof(double) can overflow int math. sizeof(double)*x*y*z has the advantage of using size_t math, certainly wider than int. Important if your x,y,z values are large.

    – chux
    Nov 18 '18 at 1:47











  • "I receive an error (during the for loop)" --> Please describe the error indication and provide any error message.

    – chux
    Nov 18 '18 at 1:48











  • int idx = x*j + x*y*i; should be int idx = z*j + y*z*i;?

    – Osiris
    Nov 18 '18 at 2:11














-1












-1








-1








I am trying to run the code below, but I receive an error (during the for loop). It seems that my "3D array" is defined (for i, j, k max) as c[nL_thread][nL][nL] while I am trying to set the indices as c[nL][nL][nL_thread], hence the error.



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<omp.h>
#include<mpi.h>

double ***alloc3(int x, int y, int z){

int i, j;
double *p = (double *) malloc(x*y*z*sizeof(double));
double ***array = (double ***) malloc(x*sizeof(double **));

for (i=0; i<x; i++){
array[i] = (double **) malloc(y*sizeof(double *));
for (j=0; j<y; j++){
int idx = x*j + x*y*i;
array[i][j] = &p[idx];}}

return array;
}

int main(int argc, char *argv){

MPI_Init(&argc, &argv);

int i, j, k;
int nL;
int nL_thread;

double ***c;
int nbr, rank;

MPI_Comm_size(MPI_COMM_WORLD, &nbr);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

nL = 10;
nL_thread = 4;

c = alloc3(nL+1, nL+1, nL_thread+1);

for(k=0; k<=nL_thread; k++){
for(j=0; j<=nL; j++){
for(i=0; i<=nL; i++){
c[i][j][k] = 0;}}}

MPI_Finalize();
}


Thank you in advance for your help.










share|improve this question
















I am trying to run the code below, but I receive an error (during the for loop). It seems that my "3D array" is defined (for i, j, k max) as c[nL_thread][nL][nL] while I am trying to set the indices as c[nL][nL][nL_thread], hence the error.



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<omp.h>
#include<mpi.h>

double ***alloc3(int x, int y, int z){

int i, j;
double *p = (double *) malloc(x*y*z*sizeof(double));
double ***array = (double ***) malloc(x*sizeof(double **));

for (i=0; i<x; i++){
array[i] = (double **) malloc(y*sizeof(double *));
for (j=0; j<y; j++){
int idx = x*j + x*y*i;
array[i][j] = &p[idx];}}

return array;
}

int main(int argc, char *argv){

MPI_Init(&argc, &argv);

int i, j, k;
int nL;
int nL_thread;

double ***c;
int nbr, rank;

MPI_Comm_size(MPI_COMM_WORLD, &nbr);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

nL = 10;
nL_thread = 4;

c = alloc3(nL+1, nL+1, nL_thread+1);

for(k=0; k<=nL_thread; k++){
for(j=0; j<=nL; j++){
for(i=0; i<=nL; i++){
c[i][j][k] = 0;}}}

MPI_Finalize();
}


Thank you in advance for your help.







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 1:44







Izyr

















asked Nov 18 '18 at 1:39









IzyrIzyr

32




32













  • Note: x*y*z in x*y*z*sizeof(double) can overflow int math. sizeof(double)*x*y*z has the advantage of using size_t math, certainly wider than int. Important if your x,y,z values are large.

    – chux
    Nov 18 '18 at 1:47











  • "I receive an error (during the for loop)" --> Please describe the error indication and provide any error message.

    – chux
    Nov 18 '18 at 1:48











  • int idx = x*j + x*y*i; should be int idx = z*j + y*z*i;?

    – Osiris
    Nov 18 '18 at 2:11



















  • Note: x*y*z in x*y*z*sizeof(double) can overflow int math. sizeof(double)*x*y*z has the advantage of using size_t math, certainly wider than int. Important if your x,y,z values are large.

    – chux
    Nov 18 '18 at 1:47











  • "I receive an error (during the for loop)" --> Please describe the error indication and provide any error message.

    – chux
    Nov 18 '18 at 1:48











  • int idx = x*j + x*y*i; should be int idx = z*j + y*z*i;?

    – Osiris
    Nov 18 '18 at 2:11

















Note: x*y*z in x*y*z*sizeof(double) can overflow int math. sizeof(double)*x*y*z has the advantage of using size_t math, certainly wider than int. Important if your x,y,z values are large.

– chux
Nov 18 '18 at 1:47





Note: x*y*z in x*y*z*sizeof(double) can overflow int math. sizeof(double)*x*y*z has the advantage of using size_t math, certainly wider than int. Important if your x,y,z values are large.

– chux
Nov 18 '18 at 1:47













"I receive an error (during the for loop)" --> Please describe the error indication and provide any error message.

– chux
Nov 18 '18 at 1:48





"I receive an error (during the for loop)" --> Please describe the error indication and provide any error message.

– chux
Nov 18 '18 at 1:48













int idx = x*j + x*y*i; should be int idx = z*j + y*z*i;?

– Osiris
Nov 18 '18 at 2:11





int idx = x*j + x*y*i; should be int idx = z*j + y*z*i;?

– Osiris
Nov 18 '18 at 2:11












1 Answer
1






active

oldest

votes


















0














You have a problem in your allocation function. The calculation of the offset idx is wrong:



int idx = x*j + x*y*i;


should be



int idx = z*j + y*z*i;


Note that array[i][j+1] should point to z elements after array[i][j], and array[i+1][j] should point to y*z elements after array[i][j]. That is the reason why the sizes seem switched (but maybe also leads to out of bounds access).






share|improve this answer

























    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%2f53357181%2fc-malloc-3d-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














    You have a problem in your allocation function. The calculation of the offset idx is wrong:



    int idx = x*j + x*y*i;


    should be



    int idx = z*j + y*z*i;


    Note that array[i][j+1] should point to z elements after array[i][j], and array[i+1][j] should point to y*z elements after array[i][j]. That is the reason why the sizes seem switched (but maybe also leads to out of bounds access).






    share|improve this answer






























      0














      You have a problem in your allocation function. The calculation of the offset idx is wrong:



      int idx = x*j + x*y*i;


      should be



      int idx = z*j + y*z*i;


      Note that array[i][j+1] should point to z elements after array[i][j], and array[i+1][j] should point to y*z elements after array[i][j]. That is the reason why the sizes seem switched (but maybe also leads to out of bounds access).






      share|improve this answer




























        0












        0








        0







        You have a problem in your allocation function. The calculation of the offset idx is wrong:



        int idx = x*j + x*y*i;


        should be



        int idx = z*j + y*z*i;


        Note that array[i][j+1] should point to z elements after array[i][j], and array[i+1][j] should point to y*z elements after array[i][j]. That is the reason why the sizes seem switched (but maybe also leads to out of bounds access).






        share|improve this answer















        You have a problem in your allocation function. The calculation of the offset idx is wrong:



        int idx = x*j + x*y*i;


        should be



        int idx = z*j + y*z*i;


        Note that array[i][j+1] should point to z elements after array[i][j], and array[i+1][j] should point to y*z elements after array[i][j]. That is the reason why the sizes seem switched (but maybe also leads to out of bounds access).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 18 '18 at 2:24

























        answered Nov 18 '18 at 2:16









        OsirisOsiris

        2,568416




        2,568416






























            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%2f53357181%2fc-malloc-3d-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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini