I want to dynamically generate a two-dimensional array and initialize it to a specific integer value.So I coded it as follows.
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <tuple>
using namespace std;
int main(){
// map 입력받기
int N,M;
cin>> N >> M;
int** map_= new int*[N];
for(int i=0; i< N; i++){
map_[i]=new int[M];
}
fill(&map_[0][0], &map_[N][M], 2);
cout << " map[0][0] " << map_[0][0] << endl;
int** visit= new int*[N];
for(int i=0; i< N; i++){
visit[i]=new int[M];
}
fill(&visit[0][0], &visit[N-1][M-1], 0); // -1 : 진입 불가, 0 : 방문 안함, 1>= : 방문
return 0;
}
A segmenation error occurs. What's the reason? please reply
The below code generated in a static array works.
#include <iostream>
#define MAX 5
using namespace std;
int matrix[MAX][MAX] = {0,};
int main(){
fill(&matrix[0][0], &matrix[MAX][MAX], 2);
for (int i=0; i<MAX; ++i){
for (int j=0; j<MAX; ++j){
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
what is difference??