Register | Login | |||||
Main
| Memberlist
| Active users
| Calendar
| Chat
| Online users Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album |
| |
0 users currently in Programming. |
Acmlm's Board - I3 Archive - Programming - Newbie-ish C++ problem - dynamic arrays | New poll | | |
Add to favorites | Next newer thread | Next older thread |
User | Post | ||
Matrixz Micro-Goomba Since: 11-28-05 Last post: 6416 days Last view: 6298 days |
| ||
After i tried C++ a bit, i ran into this problem. I went with regular arrays, but that won't do forever. I finnaly decided i need some help with this..
Im using MS Visual C++ 6.0. code start >> #include "stdafx.h" using namespace std; ifstream filedata; char* romdat; long bankmap[2]; char getbyte (long ptr); int main(int argc, char* argv[]) { char* filename; long filesize; long a; filename = "C:\\megaman3.nes"; filedata.open (filename, ios::in|ios::binary|ios::ate); filesize = filedata.tellg(); romdat = new char [filesize]; filedata.seekg(0, ios::beg); filedata.read (romdat, filesize); //cout << filedata.is_open(); filedata.close(); //body_s = body_spos; //oldsize = body_s; bankmap[0] = 0x2C010; bankmap[1] = 0x2E010; bankmap[2] = 0x30010; a = getbyte(0x0); a = getbyte(0x1); return 0; } char getbyte (long ptr) { char t1; long b; b = bankmap[ptr >> 0xD] + (ptr & 0x1FFF); t1 = romdat[0]; //MSVC++ BARKS HERE ABOUT ACCESS VIOLATION. return romdat[bankmap[ptr >> 0xD] + (ptr & 0x1FFF)]; } << code end. It gives a C0000005 Access Violation at the bold line in getbyte. This should be alright.. since "romdat" is a public array pointer, and can be accessed from any function, right? Im feeling its something obvious, but.. i dont know what If i can get to the bottom of this, i can go on with C++ programming. |
|||
MathOnNapkins 1100 In SPC700 HELL Since: 11-18-05 Last post: 6298 days Last view: 6298 days |
| ||
This line:
bankmap[2] = 0x30010; May actually be the offender. Because, you only declared bankmap as an array having two entries: long bankmap[2]; Trying to access this will corrupt your heap since you read beyond the array's bounds. Instead, you should declare it as: long bankmap[3]; Doing illegal reads/writes may not immediately trigger the exception. It may be the next time you access dynamically allocated memory that the exception triggers. I had a very nasty bug like this recently in a program that I was writing. I would recommend writing a class or struct to better manage your allocated memory, but in this case it seems to be something you just did out of ignorance (or unattentiveness) to how arrays work. Accessing romdat in the way you have done doesn't seem to present any glaring errors to me, and this is why I have directed your attention elsewhere. |
|||
Matrixz Micro-Goomba Since: 11-28-05 Last post: 6416 days Last view: 6298 days |
| ||
That was it. Thanks.
I guess im carrying a habit from Visual Basic, since it have you declare arrays with [# of entries] - 1 as argument. Im going to look closer for memory access past boundaries which doesnt give immediate errors, if i get similiar problems.. this is something new to me. |
|||
MisterJones Tooky Since: 12-08-05 From: Mexico Last post: 6388 days Last view: 6324 days |
| ||
You might want to take a look at the Satndard Template Library. Together with the Boost Library, it is almost a blessing for this stuff. | |||
Guy Perfect Since: 11-18-05 Last post: 6300 days Last view: 6298 days |
| ||
In C++, using the Vector of the Standard Template Library is undoubtedly the easiest way to manage dynamic arrays. |
Add to favorites | Next newer thread | Next older thread |
Acmlm's Board - I3 Archive - Programming - Newbie-ish C++ problem - dynamic arrays | | |