Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7755227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:30:57+00:00 2026-06-01T12:30:57+00:00

I have a code segment that generates a dynamic 3D array of random numbers

  • 0

I have a code segment that generates a dynamic 3D array of random numbers and allocates it to a block of memory. I think I have it correct but how do I test whether or not it is all located in a contiguous block of memory? Is there a way? I have a cout function that prints the address of each element to the screen and it seems to be correct but I can’t be sure. I know that I have to free the memory again too but that will come later.

#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
 int ***dyn_array;
 int **buffer_1;
 int i;
 int j;
 int k;

  int *buffer=new int[5*3*2];//Create a buffer for a block of memory
 dyn_array = new int**[5];
 buffer_1 = new int *[3];

  srand(time(NULL));

  cout<<"The address of the buffer is: "<<buffer<<endl;

 for(i=0;i<5;i++)
 {
  dyn_array[i] = &buffer_1[i*3]; 
    for(j=0;j<3;j++)
    {
     dyn_array[i][j] = &buffer[j*2];
    }
 }


   for(i=0; i<5; i++)
   {
     for(j=0; j<3; j++)
     {
       for(k=0; k<2; k++)
       {
      dyn_array[i][j][k]=rand()%40;
          cout<<"dyn array ["<<i<<"]["<<j<<"]["<<k<<"] is: "<<dyn_array[i][j][k]<<endl;
          cout<<"Memory Address is: "<<&dyn_array[i][j][k]<<endl;
       }
     }
    }
 return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:30:58+00:00Added an answer on June 1, 2026 at 12:30 pm

    Few things first:

    buffer_1 = new int *[3];
    for(i=0;i<5;i++) {
      dyn_array[i] = &buffer_1[i*3];
      ...
    }
    

    You are allocating memory for three integer pointers for buffer_1 (elements 0, 1, 2), but in the loop you access elements 0, 3, 6, 9, 12. (Actually, you take the address of those non-allocated elements, but then you do access them in the loop immediately after) Accessing out-of-bounds elements is undefied behavior

    Also, to answer your main question: the language guarantees that when you allocate an array (statically, or dynamically) the elements occupy a contiguous memory block. Otherwise pointer arithmetics (`int*p=new int[2]; *(p+1)=5;) would not work.

    UPDATE from comments:

    To achieve the multi-dimensional array effect, the easiest way is to go with your int *buffer=new int[5*3*2]; declaration: it allocates memory for 30 elements. To index into it you could use buffer[i*3*2+j*2+k]=n, where 0<=i<5, 0<=j<3, 0<=k<2 and n is an integer. If you were to allocate the array statically (int buffer[5][3][2];), the internal indexing mechanism would be the same.

    Also, use named constants (e.g. const int MAX_DIM1=5;) instead of the raw numbers (aka magic numbers) — will make your code more maintanable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code segment as below that uses BufferedReader to read inputs from
I'm finalizing a code segment that lists the files in a directory. I have
So basically the problem is that I have segment of code that needs to
I have seen this code segment for opening a native activity that chooses one
I have a controller class as in the following code segment: @Controller public class
I have two identical byte arrays in the following segment of code: /// <summary>
I have code that looks like this: template<class T> class list { public: class
I have code that I want to look like this: List<Type> Os; ... foreach
I have code that uses Win API function RegSaveKeyEx to save registry entries to
I have code that uses jquery.slideup and jquery.slidedown How can i know that div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.