Author Topic: C Q&A Thread  (Read 19247 times)

0 Members and 1 Guest are viewing this topic.

Offline sammyMaX

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 204
  • Rating: +9/-0
    • View Profile
Re: C Q&A Thread
« Reply #30 on: August 22, 2011, 05:11:46 pm »
Awww... whatever, it's not crucial to my program anyways.

Are you wondering who Sammy is? My avatar is Sammy.
   

Offline sammyMaX

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 204
  • Rating: +9/-0
    • View Profile
Re: C Q&A Thread
« Reply #31 on: August 22, 2011, 06:29:52 pm »
Here's another question I have:
To handle input in my program, I have made a
Code: [Select]
char input[500];This means the input can only be 500 characters, but if I use fancier dynamic allocation methods this problem still remains: sometimes when I type, weird characters will be outputted in front of the useful text, only to be overwritten as I type more. I might type in "12345", and then have it suddenly turn into "12345[=$_L?<". As I type in more characters, they get overwritten into "123459843406", but more random symbols may pop up later. I really don't know what would be causing that to happen, because my string drawing function has already worked on other things, and I can't find anything wrong with the key detection code. Any ideas?

Are you wondering who Sammy is? My avatar is Sammy.
   

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: C Q&A Thread
« Reply #32 on: August 23, 2011, 04:06:31 am »
We would need the code snippet which fills up "input[] to be able to help you, the problem may be there.
Ndless.me with the finest TI-Nspire programs

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: C Q&A Thread
« Reply #33 on: August 23, 2011, 09:19:33 am »
Could it be a problem with null-termination? You can't assume that a dynamically allocated buffer (or a fixed-size local buffer) will be filled with 0.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Tribal

  • The Fallen
  • LV5 Advanced (Next: 300)
  • *
  • Posts: 218
  • Rating: +15/-1
    • View Profile
Re: C Q&A Thread
« Reply #34 on: August 23, 2011, 09:23:45 pm »
Yeah, it kinda sounds like a problem with null termination.  You should never assume allocated memory is clean, the safest bet would be to do a
Code: [Select]
memset(input, 0, bytes allocated);
after allocation and see if it still does it.

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: C Q&A Thread
« Reply #35 on: August 24, 2011, 08:34:47 am »
Or use calloc().
Ndless.me with the finest TI-Nspire programs

Offline sammyMaX

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 204
  • Rating: +9/-0
    • View Profile
Re: C Q&A Thread
« Reply #36 on: August 25, 2011, 08:10:41 pm »
Yay, it works! Thanks again!

Are you wondering who Sammy is? My avatar is Sammy.
   

Offline Tribal

  • The Fallen
  • LV5 Advanced (Next: 300)
  • *
  • Posts: 218
  • Rating: +15/-1
    • View Profile
Re: C Q&A Thread
« Reply #37 on: August 25, 2011, 09:42:02 pm »
Or use calloc().

I've never used calloc ever since I came across the post on cprogramming's discussion board:

Quote
malloc will return an uninitialized chunk of memory for you to use.
calloc ( clear alloc ) will attempt to initialize that chunk of memory to all bits zero.

Note that when I say attempt, I mean that this may not be possible so you shouldn't rely on it. Other than that the only difference is how they are called and how they are implemented. So in the end, which one you choose to use is a matter of preference.
ref: http://cboard.cprogramming.com/c-programming/20505-malloc-vs-calloc.html#post132635
« Last Edit: September 03, 2011, 12:27:16 pm by Tribal »

Offline sammyMaX

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 204
  • Rating: +9/-0
    • View Profile
Re: C Q&A Thread
« Reply #38 on: September 03, 2011, 10:37:45 am »
I'm getting a weird problem creating arrays now. Here's the code:
Code: [Select]
int numInts1 = ceil(operationPos, 4);
int numInts2 = ceil((inputLen - operationPos - 1), 4);

int *in1 = (int*)calloc(numInts1, sizeof(int));
int *in2 = (int*)calloc(numInts2, sizeof(int));
Using the Eclipse debugger, numInts1 is what it's supposed to be, and same as numInts2, but the arrays in1 and in2 are always only one int long. Attached a screenshot of the debugger. (What it's showing means that the arrays are one int long, right? Otherwise I wouldn't know what's wrong with my program)

Are you wondering who Sammy is? My avatar is Sammy.
   

Offline sammyMaX

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 204
  • Rating: +9/-0
    • View Profile
Re: C Q&A Thread
« Reply #39 on: September 04, 2011, 10:56:02 am »
Never mind - got it all fixed. It turns out that Eclipse can't figure out what's an array and what's not when they are dynamically allocated. Just click on the variable and then "Display as an Array."

Are you wondering who Sammy is? My avatar is Sammy.
   

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: C Q&A Thread
« Reply #40 on: January 19, 2012, 09:14:48 pm »
How do you use the show_msgbox function?

I have the following code:

Code: [Select]
#include <os.h>

int main()
{
    void show_msgbox("Hello World", "Hello Omnimaga");
    return 0;
}

And it's not compiling with the error:
Code: [Select]
Tom@AdmirableCoffee /c/Users/Tom/Desktop/Programs/Ndless/Hello_World
$ make
nspire-gcc -Os -nostdlib -Wall -W -marm -c main.c
main.c: In function 'main':
main.c:5:10: error: expected declaration specifiers or '...' before string const
ant
main.c:5:10: error: expected declaration specifiers or '...' before string const
ant
main.c:5:10: error: expected declaration specifiers or '...' before numeric cons
tant
make: *** [main.o] Error 1

Tom@AdmirableCoffee /c/Users/Tom/Desktop/Programs/Ndless/Hello_World
$

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: C Q&A Thread
« Reply #41 on: January 19, 2012, 09:34:54 pm »
Lose the "void" since you're calling the function, not declaring it. As far as making sure you're calling the function with the proper arguments, I can't help you there because I don't know anything about device-specific C development.

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: C Q&A Thread
« Reply #42 on: January 19, 2012, 10:01:10 pm »
It works. Thanks :D

I honestly feel I should hit myself over the head hard for something so obvious :P

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...