(Link to AcmlmWiki) Offline: thank ||bass
Register | Login
Views: 13,040,846
Main | Memberlist | Active users | Calendar | Chat | Online users
Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album
06-15-24 05:43 PM
0 users currently in Programming.
Acmlm's Board - I3 Archive - Programming - Newbie-ish C++ problem - dynamic arrays
  
User name:
Password:
Reply:
 
Options: - -
Quik-Attach:
Preview for more options

Max size 1.00 MB, types: png, gif, jpg, txt, zip, rar, tar, gz, 7z, ace, mp3, ogg, mid, ips, bz2, lzh, psd

UserPost
Guy Perfect
Posts: 388/451
In C++, using the Vector of the Standard Template Library is undoubtedly the easiest way to manage dynamic arrays.
MisterJones
Posts: 124/125
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.
Matrixz
Posts: 13/16
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.
MathOnNapkins
Posts: 863/1106
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
Posts: 11/16
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.
Acmlm's Board - I3 Archive - Programming - Newbie-ish C++ problem - dynamic arrays


ABII

Acmlmboard 1.92.999, 9/17/2006
©2000-2006 Acmlm, Emuz, Blades, Xkeeper

Page rendered in 0.010 seconds; used 347.27 kB (max 383.96 kB)