1

I want to allocate memory for 3d array in c++ one by one like..

typedef struct {
int id;int use;
}slotstruct;
slotstruct slot1[3][100][1500];  // This should be 3d array
for(i=0;i<3;i++){
  for(j=0;j<100;j++){
     for(k=0;k<1500;k++){
         slot1[i][j][k] = (slotstruct *)calloc(1,sizeof(slotstruct));
      }
   }
}

i have used this code but i am getting segmentation fault..

Nilam Naghor
  • 57
  • 3
  • 12
  • 2
    You have wrong idea about arrays, nevertheless, this code won't give you segmentation fault. This code won't compile at all, even when wrapped in a function with all required declarations and includes. Post your real code. – michalsrb Sep 05 '16 at 10:55
  • 1
    I'm afraid that what you call C++ it's actually C. A couple of standards old, too. – Bob__ Sep 05 '16 at 12:56
  • @Bob__ this is just small part of my project.. my whole project is in c++ so by this small code you won't be able to differentiate – Nilam Naghor Sep 05 '16 at 18:53
  • @michalsrb in my project there are 3 beams for each beam there is 100 channels and in each channel there are 1500 slots so in main fun i will call this method to allocate memory.. – Nilam Naghor Sep 05 '16 at 18:57
  • Have you considered to use a class with a 1D std::vector and a custom accessor? – Bob__ Sep 05 '16 at 18:59
  • @Bob__ sorry but i am not able to understand what you are trying to ask me. – Nilam Naghor Sep 05 '16 at 19:02
  • I mean something like [this](https://ideone.com/wwsIlq). – Bob__ Sep 05 '16 at 20:15

3 Answers3

1

Write

slotstruct ( *slot1 )[100][1500];

slot1 = calloc( 1, 3 * sizeof( *slot1 ) ); 

Or try something like the following

slotstruct ***slot1;

slot1 = malloc( 3 * sizeof( slotstruct ** ) );

for ( int i = 0; i < 3; i++ )
{ 
    slot1[i] = malloc( 100 * sizeof( slotstruct * ) );
    for ( int j = 0; j < 100; j++ )
    {
        slot1[i][j] = calloc( 1, 1500 * sizeof( slotstruct ) );
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • this is not as per my requirement.. when i print addresses using thid code snippet i am getting addresses like. 16135008 , 161350016 , 161350024 , 16137024 while i need like 5008,50016,50024,50032,50040,50048 – Nilam Naghor Sep 05 '16 at 12:16
  • @nlm you have tried the second snippet, what about the first? – Bob__ Sep 05 '16 at 13:02
  • i want to allocate dynamically.. i think first method would be static way of allocation. – Nilam Naghor Sep 05 '16 at 18:51
  • @nlm The both shown methods allocate memory dynamically. – Vlad from Moscow Sep 06 '16 at 15:09
  • in first one it is dynamic but not one by one.. three conditions should be satisfied one by one allocation + dynamically + continuous.. – Nilam Naghor Sep 07 '16 at 11:32
  • @nlm Use the second one. What is the problem? – Vlad from Moscow Sep 07 '16 at 13:33
  • when i am using first one in my code it is giving err like calloc:invalid conversion from (void*) to slotstruct(*)[100][1500].. i think it is c++/c err.. because same code executed with c compiler it does not give err but when i am using c++ compiler it gives err.. and my project is in c++ so i have to use c++ compiler.. – Nilam Naghor Sep 08 '16 at 10:53
  • @nlm You are compiling the program as a C++ program instead of a C ptogram. In C this statement is valid. In C++ you have to use operator new instead of calloc or malloc. – Vlad from Moscow Sep 08 '16 at 13:08
  • @VladfromMoscow but by using new it will not allocate memory continuous.. my primary requirement is continuous memory allocation.. – Nilam Naghor Sep 08 '16 at 15:57
  • @nlm You adked about calloc. I showed how one extent can be allocated using callo. You can also use the new operator in C++ to allocate one extent. – Vlad from Moscow Sep 08 '16 at 15:59
0

Calculate the total amount of memory needed first then allocate memory first for main array and the sub arrays as below. It won't cause segmentation fault. Even you can check the addresses also they are contiguous. Try below code it works fine for me:

typedef struct
{
int id;
int use;
}slotstruct;

main()
{
        int i,j,k;
        char row=2 ,col =3, var=3;
        //char **a=(char**)malloc(col*sizeof(char*));
        slotstruct*** a =(slotstruct***)calloc(col,sizeof(slotstruct*));

        for(i=0;i<col;i++)
                a[i]=(slotstruct**)calloc(row,sizeof(slotstruct*));

        for(i=0;i<col;i++)
                for(j=0;j<row;j++)
                        a[i][j]=(slotstruct*)calloc(var,sizeof(slotstruct*));


        int cnt=0;
        for( i=0;i<col;i++)
                for( j=0;j<row;j++)
                {
                        for(k=0;k<var;k++)
                                a[i][j][k].id=cnt++;
                }

        for(i=0;i<col;i++)
                for(j=0;j<row;j++)
                {
                        for(k=0;k<var;k++)
                                printf("%d ",a[i][j][k].id);
                                printf("%u ",&a[i][j][k]);
                        printf("\n");
                }
}
Stubborn
  • 780
  • 1
  • 5
  • 20
-1

You've already allocated memory when you wrote

slotstruct slot1[3][100][1500]

Did you mean to write following?

slotstruct ***slot1
patentfox
  • 1,436
  • 2
  • 13
  • 28