C Malloc 3D array
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
add a comment |
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
Note:x*y*z
inx*y*z*sizeof(double)
can overflowint
math.sizeof(double)*x*y*z
has the advantage of usingsize_t
math, certainly wider thanint
. 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 beint idx = z*j + y*z*i;
?
– Osiris
Nov 18 '18 at 2:11
add a comment |
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
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
c
edited Nov 18 '18 at 1:44
Izyr
asked Nov 18 '18 at 1:39
IzyrIzyr
32
32
Note:x*y*z
inx*y*z*sizeof(double)
can overflowint
math.sizeof(double)*x*y*z
has the advantage of usingsize_t
math, certainly wider thanint
. 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 beint idx = z*j + y*z*i;
?
– Osiris
Nov 18 '18 at 2:11
add a comment |
Note:x*y*z
inx*y*z*sizeof(double)
can overflowint
math.sizeof(double)*x*y*z
has the advantage of usingsize_t
math, certainly wider thanint
. 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 beint 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
add a comment |
1 Answer
1
active
oldest
votes
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).
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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).
add a comment |
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).
add a comment |
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).
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).
edited Nov 18 '18 at 2:24
answered Nov 18 '18 at 2:16
OsirisOsiris
2,568416
2,568416
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Note:
x*y*z
inx*y*z*sizeof(double)
can overflowint
math.sizeof(double)*x*y*z
has the advantage of usingsize_t
math, certainly wider thanint
. 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 beint idx = z*j + y*z*i;
?– Osiris
Nov 18 '18 at 2:11