News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: C array access crashes program  (Read 4836 times)

Mikey88

  • Jr. Member
  • **
  • Posts: 10
    • View Profile
C array access crashes program
« on: January 23, 2013, 11:57:21 pm »
**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.
« Last Edit: January 24, 2013, 12:21:24 am by Mikey88 »

Pikachumanson

  • Hero Member
  • *****
  • Posts: 607
    • View Profile
Re: C array access crashes program
« Reply #1 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!

furrykef

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: C array access crashes program
« Reply #2 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.

Rhys

  • Hero Member
  • *****
  • Posts: 706
    • View Profile
    • CN
Re: C array access crashes program
« Reply #3 on: January 24, 2013, 08:58:12 am »
C# or VB.NET are also great tools for this :)

Bregalad

  • Hero Member
  • *****
  • Posts: 2636
    • View Profile
Re: C array access crashes program
« Reply #4 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).

tryphon

  • Hero Member
  • *****
  • Posts: 722
    • View Profile
Re: C array access crashes program
« Reply #5 on: January 24, 2013, 11:01:31 am »
I wouldn't recommend C for ROM hacking tools. It introduces needless complexity. My own personal recommendation is Python.
:thumbsup:

Mikey88

  • Jr. Member
  • **
  • Posts: 10
    • View Profile
Re: C array access crashes program
« Reply #6 on: January 24, 2013, 08:21:11 pm »
Thanks everyone for the reply's I really appreciate it!

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.

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

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.

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

  • Hero Member
  • *****
  • Posts: 906
    • View Profile
    • au-ro-ra.net
Re: C array access crashes program
« Reply #7 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.

Code: [Select]
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:

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

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

ETG

  • Jr. Member
  • **
  • Posts: 49
    • View Profile
Re: C array access crashes program
« Reply #8 on: January 25, 2013, 08:06:25 am »
==.

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

furrykef

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: C array access crashes program
« Reply #9 on: January 25, 2013, 10:42:48 am »
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.
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

  • Jr. Member
  • **
  • Posts: 10
    • View Profile
Re: C array access crashes program
« Reply #10 on: January 25, 2013, 04:00:19 pm »
It can't work because you have to access readBuf again with the new index, for example by using a loop.

Code: [Select]
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:

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

Code: [Select]
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!

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

  • Full Member
  • ***
  • Posts: 227
    • View Profile
Re: C array access crashes program
« Reply #11 on: January 30, 2013, 04:53:25 am »
stay at C, you can do almost everything alone and its best optimized by compilers.

tryphon

  • Hero Member
  • *****
  • Posts: 722
    • View Profile
Re: C array access crashes program
« Reply #12 on: January 30, 2013, 07:10:35 am »
Bad advice if it's your first language.

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

furrykef

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: C array access crashes program
« Reply #13 on: February 02, 2013, 01:24:14 am »
you can do almost everything alone
I have no idea what this means.

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