An application that might cause problem

T

Tony Johansson

Hello!

Is this little nice main program a good solution?? or is it a bad solution.
I can't see any problem with it?

int main()
{
int *vektor = new int[3];
for (int i=0; i<3; i++)
vektor = i;

int ** matris;
matris = new int*[3];
for (int k=0; k<3; k++)
matris[k] = vektor;
}

Many thanks!
//Tony
 
R

Rolf Magnus

Tony said:
Is this little nice main program a good solution??

To what?
or is it a bad solution. I can't see any problem with it?

Well, that piece of code alone isn't very useful. It depends on what you
want to use it for. Anway, did you really want to make an array of pointers
that all point to the same address?
int main()
{
int *vektor = new int[3];
for (int i=0; i<3; i++)
vektor = i;

int ** matris;
matris = new int*[3];
for (int k=0; k<3; k++)
matris[k] = vektor;
}
 
M

MJ

Hi this is a bad practice of code
check this code given below
it will crash if you try to delete matris



int main()
{
int *vektor = new int[3];
for (int i=0; i<3; i++)
vektor = i;

int ** matris;
matris = new int*[3];
for (int k=0; k<3; k++)
matris[k] = vektor;

for ( k=0; k<3; k++)
delete(matris[k]);
delete []matris;
return 0;

}

MJ
 
J

Jesper Madsen

MJ said:
Hi this is a bad practice of code
check this code given below
it will crash if you try to delete matris



int main()
{
int *vektor = new int[3];
for (int i=0; i<3; i++)
vektor = i;

int ** matris;
matris = new int*[3];
for (int k=0; k<3; k++)
matris[k] = vektor;

for ( k=0; k<3; k++)
delete(matris[k]);
delete []matris;
return 0;


I just figured it would leak the vektor not crash..
a delete [] vektor; is missing.. I haven't heard that delete [] should do a
cascading delete..?!?
 
J

Jean-Sebastien Samson

I just figured it would leak the vektor not crash..
a delete [] vektor; is missing.. I haven't heard that delete [] should do
a
cascading delete..?!?

Actually, it could crash because delete and delete[] are different
operators. If, for instance you define a specific operator new/delete which
allocates memory from a different heap than the one operator new[]/delete[]
uses, then you have UB. A crash is eligible as UB.
Besides, delete could free the same amount of memory as delete[] depending
on their implementation (and thus imply no resource leak), but you would not
call the destructors of the objects in the array, which is not a problem if
you have basic types. But my first remark is still valid, you never know if
delete and delete[] will be overriden or not.
 
J

Jesper Madsen

Jean-Sebastien Samson said:
I just figured it would leak the vektor not crash..
a delete [] vektor; is missing.. I haven't heard that delete [] should do
a
cascading delete..?!?

Actually, it could crash because delete and delete[] are different
operators. If, for instance you define a specific operator new/delete which
allocates memory from a different heap than the one operator new[]/delete[]
uses, then you have UB. A crash is eligible as UB.
Besides, delete could free the same amount of memory as delete[] depending
on their implementation (and thus imply no resource leak), but you would not
call the destructors of the objects in the array, which is not a problem if
you have basic types. But my first remark is still valid, you never know if
delete and delete[] will be overriden or not.

I missed the..
for ( k=0; k<3; k++)
delete(matris[k]);
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top