![]() |
|
#1
|
|||
|
|||
Error in execution of 2D dynamic arrayI am new to C++ (6 weeks and counting) and am using VC++ 6.0 and receive the attached error (see attached file) when executing my code. I am creating a dynamic 2D array of derived objects, which are used to simulate a medical linear accelerator for those who may be interested.
I am fairly certain it lies around the two delete lines (lines 115-116) at the end of the .cpp file. However as a newbee I am not sure what I have done wrong (apologies for the inelegant coding)! Thanks for taking the time to read this. CPP / C++ / C Code:
CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: Error in execution of 2D dynamic arrayYou are correct that your delete operations are slightly wrong. You need to delete each FieldSyn object before deleting the array. Something like:
CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: Error in execution of 2D dynamic arrayObvious really!Thanks.
|
|
#4
|
|||
|
|||
Re: Error in execution of 2D dynamic arrayI celebrated too soon! I still have an error after inclusion of the for loop for pBeam[j]. Error message is in German, but it states that the read action to the particular address cannot be performed. Any other ideas? Or is this a quirk of VC++ 6.0?
thanks in advance |
|
#5
|
|||
|
|||
Re: Error in execution of 2D dynamic arrayQuote:
CPP / C++ / C Code:
You increment pBeam and then you increment j and then you have a statement with pBeam[j]. The memory access is incorrect for the desired functionality, and, furthermore, it goes out of range of the array, causing the memory access violation error manifestation. 1. You should never change the value of a pointer that you obtained from the new operator, since delete must have the same value to work with. If you need to increment it, then make a copy of the pointer and increment the copy. Use the original pointer value in your delete statement. 2. You really don't want to change pBeam anyhow, since you use j to index into the array. So: 1. Don't increment pBeam. 2. Use the method recommended by fakepoo to delete everything that you allocated. Regards, Dave |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| where is the problem and can you fix it (php) | oggie | MySQL / PHP Forum | 8 | 14-Apr-2008 15:08 |
| Getting a line error in register | oggie | MySQL / PHP Forum | 5 | 13-Apr-2008 16:16 |
| Returning a 2 dimensional Array from a function | vicky_brsh | C++ Forum | 1 | 04-Jan-2008 14:06 |
| Dynamic Array and Input/Output Files | OnlyCurious | C++ Forum | 11 | 08-Jan-2007 13:49 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 19:31 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The