SRL Archive - C/C++ Help and Tutorials

"Unhandled exception" Runetime error. HELP!!!!


Macrosoft

2007-11-21 20:17:52

Everytime I debug my script, I get this runtime error:

Unhandled exception at 0x1048153b in SRL 3.81 to SRL 4.exe: 0xC0000005: Access violation writing location 0x00534559.

here is the script

#include <iostream>
#include <fstream>
#include <string>
#include <new>
using namespace std;

int main ()
{
char File[150], re[4];
string line[1];
string response;
int x;
int counter=0;
size_t found;
string strtofind[]={"oolean1", "oolean2", "oolean3", "nteger1", "nteger2", "nteger3",
"tring1", "tring2", "tring3"};
string changestr[]={"ooleans[0]", "ooleans[1]", "ooleans[2]", "ntegers[0]", "ntegers[1]",
"ntegers[2]", "trings[0]", "trings[1]", "trings[2]"};
int index;


cout<<" Script Converter\n";
cout<<" By Macrosoft\n"<<"Special Thanks to R0b0t1 for his help\n\n";
cout<<"Would you like to convert a SRL 3.81 script to SRL 4? (yes or no) \n";
cin>>re;
_strupr_s(re);
response=re;
if(response== "YES") {
cout<<"Please enter the name of the file (C:/Program Files/example.scar)"<<endl;
cout<<"the location is not needed if this program and the script are in the same folder\n";
cin.ignore ( 150, '\n' ); //flush the input stream
cin.getline(File, 150);
cout<<"Now Converting "<< File<< " to SRL 4 ..."<<endl;
ifstream fin;
ofstream fout;
fin.open(File);
if(fin){
while (getline(fin, line[counter])){ //this is where the "arrow" is at when there is the error
string* line = new string[counter];
counter++;
}
x=counter;
for(counter=0; counter<x+1; counter++){
for(index=0; index<9; index++){
found=line[counter].find(strtofind[index]);
if (found!=string::npos){
line[counter].replace(int(found), strtofind[index].length(), changestr[index]);
}
}
}
fout.open(File);
fout<<"{Script Converted to SRL 4 by Macrosoft's Script Converter}\n";
for(counter=0; counter<x+1; counter++){
fout<<line[counter]<<"\n";
}
cout<<"Finished converting script\n";
}
else {
cout<<"Failed to open file\n";
}
fin.close();
fout.close();
}
else if(response == "NO"){
cout<<"Closing Program...\n";
}
else{
cout<<"Response not understood. Closing Program...\n";
}
system("PAUSE");
return 0;
}

R0b0t1

2007-11-23 13:28:03

How many times: "IT IS A PROGRAM!!!"

Macrosoft

2007-11-23 18:35:29

sorry?!, dont kill me please

have you found whats wrong with it?

im gunna try and post it on daniweb forums

R0b0t1

2007-11-24 00:39:23

I would post it there. Or MSDN forums, but it kinda depends.


What are you compiling it with?

Macrosoft

2007-11-24 20:50:00

ms visual c++

BenLand100

2007-11-25 10:35:19

From: Macrosoft
ms visual c++

Debug it and it will tell you what line the problem is on ;)

Macrosoft

2007-11-25 12:37:37

look at the code, I have a comment saying what line it is on

BenLand100

2007-11-25 14:06:18

Ah, missed that. Anyway, you've got your line array messed up. I'm not sure what you're doing, but you're declaring the array after you use it… Look back over that part.

Macrosoft

2007-11-25 14:40:27

yeah, i am having trouble with dynamic data, i want to add to the array everytime i read a new line, not sure how to do it though

BenLand100

2007-11-26 19:40:58

From: Macrosoft
yeah, i am having trouble with dynamic data, i want to add to the array everytime i read a new line, not sure how to do it though

Make a really big buffer and hope you don't run out of room. Or, you could make an array of arrays (an array of pointers to char pointers) and allocate a new array for each line, and add it to the line array. However, there is no such thing as a dynamic array in c, you can allocate a larger area and copy old stuff into it, which is what other languages do, but you cannot say "tack on some more space on the end of this memory segment" since its not all in the same space anyway ;)

R0b0t1

2007-11-26 22:15:25

What about making an array like this?


CHAR **chrBigArr = new CHAR*[];

I think that works…

BenLand100

2007-11-27 01:31:14

From: R0b0t1
What about making an array like this?


CHAR **chrBigArr = new CHAR*[];
I think that works…

Yea, but you still can't resize it can you? Unless there was a memo I missed somewhere… And still thats what I said do, an array of arrays of characters. IE, its not one continuous memory segment, but its an array pointing to different memory segments containing a line a piece. If you start at the first memory address, you can't just keep incrementing it and expect to get all the characters, once that line is over, the next line starts somewhere else (or could, it doesn't have to be one way or the other).

Macrosoft

2007-11-27 16:53:07

… im confused, i thought the whole point of dynamic data was to add another part to an array (stupid tuts r nevr right)

R0b0t1

2007-11-28 19:36:22

Well the code that I posted was not meant to have numbers inserted into it, but to just be used as it is…

NxTitle

2007-12-01 22:20:54

You cannot resize a character array. You're probably better off using C++ strings, as those are resizable.
Although, if you have to stick to character arrays, one option is to redeclare the array.

For the first time you declare it:
char * blah = new char ;

then when you need a different size, run the following code:
free(blah);
char * blah = new char ;

Or, if you really want to go down-low into C, you can actually re-allocate memory:
char * blah = new char ;

realloc(blah,size);

Hope this helps.

R0b0t1

2007-12-02 19:47:33

Wait… realloc means its kinda like dynamic arrays… WTF?

Paul23

2007-12-06 09:41:26

I am really not great at C++ but i believe that you problem stems from the fact that
the variable line has not be declared when this code executes
while (getline(fin, line[counter])

if you look at the tutorial on this page you will see the difference
http://www.cppreference.com/cppstring/getline.html

boberman

2007-12-15 01:38:06

from what I can tell, the problem definitely stems from an array, more specifically it looks like you are trying to write farther then the array can handle (I could be wrong)

Using the New command is what you need to do for a dynamic array, but make sure that you use the delete command somewhere in your program (or else you get a memory leak…)

Macrosoft

2007-12-15 14:00:16

y do i need to use the delete command???

im so confused…

u know how in scar there is setarraylenght? Basicly, thats what i want to do, add another part of the str array everytime

BenLand100

2007-12-15 22:03:51

From: R0b0t1
Wait… realloc means its kinda like dynamic arrays… WTF?

No, realloc is as close to blasphemy as you can get without being smote by the God of C :) What it does is changes the "size" of the memory block to a smaller size, or larger size if there is room. But if there is not enough room, it allocates a new memory segment of the right size, copies the data into it, frees the old segment, and returns the pointer to the new segment.

R0b0t1

2007-12-16 00:03:17

Well, that sounds pretty convenient, is it not supported or considered lazy? The reason the OS developed was to take care of memory and disk management you know :)



EDIT: So is this what the VB ReDim does?
EDIT: And it seems as I've been reading, you need to assign the output of a realloc as a pointer.
EDIT: free() is the same as delete[] ….

BenLand100

2007-12-16 08:45:31

From: R0b0t1
Well, that sounds pretty convenient, is it not supported or considered lazy? The reason the OS developed was to take care of memory and disk management you know :)



EDIT: So is this what the VB ReDim does?
EDIT: And it seems as I've been reading, you need to assign the output of a realloc as a pointer.
EDIT: free() is the same as delete[] ….

Its lazy and slow/inefficient. You might start doing things like you do in scar,
SetLength(arr, Length(arr)+1);
If you did that for say, 1000 entries, slowness is your result ;) Most likely this is what VB's redim and Delphi's SetLength does. Yes if it changes the position of the array, you need to know where it put it, so something like this is in order:
ptr = realloc(ptr,42);
free() is not the same as delete. Delete is used to 'free' data created with new, free() is used to 'free' data created with malloc(), realoc(), and other alloc's. They both do the same thing, but they are not guaranteed to in the future, and therefore you should not mix them. For instance, if kicking your car makes it start sometimes, do you want to kick it every time (thereby destroying the paint and causing dents), or just use the key (which always works)? :D

Macrosoft

2007-12-18 21:17:29

just tell me in english how im supposed to do this lol. A huge buffer doesn't work, thats what i origionaly had, but it got an error too (runtime error)