Your code with a bit of refactoring:
#include <iostream>
int** create2DArray( const std::size_t rowCount, const std::size_t colCount );
void delete2DArray( int** const array2D, const std::size_t rowCount );
void print2DArray( int** const array2D, const std::size_t rowCount, const std::size_t colCount );
int main( )
{
constexpr std::size_t ROW_COUNT { 8 };
constexpr std::size_t COL_COUNT { 8 };
int** map { create2DArray( ROW_COUNT, COL_COUNT ) };
std::cout << '\n' << "printing map in main" << '\n';
print2DArray( map, ROW_COUNT, COL_COUNT );
// after work is done with array, delete it
delete2DArray( map, ROW_COUNT );
map = nullptr; // to prevent it from becoming a dangling pointer
return 0;
}
int** create2DArray( const std::size_t rowCount, const std::size_t colCount )
{
int** const array2D = new int* [rowCount];
for ( std::size_t row = 0; row < rowCount; ++row )
{
array2D[row] = new int[colCount];
}
for ( std::size_t row = 0; row < rowCount; ++row )
{
for ( std::size_t col = 0; col < colCount; ++col )
{
array2D[row][col] = 1;
}
}
return array2D;
}
void delete2DArray( int** const array2D, const std::size_t rowCount )
{
for ( std::size_t row = 0; row < rowCount; ++row )
{
delete[] array2D[row];
}
delete[] array2D;
}
void print2DArray( int** const array2D, const std::size_t rowCount, const std::size_t colCount )
{
for ( std::size_t row = 0; row < rowCount; ++row )
{
for ( std::size_t col = 0; col < colCount; ++col )
{
std::cout << array2D[row][col] << " ";
}
std::cout << '\n';
}
}
The output:
printing map in main
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
Note #1: When constexpr is used in the declaration of a variable, that variable is evaluated at compile-time and not run-time and becomes a constant expression. So it's good practice to declare variable with constant value (e.g. an immediate like 5 or 3.7) as constexpr.
Check constexpr (C++) for more details.
Note #2: std::size_t is an unsigned integral type. On my compiler it is the equivalent of unsigned long long int (which BTW has a size of 8 bytes).
Here it is from this std::size_t:
typedef /*implementation-defined*/ size_t;
mapbefore and aftercreate(map), that may explain at least part of your problems.