473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

An application that might cause problem

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] = i;

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

Many thanks!
//Tony

Jul 23 '05 #1
5 1118
Tony Johansson wrote:
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] = i;

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


Jul 23 '05 #2
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] = 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

Jul 23 '05 #3

"MJ" <ma********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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] = 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..?!?

}

MJ

Jul 23 '05 #4
> 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.
--
JS
Jul 23 '05 #5

"Jean-Sebastien Samson" <js***@yahoo.fr> wrote in message
news:42**********************@news.free.fr...
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]);
--
JS

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: VinnieT | last post by:
I have a load balanced system that consists of 3 production servers. There are about 20 different applications that are used on these boxes. One of my applications in particular is used more than...
11
by: LordHog | last post by:
Hello, I recently wrote an application that is used for testing units in a burn-in chamber. It uses two external library that require the use of P\Invoke in order to work with them. There is a...
2
by: Ben S | last post by:
Hi I have a reasonably complex ASP.Net application which unexpectedly ends 2 or 3 times a day loosing session info and causing users to log back in. There is comprehensive error tapping and...
5
by: Stephane | last post by:
Hi, I want to keep a list of my visitors in an ArrayList which I place in the application object like this: Application("Visitors") = new ArrayList(); // The list of visitors Then, each...
20
by: Peter Oliphant | last post by:
How does one launch multiple forms in an application? Using Photoshop as an example, this application seems to be composed of many 'disjoint' forms. Yet, they all seem somewhat 'active' in...
9
by: jeff | last post by:
Hi All. I realize that when my Deployed winforms application starts, Windows needs to load the .net 2 framework before control is given to my application.... Is there anyway to either ... -...
4
by: =?Utf-8?B?bWFzbWl0aA==?= | last post by:
Can you restart an ASP.NET application in the Application_Start event when an error occurs so the next request for the application will fire the Application_Start event again? This would allow...
0
by: =?Utf-8?B?SkhhbGV5?= | last post by:
Our system is: IIS Server: dual Intel Xeon 2.80 GHz, 4 GB Ram Windows Server 2003 SP2 IIS 6.0 SQL Server: dual Intel Xeon 2.80 GHz, 4 GB Ram (separate server) Windows Server 2003 SP2 SQL...
1
by: davidmurray1 | last post by:
I have a C++ app on my flash drive that i am running, but often times, i must take the flash drive out of the computer and use it elsewhere while the executable continues to run. however, the...
2
by: Hans van Kruijssen | last post by:
We are using the windows forms webbrowser control in a WPF client application. Very rarely we get the following error message: Message Attempted to read or write protected memory. This is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.