Homebrew I need help.

ItsKaitlyn03

Official GBAtemp ?
OP
Developer
Joined
Jan 12, 2017
Messages
277
Trophies
0
Location
United States
XP
1,201
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?
 

Paccc

Well-Known Member
Newcomer
Joined
Jun 17, 2016
Messages
70
Trophies
0
Age
68
XP
177
Country
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.
 

NexoCube

Well-Known Member
Member
Joined
Nov 3, 2015
Messages
1,222
Trophies
0
Age
29
Location
France
XP
1,340
Country
France
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, ...);
...
 

ItsKaitlyn03

Official GBAtemp ?
OP
Developer
Joined
Jan 12, 2017
Messages
277
Trophies
0
Location
United States
XP
1,201
Country
Japan
upload_2017-4-12_5-3-47.png


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

fixed board text but above error is still happening
 

Wolfvak

nyaa~
Member
Joined
Oct 25, 2015
Messages
918
Trophies
1
XP
3,486
Country
Uruguay
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...)
 

ItsKaitlyn03

Official GBAtemp ?
OP
Developer
Joined
Jan 12, 2017
Messages
277
Trophies
0
Location
United States
XP
1,201
Country
Japan
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
 

NexoCube

Well-Known Member
Member
Joined
Nov 3, 2015
Messages
1,222
Trophies
0
Age
29
Location
France
XP
1,340
Country
France
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

General chit-chat
Help Users
  • No one is chatting at the moment.
    Psionic Roshambo @ Psionic Roshambo: https://www.youtube.com/watch?v=dZbUuSmFgMo