News:

11 March 2016 - Forum Rules

Main Menu

C array access crashes program

Started by Mikey88, January 23, 2013, 11:57:21 PM

Previous topic - Next topic

Mikey88

**Edit, Sorry guys figured it out I was using the wrong format specifier. Still a super noob with this C stuff. Man that took me almost 2 hours to figure out.

Hi guys,

   I am very new to C programming, and I'm attempting to write a decompression program. I am having a problem with my array that holds the ROM data. I have read the graphics data from the ROM to an array in main, I have been using print statements to check the data is correct and they work fine as long as I am in main, but when I try and access the data array inside another function it crashes. More specifically anytime I access the first element it is fine, but when I try to access anything else readbuf[1] it crashes. This is the first program so I might be missing something extremely obvious, any help would be most appreciated. Thanks for the time guys

Mikey.

Pikachumanson

That's ok, I once spent all day and all night trying to figure out collision and scrolling for my town for an overhead RPG I was working on. Finally got it to work at six in the morning, lol!

furrykef

I wouldn't recommend C for ROM hacking tools. It introduces needless complexity. My own personal recommendation is Python.

doop


Bregalad

This is very typical of you trying to access data that is outside the memory reserved for the array.

Make sure your index is ALWAYS beween 0 and Size-1.
Also make sure the pointer is initialized - either at compilation time (if you declare it as an array in the code, with the size specified), or at runtime using malloc function (if you declare it as a pointer - or an array with no specified size - which means exactly the same).

tryphon

Quote from: furrykef on January 24, 2013, 04:56:13 AM
I wouldn't recommend C for ROM hacking tools. It introduces needless complexity. My own personal recommendation is Python.
:thumbsup:

Mikey88

Thanks everyone for the reply's I really appreciate it!

Quote from: Pikachumanson on January 24, 2013, 01:19:22 AM
That's ok, I once spent all day and all night trying to figure out collision and scrolling for my town for an overhead RPG I was working on. Finally got it to work at six in the morning, lol!

I have had a few days/nights like that since starting with SNES ASM, and C programming lol.

Quote from: furrykef on January 24, 2013, 04:56:13 AM
I wouldn't recommend C for ROM hacking tools. It introduces needless complexity. My own personal recommendation is Python.

Quote from: Rhys on January 24, 2013, 08:58:12 AM
C# or VB.NET are also great tools for this :)

I might look into these languages. I picked C because it seemed to be a good in general programming language, but would hate to quit after spending weeks of reading tuts/books.

Quote from: Bregalad on January 24, 2013, 09:07:57 AM
This is very typical of you trying to access data that is outside the memory reserved for the array.

Make sure your index is ALWAYS beween 0 and Size-1.
Also make sure the pointer is initialized - either at compilation time (if you declare it as an array in the code, with the size specified), or at runtime using malloc function (if you declare it as a pointer - or an array with no specified size - which means exactly the same).

I am currently having a problem with using a variable as my index (array[j]) when I try this I just get the value 0 back every time even after j++, or I even tried j = j+1. They load the values fine if I do array[0], and array[1], but will not work with the variable in place.

Here is a link to my code, I did not want my post to be massive.
http://tny.cz/62c71760


LostTemplar

It can't work because you have to access readBuf again with the new index, for example by using a loop.


int j = 0;
unsigned char my_char = readBuf[j];
j++;


This code reads the j-th element of your array into my_char, and increments the index, nothing else. Incrementing the index does not change my_char. Here are two examples that show how to update my_char's value:


int j = 0;
unsigned char my_char = readBuf[j]; /* my_char is readBuf[0] */
j++;
/* ... */
my_char = readBuf[j]; /* my_char is readBuf[1] */



int j;
for(j = 0; j < max; j++)
{
    my_char = readBuf[j]; /* my_char is updated each iteration */
    /* ... */
}


ETG

==.

Make sure your using = and == correctly. It's easy to overlook that.

furrykef

Quote from: Mikey88 on January 24, 2013, 08:21:11 PMI picked C because it seemed to be a good in general programming language, but would hate to quit after spending weeks of reading tuts/books.
Don't get me wrong, C is a very good language to know. It helps you understand how things work at the system level and there is lots and lots of code written in C. Even if you don't code in it very often, you will likely want to read a lot of code written in C or C++, since quite a lot of programming involves seeing how somebody else solved a problem similar to the one you're working on.

If your primary goal is to learn C and making useful tools with it is secondary, by all means go ahead. If your primary goal is to make tools and learning C is secondary, though, I would focus on languages that are good for writing the sorts of tools you wish to write.

Mikey88

Quote from: LostTemplar on January 25, 2013, 02:47:24 AM
It can't work because you have to access readBuf again with the new index, for example by using a loop.


int j = 0;
unsigned char my_char = readBuf[j];
j++;


This code reads the j-th element of your array into my_char, and increments the index, nothing else. Incrementing the index does not change my_char. Here are two examples that show how to update my_char's value:


int j = 0;
unsigned char my_char = readBuf[j]; /* my_char is readBuf[0] */
j++;
/* ... */
my_char = readBuf[j]; /* my_char is readBuf[1] */



int j;
for(j = 0; j < max; j++)
{
    my_char = readBuf[j]; /* my_char is updated each iteration */
    /* ... */
}



Awesome that does it. Thank you very much!

Quote from: furrykef on January 25, 2013, 10:42:48 AM
Don't get me wrong, C is a very good language to know. It helps you understand how things work at the system level and there is lots and lots of code written in C. Even if you don't code in it very often, you will likely want to read a lot of code written in C or C++, since quite a lot of programming involves seeing how somebody else solved a problem similar to the one you're working on.

If your primary goal is to learn C and making useful tools with it is secondary, by all means go ahead. If your primary goal is to make tools and learning C is secondary, though, I would focus on languages that are good for writing the sorts of tools you wish to write.

You make a very good point, I started learning how to program to do Rom hacking related projects. Before I started I looked around for source codes to of SNES related programs at the majority seemed to be in C so I assumed that was the best way to go. I think I going to try and finish this one (hopefully I can) since I have already started, then possibly look into Python or another. I appreciate the input!

syntax error

stay at C, you can do almost everything alone and its best optimized by compilers.

tryphon

Bad advice if it's your first language.

Likely bad if not (and I'm a C lover) :)

furrykef

Quote from: syntax error on January 30, 2013, 04:53:25 AMyou can do almost everything alone
I have no idea what this means.

Quote from: syntax errorand its best optimized by compilers.
Do you have any idea how irrelevant this is for 99% of software?