|
Thanks.
tim_var has two dimensions: tim_var[1000][6]
Your code below compiled OK:
for (i = 0; i < 1000; i++)
delete [] tim_var[i];
But the following code also compiled:
for (i = 0; i < 1000; i++)
delete tim_var[i];
Which one delete the two dimensional array tim_var[1000][6] ?
Please help
thanks
|
|
|
|
|
mrby123 wrote: tim_var has two dimensions: tim_var[1000][6]
No, tim_var has one dimension (1000), whereas tim_var[0...999] also has one dimension (6). The difference is subtle but important nonetheless.
mrby123 wrote: But the following code also compiled:
for (i = 0; i < 1000; i++)
delete tim_var[i];
It will compile but will result in a memory leak.
mrby123 wrote: Which one delete the two dimensional array tim_var[1000][6] ?
Neither, since that is a stack-based notation.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Hi Cédric,
To delete the two dimensional array tim_var[1000][6], you please comment:
for (i=0;i<1000;i++)
{
delete tim_var[i];
}
or for (i=0;i<1000;i++)
{
delete[] tim_var[i];
}
Thanks
time_var[1000][6] was declared:
float* tim_var[1000];
for (i=0;i<1000;i++)
{
tim_var[i]=new float[6];
}
//
for(i=0;i<1000;i++) {
for(int j=0;j<6;j++)
{
tim_var[i][j]=0;
}
}
//
|
|
|
|
|
There's no need to write such error-prone code (unless you're learning about how things work). Please use an STL container[^] if you're using C++.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Thank you for your comments. Could you give me the code for STL container ? I am a learner.
Thanks for lighting me up.
|
|
|
|
|
I already provided you a link. Click on each container class there and the page would contain an example at the bottom. You might want to search Google for more examples and tutorials.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
mrby123 wrote: Should I put a loop of 1000 to delete ?
Yes.
mrby123 wrote: I am not sure how to put code with a loop to delete.
As Nemanja suggested, you should call delete on every pointer that was assigned by means of new .
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Should I put:
for (i=0;i<1000;i++)
{
delete tim_var[i];
}
or for (i=0;i<1000;i++)
{
delete *tim_var[i];
}
Please comment.
Thanks
|
|
|
|
|
mrby123 wrote: delete tim_var[i];
mrby123 wrote: delete *tim_var[i];
Please try to understand the difference between the two. That'll help you do it on your own. Come up and ask if you have more doubts in the understanding process.
People here are helpful. But they try not to hand code to you in a plate, for your own benefit.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Thanks. In fact,
delete *tim_var[i];
can not be compiled, but
delete tim_var[i];
I think the latter delete a two dimensional array declared and initialized by:
float* tim_var[1000];
for (i=0;i<1000;i++)
{
tim_var[i]=new float[6];
}
for(i=0;i<1000;i++) {
for(int j=0;j<6;j++)
{
tim_var[i][j]=0;
}
}
|
|
|
|
|
consider
int * p;
p = new int[5];
the pointer assigned is p , hence you have to call
delete p;
in order to release memory.
Now try with your actual code.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks CPallini.
Your above code works for one-dimensional array p[5].
But now I got tim_var[1000][6]
|
|
|
|
|
mrby123 wrote: But now I got tim_var[1000][6]
Have you tried:
float **tim_var;
tim_var = new float*[1000]; <<----------
|
for (int x = 0; x < 1000; x++) |
{ |
tim_var[x] = new float[6]; <<----- |
| |
for (int y = 0; y < 6; y++) | |
tim_var[x][y] = 0.0; | |
} | |
| |
for (x = 0; x < 1000; x++) | |
delete [] tim_var[x]; <<---------- |
|
delete [] tim_var; <<-------------------
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Thanks. Your code compiled OK.
Want do you think. I think it will work ?
Thanks
|
|
|
|
|
My intention is to release all memories. The following code was suggested by you. I tried however, seems a problem. I got error message when I run the program: (delete statements got problem). It compiles OK.
"DAMAGE after normal block (#125) at 0x00E40610"
Would you please check ?
|
|
|
|
|
mrby123 wrote: Would you please check ?
Check what? The code snippet I provided you here is fine. If you have added to it or taken from it, that would be the reason for the error.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Thanks. I believe your code is good. I got the problem that is how to declare, define, and call a function passing two dimensional array.
Please correct me. I am trying to delcare, define, and call a two dimensional array like:
declare in the header file:
void CCvib_procDlg::time(float*);
Define:
void CCvib_procDlg::time(float delms[][6])
{ ... code ... }
call:
time(&delms[0][0]);
When I tried to compile, I got:
error C2511: 'time' : overloaded member function 'void (float [][6])' not found in 'CC_vib_procDlg'
Please correct mm. Thanks
|
|
|
|
|
This set does not compile, too.
Declare:
void CC_procDlg::time(float**);
define:
void CC_procDlg::time(float (*delms)[6])
{ code }
Call:
time(delms)
Please correct
|
|
|
|
|
In your original post, you allocated an array of 1000 pointers on the stack.
float * tim_var[1000];
then, for every pointer, you allocated a memory buffer (6 float s) on the heap.
for (i=0;i<1000;i++)
{
tim_var[i] = new float[6];
}
Heap-allocated memory must be explicitely released, hence
for (i=0;i<1000;i++)
{
delete tim_var[i];
}
On the other hand, you must not release stack-allocated memory.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
My intention is to release all memories. The following code was suggested by a guru. I tried however, seems a problem. I got error message when I run the program: (delete statements got problem).
"DAMAGE after normal block (#125) at 0x00E40610"
////////////////////////////////////////////////////////////////////////////
float **tim_va;
tim_va = new float*[1000];
for (int ix = 0; ix < 1000; ix++)
{
tim_va[ix] = new float[6];
for (int jy = 0; jy < 6; y++)
tim_va[ix][jy] = 0.0;
}
for (ix = 0; ix < 1000; ix++)
delete [] tim_va[ix];
delete [] tim_va;
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
The code you're reporting doesn't compile. However if you modify it in order to make the compiler happy, it will run fine.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks. Would you please modify the code to make it work ?
Thanks
|
|
|
|
|
float **tim_va;
tim_va = new float*[1000];
for (int ix = 0; ix < 1000; ix++)
{
tim_va[ix] = new float[6];
for (int jy = 0; jy < 6; jy++)
tim_va[ix][jy] = 0.0;
}
for (int ix = 0; ix < 1000; ix++)
delete [] tim_va[ix];
delete [] tim_va;
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hi
Thanks. The code you put are the same as I reported before. I can not see any difference.
Please check. I know you have a very clear concept according to you post before.
|
|
|
|
|
mrby123 wrote: The code you put are the same as I reported before. I can not see any difference.
If you cannot see the difference, please check better.
This thread cannot last forever, I suppose: Good luck.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|