How to put values to 3d array which is dynamically allocated in C++

  c++

I am writing some codes to understand how dynamically allocating 3d array works in C++.
This is just a sample code I wrote.

#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
using namespace std;

int Na = 3;
int Nd = 3;
int Ny = 3;

int main() {

    double*** tempmax = new double** [Na];
    for (int i = 0; i < Na; i++) {
        tempmax[i] = new double* [Nd];
        for (int j = 0; j < Nd; j++) {
            tempmax[i][j] = new double[Ny];
            fill_n(tempmax[i][j], Ny, 2);
        }
    }
    
    tempmax[2][2][1] = 3;
    
}

First, I allocate ‘tempmax’ 3d array filled with 2.
Then I was trying to change the value to 3 for tempmax[2][2][1].
Then I got this warning message "Warning C6386 Buffer overrun while writing to ‘tempmax’: the writable size is ‘int Na*8′ bytes, but ’24’ bytes might be written."

Some people say that sometimes this is just a false warning.
And I just want to check whether this is false or there is some basic mistake I am making here.

Source: Windows Questions C++

LEAVE A COMMENT