Author Topic: General Ndless Questions and Support  (Read 106566 times)

0 Members and 1 Guest are viewing this topic.

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: General Ndless Questions and Support
« Reply #120 on: January 03, 2013, 07:59:38 pm »
FILE * f = fopen("/documents/miner/mapdata.tns", "r");
for(j = 0; j < 10120; j++)
   fscanf(f, "%d", &map[j]);
fclose(f);


How do I put in scanf?
Compiler doesn't recognize it

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: General Ndless Questions and Support
« Reply #121 on: January 03, 2013, 08:02:21 pm »
I think it's not implemented in ndless' os.h.
Try to compile without -nostdlib, but this will make your binary ~200KB larger.

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: General Ndless Questions and Support
« Reply #122 on: January 03, 2013, 08:05:18 pm »
I don't see any -nostdlib in my makefile.

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: General Ndless Questions and Support
« Reply #123 on: January 03, 2013, 08:12:09 pm »
It's probably easier if you just use fread, the code you showed in the other thread seemed correct. fwrite is just the opposite and not more difficult to use.

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: General Ndless Questions and Support
« Reply #124 on: January 03, 2013, 08:22:25 pm »
You mean something like this?

FILE *f = fopen("/documents/miner/mapdata.tns", "w+");
fwrite(map, sizeof(int), 32000, f); //32000 blocks in map
fclose(f);

FILE *f = fopen("/documents/miner/mapdata.tns", "r");
fread(map, sizeof(int), 32000, f);
fclose(f);


Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: General Ndless Questions and Support
« Reply #125 on: January 03, 2013, 08:37:21 pm »
Something like that, yes. It looks good, but you possibly need "w+b" and "rb" because it's not text. Just test if it works :P

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: General Ndless Questions and Support
« Reply #126 on: January 03, 2013, 09:17:16 pm »
Yup, that works :P

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: General Ndless Questions and Support
« Reply #127 on: January 04, 2013, 08:28:08 pm »
I'm kinda confused as to why this works :P

fwrite(map, 1, 24000, f);
fread(map, 1, 24000, f);
These lines work and output a file 24k large, but I wonder why it works because the values stored in map are sometimes > 255, and I thought that this code would only work with 8 bit numbers.

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: General Ndless Questions and Support
« Reply #128 on: January 04, 2013, 08:33:23 pm »
It works for all numbers, even something like
fwrite(map, 1, 3, f) with 24 bit integers :)
24000 * 1 byte(char) = 6000 * 4 byte(int)
fwrite and fread don't care about what they're writing/reading, you only have to give the size of your object.
« Last Edit: January 04, 2013, 08:33:30 pm by Vogtinator »

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: General Ndless Questions and Support
« Reply #129 on: January 21, 2013, 08:33:01 am »
Plop,

I've been encountering some problems with Ndless' UI features. This code :
Code: [Select]
show_msgbox_2b("Title", "Text", "Button 1", "Button 2");Gives that :



And it's basically the same with show_msgbox_3b (displays a dialog box with 3 buttons) ; the labels are "Button 1Button 2Button 3", "Button 2Button 3" and "Button 3".

Also, this code just crashes nspire_emu :
Code: [Select]
char filename[256];
show_msg_user_input("Test", "Test this", "Default", &filename);
printf("%s", filename);
free(filename);

return 0;

I think that it's a bug with Ndless ...
« Last Edit: January 21, 2013, 08:33:21 am by Matrefeytontias »

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: General Ndless Questions and Support
« Reply #130 on: January 21, 2013, 09:56:51 am »
You're freeing a memory location which was not allocated with malloc... which does, usually, precisely trigger a crash ;)
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: General Ndless Questions and Support
« Reply #131 on: January 21, 2013, 09:58:14 am »
Hmhm, I'll try that, and the show_msgbox_2b bug ?

EDIT : nope, it crashes before the free(filename);, right after char *filename.
« Last Edit: January 21, 2013, 10:15:20 am by Matrefeytontias »

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: General Ndless Questions and Support
« Reply #132 on: January 21, 2013, 10:19:18 am »
Plop,

I've been encountering some problems with Ndless' UI features. This code :
Code: [Select]
show_msgbox_2b("Title", "Text", "Button 1", "Button 2");Gives that :



And it's basically the same with show_msgbox_3b (displays a dialog box with 3 buttons) ; the labels are "Button 1Button 2Button 3", "Button 2Button 3" and "Button 3".

Not sure why, but in _show_msgbox.c, I found this
Code: [Select]
char button1_16[14];
char button2_16[14];
This explains your problem. Do not make your button text longer than 7 characters (without the \0).

If you have a text larger than 7 characters and don't want to wait ExtendeD to change the trunk, you can change it to
Code: [Select]
char button1_16[(strlen(button1) + 1) * 2];
char button2_16[(strlen(button2) + 1) * 2];

And later, change
Code: [Select]
char button3_16[14];

to

Code: [Select]
char button3_16[(strlen(button3) + 1) * 2];

And recompile libndls.a (Ndless).

Quote
Also, this code just crashes nspire_emu :
Code: [Select]
char filename[256];
show_msg_user_input("Test", "Test this", "Default", &filename);
printf("%s", filename);
free(filename);

return 0;

I think that it's a bug with Ndless ...

1) free() on a char filename[256] does crash, even in a computer program. filename[256] is a static allocation contrary to malloc.
2) filename[256] : you surely don't need this, look at the example in ndless/libndls/show_msg_user_input.c
You have to pass a pointer to your char array, but you should not initialize it ! Indeed, you don't know its size and putting arbitrary size (here 256) is a bad programming behavior.
3) Do not free if the popup returned -1, because the popup was canceled, there is no result, and your char array is not initialized. Again, checkout the sample code in ndless/libndls/show_msg_user_input.c
« Last Edit: January 21, 2013, 10:21:08 am by Levak »
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: General Ndless Questions and Support
« Reply #133 on: January 21, 2013, 11:07:01 am »
This code crashes :
Code: [Select]
int main()
{
  char *filename;

  if(show_msg_user_input("A", "B", "C", &filename) != -1) free(filename);
  return 0;
}

And I can't find any libndls directory in /ndless/ ... (I use the Ndl3ss SDK)

EDIT : second question on the go : why does libndls's refresh_osscr() crashes ? :banghead:
« Last Edit: January 21, 2013, 11:13:59 am by Matrefeytontias »

Offline hoffa

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 322
  • Rating: +131/-13
    • View Profile
Re: General Ndless Questions and Support
« Reply #134 on: January 21, 2013, 11:31:36 am »
Indeed, you don't know its size and putting arbitrary size (here 256) is a bad programming behavior.
I don't want to be a nitpicker, but I have to disagree. In a practical context, using "big enough" buffers when dealing with arbitrary-sized strings is in many cases better than hassling around with a bunch of pointers and dynamically allocated memory. Using the latter method complexifies and bloats the code unnecessarily, gives the programmer additional responsibility (gotta compute the size of the data, malloc it, remember to free the memory, etc.), decreases the programs maintainability (as the code for that filename container is scattered around, when modifying bigger chunks better check it doesn't cause any undefined behavior), and generally the whatever academic perfection of the code doesn't outweigh the pain of dealing with such trivial stuff. We're not talking about security-critical government-class programs here, and it's not like there aren't any functions to avoid buffer overflows and whatnot.
I'm not saying you should be using statically allocated data for everything (quite the contrary usually), but for string buffers and such, a char buf[BUF_SIZE]; is usually good enough.
« Last Edit: January 21, 2013, 11:32:18 am by hoffa »