By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

dynamic memory: was not declared in this scope

P: n/a

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

Mar 21 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* Frank:
>
Does anyone know what is the problem here?
Post a complete program that exhibits the problem.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 21 '07 #2

P: n/a
On 21 Mar, 19:08, "Frank" <super...@gmail.comwrote:
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;
}
}
>>>>>>>>>>>delete [] w;

}
}

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

Mar 21 '07 #3

P: n/a

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

Mar 21 '07 #4

P: n/a
On Mar 21, 10:24 pm, "Frank" <super...@gmail.comwrote:
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

Mar 21 '07 #5

P: n/a

"Frank" <su******@gmail.comwrote in message
news:11*********************@e65g2000hsc.googlegro ups.com...
>
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.
>
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?

Mar 21 '07 #6

P: n/a
On 2007-03-21 22:24, Frank wrote:
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[i];
delete[] w;

--
Erik Wikström
Mar 21 '07 #7

P: n/a
On Mar 21, 2:08 pm, "Frank" <super...@gmail.comwrote:
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

Mar 22 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.