I need help.

Discussion in '3DS - Homebrew Development and Emulators' started by HexOverflow, Apr 12, 2017.

  1. HexOverflow
    OP

    HexOverflow GBAtemp Meme Lord

    Member
    105
    120
    Jan 12, 2017
    United States
    Userland
    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?
     
  2. Paccc

    Paccc Member

    Newcomer
    45
    7
    Jun 17, 2016
    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.
     
  3. NexoCube

    NexoCube stop using piracy :(

    Member
    1,184
    587
    Nov 3, 2015
    France
    Stack Pointer
    Pacc is right, try to alloc linear memory, the linear heap is damn big
     
  4. HexOverflow
    OP

    HexOverflow GBAtemp Meme Lord

    Member
    105
    120
    Jan 12, 2017
    United States
    Userland
    malloc(1024*1024*9); = still nothing
     
  5. NexoCube

    NexoCube stop using piracy :(

    Member
    1,184
    587
    Nov 3, 2015
    France
    Stack Pointer
    unsigned char *dick_allocation;
    dick_allocation = linearAlloc(1024*1024*4);
    fread(dick_allocation, ...);

    — Posts automatically merged - Please don't double post! —

    Edited my last post lol
     
    Last edited by NexoCube, Apr 12, 2017
  6. HexOverflow
    OP

    HexOverflow GBAtemp Meme Lord

    Member
    105
    120
    Jan 12, 2017
    United States
    Userland
    DOOD HOLY FUCK THAT WORKED but why isnt board working now
     
    NexoCube likes this.
  7. NexoCube

    NexoCube stop using piracy :(

    Member
    1,184
    587
    Nov 3, 2015
    France
    Stack Pointer
    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, ...);
    ...
     
  8. HexOverflow
    OP

    HexOverflow GBAtemp Meme Lord

    Member
    105
    120
    Jan 12, 2017
    United States
    Userland
    upload_2017-4-12_5-3-47.png

    — Posts automatically merged - Please don't double post! —

    fixed board text but above error is still happening
     
  9. Wolfvak

    Wolfvak *yawn*

    Member
    801
    1,061
    Oct 25, 2015
    Uruguay
    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...)
     
  10. HexOverflow
    OP

    HexOverflow GBAtemp Meme Lord

    Member
    105
    120
    Jan 12, 2017
    United States
    Userland
    it appears that im still getting memory errors upload_2017-4-12_5-16-46.png

    — Posts automatically merged - Please don't double post! —

    Fixed it by putting board data on sd..

    — Posts automatically merged - Please don't double post! —

    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
     
  11. NexoCube

    NexoCube stop using piracy :(

    Member
    1,184
    587
    Nov 3, 2015
    France
    Stack Pointer
    Yay, i went rude with someone in internet, my virtual girlfriend will be proud of me <3

    — Posts automatically merged - Please don't double post! —

    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, Apr 12, 2017