Homebrew I need help.

ItsKaitlyn03

Official GBAtemp ?
Developer
Joined
Jan 12, 2017
Messages
277
Reaction score
724
Trophies
0
Location
United States
XP
1,246
Country
Japan
So I'm trying to read a 4MB (4,000,000 bytes) binary file into a struct like so

Code:
typedef struct boardData {
  unsigned char board[1024*1024*4];
} board;

int main(int argc, char **argv) {

    gfxInitDefault();
    consoleInit(GFX_TOP, NULL);

  struct boardData board;

  FILE *fpr = fopen("romfs:/boarddata", "r");
  if(fpr != NULL)
  {
    printf(":O");
    fread(&board, 1, 2000, fpr);
    fclose(fpr);
  }

but no matter what I do it just crashes on everything?
 
You put 4 MB on the stack,
so if you did not specifically increase the stack of your process to 4 MB it will overflow into the memory of something else.
Try to allocate memory instead.
 
DOOD HOLY FUCK THAT WORKED but why isnt board working now

You need to modify the way you define your struct

typedef struct BoardData {
unsigned char *board_text;
} BoardData;

BoardData *board = linearAlloc(4); // size of a pointer
board->board_text = linearAlloc(1024*1024*4);

// fopen
...
fread(board->board_text, ...);
...
 
upload_2017-4-12_5-3-47.png


--------------------- MERGED ---------------------------

fixed board text but above error is still happening
 
BoardData *board = linearAlloc(4); // size of a pointer

Allocating pointers in dynamic memory is somewhat useless tbqh, they're only 4 or 8 bytes and we get 32KiB of stack with ctrulib (obviously changeable).

To OP, I'd recommend reading a book or two on C/C++ and dynamic memory usage. It seems like you have no idea what you're doing (don't worry, we all start somewhere...)
 
it appears that im still getting memory errors
upload_2017-4-12_5-16-46.png


--------------------- MERGED ---------------------------

Fixed it by putting board data on sd..

--------------------- MERGED ---------------------------

hrm... fixed all of it but this happens when i wanna do printf("%d\n", board->board[0+0*2000]);
upload_2017-4-12_5-32-38.png
 
Allocating pointers in dynamic memory is somewhat useless tbqh, they're only 4 or 8 bytes and we get 32KiB of stack with ctrulib (obviously changeable).

To OP, I'd recommend reading a book or two on C/C++ and dynamic memory usage. It seems like you have no idea what you're doing (don't worry, we all start somewhere...)

Yay, i went rude with someone in internet, my virtual girlfriend will be proud of me <3

--------------------- MERGED ---------------------------

it appears that im still getting memory errorsView attachment 83908

--------------------- MERGED ---------------------------

Fixed it by putting board data on sd..

--------------------- MERGED ---------------------------

hrm... fixed all of it but this happens when i wanna do printf("%d\n", board->board[0+0*2000]);View attachment 83909

Printing text using printf is using "%s" not "%d"

And, 0+0*2000 = 0
You should use printf("%s", board->board_text); (because %s take a char pointer)
 
Last edited by NexoCube,

Site & Scene News

Popular threads in this forum