How to make two-dimensional array put 'new' to use in C++?

C

chajs226

Hi everyone.. I'm doing programing a maze problem.
I want to make a maze(array[5][7]) that use two-dimensional array...

but I don't know this...

{
int *a[7];
a[7] = new int [5];
}

this is result in segmentation error


help me... Thanks.
 
V

Victor Bazarov

Hi everyone.. I'm doing programing a maze problem.
I want to make a maze(array[5][7]) that use two-dimensional array...

but I don't know this...

{
int *a[7];
a[7] = new int [5];
}

this is result in segmentation error

See FAQ.

V
 
F

fungus

{
int *a[7];
a[7] = new int [5];
}

this is result in segmentation error

When you create a[7] then elements are
in the range 0 to 6. "7" is past the end
of the array.

--
<\___/>
/ O O \
\_____/ FTB. For email, remove my socks.


We¡¯re judging how a candidate will handle a nuclear
crisis by how well his staff creates campaign ads.
It¡¯s a completely nonsensical process.
 
S

Sean Fritz

fungus said:
{
int *a[7];
a[7] = new int [5];
}

this is result in segmentation error

When you create a[7] then elements are
in the range 0 to 6. "7" is past the end
of the array.

Definitely have to be careful with array ranges in C++, most compilers won't
give you an error if you try to go outside the index.
 
S

Salt_Peter

Hi everyone.. I'm doing programing a maze problem.
I want to make a maze(array[5][7]) that use two-dimensional array...

but I don't know this...

{
int *a[7];
a[7] = new int [5];
}

this is result in segmentation error


help me... Thanks.

there are 7 numbers from 0 to 6 and 7 isn't one of them.
 
D

David Harmon

On 5 Jan 2007 21:06:10 -0800 in comp.lang.c++, "(e-mail address removed)"
Hi everyone.. I'm doing programing a maze problem.
I want to make a maze(array[5][7]) that use two-dimensional array...

Avoid 'new' and bare naked arrays in application-level code.

std::vector< std::vector<int> > array(5, std::vector<int>(7));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top