|
I use the following to declare, initializing, and delete arrays. Please check if I am doing OK? Thanks!
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;
}
}
//
...........
//
delete [] *tim_var;
|
|
|
|
|
A rule of thumb: for each call to new there needs to be a corresponding delete . In your sample you call new within a loop (1000 times) and have only one delete.
Can you use STL or some other containers instead?
|
|
|
|
|
Thanks.
Should I put a loop of 1000 to delete ? If so could you give me
the correct code to do so. I am not sure how to put code with a loop to delete.
Tahnks again.
|
|
|
|
|
for (i=0;i<1000;i++)
{
delete tim_var[i];
}
and as Nemanja suggested, can't you use the STL container classes ? This will save you a lot of troubles in the future.
|
|
|
|
|
tim_var [][] is a two dimensional array.
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;
}
}
//
So the delete loop you gave me is compiled. So this means the two dimensional array is deleted for 6x1000 variable memories.
Thanks
|
|
|
|
|
mrby123 wrote: tim_var [][] is a two dimensional array.
float* tim_var[1000];
But you've only shown one of those dimensions.
"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
|
|
|
|
|
This was code I put in the beginning to show a two dimensional array
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;
}
}
//
...........
//
delete [] *tim_var;
|
|
|
|
|
mrby123 wrote: This was code I put in the beginning to show a two dimensional array
float* tim_var[1000];
But tim_var only has one dimension: 1000 .
mrby123 wrote: delete [] *tim_var;
Should be:
for (i = 0; i < 1000; i++)
delete [] tim_var[i];
"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.
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
|
|
|
|