dynamic memory: was not declared in this scope

F

Frank

Hi,

I have the following problem with dynamic memory:

int main(){

for(){

int (**w)=new int *[N1];
for(m = 0; m < N1; m++) {
w[m]= new int[N1];
for(j = 0; j < N1; j++) {
w[m][j] = 0;
}
}

}

delete [] w;

}

This gives the error: 259: error: 'w' was not declared in this scope

I do not understand why a dynamic memory has a scope. (w is defined in
main and not in a class or function).

Does anyone know what is the problem here?

Thanks!

Frank
 
D

Doug

Hi,

I have the following problem with dynamic memory:

int main(){

for(){

int (**w)=new int *[N1];
for(m = 0; m < N1; m++) {
w[m]= new int[N1];
for(j = 0; j < N1; j++) {
w[m][j] = 0;
}
}

}

delete [] w;

}

This gives the error: 259: error: 'w' was not declared in this scope

I do not understand why a dynamic memory has a scope. (w is defined in
main and not in a class or function).

Does anyone know what is the problem here?

Thanks!

Frank

Hi Frank,

I don't think the code you posted will actually compile (that "for()"
looks a bit fishy to me), but I'll take a guess.

I think w goes out of scope at the "}" just before "delete [] w". You
need to move that line up to the same code block where w is actually
declared.
int main(){

for(){

int (**w)=new int *[N1];
for(m = 0; m < N1; m++) {
w[m]= new int[N1];
for(j = 0; j < N1; j++) {
w[m][j] = 0;
}
}

}


}


BTW, you're leaking the 2nd dimension arrays you store in w - you
don't delete those. Since this is obviously an incomplete code
snippet, you might already know that...

Hope that helps,
Doug
 
F

Frank

Hi Doug,

I found the problem. You were right, I define it in the for loop each
time it is executed but I delete only after I finish the complete
loop.

Regarding the 2D array:

Should I use
delete [][]w;

if it is more dimensional?

Frank
 
P

pawel_kunio

Hi Doug,

I found the problem. You were right, I define it in the for loop each
time it is executed but I delete only after I finish the complete
loop.

Regarding the 2D array:

Should I use
delete [][]w;

if it is more dimensional?

Frank

Nope, sorry no possibility to free the whole >1-dimensional array at
once. you should rather free each array like delete[]w[0], delete
[]w[1] etc. and delete [] w afterwards.

regards,
woolf
 
J

Jim Langston

Frank said:
Hi,

I have the following problem with dynamic memory:

int main(){

for()
{

This starts a block.
int (**w)=new int *[N1];

w is local to this block, since it's after the bracket.
for(m = 0; m < N1; m++) {
w[m]= new int[N1];
for(j = 0; j < N1; j++) {
w[m][j] = 0;
}
}

}

The block has ended so w goes out of scope.
delete [] w;

Now you're tryign to work on a variable that is not in scope.

Easy fix. Jsut before your for() statment move the line:
int (**w) = new int *[N1];

Note: I'm not looking at the logic of your program or what it's trying to do
(right or wrong). Just answering your question about the error.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi Doug,

I found the problem. You were right, I define it in the for loop each
time it is executed but I delete only after I finish the complete
loop.

Regarding the 2D array:

Should I use
delete [][]w;

if it is more dimensional?

No, C++ does not have multi-dimensional arrays, only arrays of arrays,
so you'll have to iterate through the "outer" array and run delete[] on
the "inner" ones, something like (not tested):

// w has type int*[]
for (int i = 0; i < N; ++i
delete[] w;
delete[] w;
 
M

Matteo

Hi,

I have the following problem with dynamic memory:

int main(){

for(){

int (**w)=new int *[N1];
for(m = 0; m < N1; m++) {
w[m]= new int[N1];
for(j = 0; j < N1; j++) {
w[m][j] = 0;
}
}

}

delete [] w;

}

This gives the error: 259: error: 'w' was not declared in this scope

I do not understand why a dynamic memory has a scope. (w is defined in
main and not in a class or function).

Does anyone know what is the problem here?

Other people in this thread have mentioned what needs to be done to
fix your problem - I would like to just clarify one point.

You are correct that the dynamic memory does not follow scope - i.e.
the memory you allocated remains around until you explicitly delete it
(or program termination).

However, the _pointer_ to that memory, w, does have scope. w is a
variable that stores the location of the allocated memory, and thus w
follows standard scoping rules (i.e. it is only availible inside the
for loop). So in your case, the memory is still around when you try to
'delete [] w' as you expected, however the pointer to it is
inaccessible.

-matt
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top