Miscellaneous > Programming & Networking

Now what ?

(1/2) > >>

foobar:
Hi,
Got this strange problem with C.
I have a program that reads a string, wich contains characters (duuh) that say where a dot should be drawn compared to the current position. 'd' means below the current position, 'l' means left of the current position, et c.
Now it works just fine if you'd just do 'dd', but with longer or more variated strings (like 'dduuullrurrlruuurld') it goes on for about three times ... and then segfaults. Wierd. When i compile, gcc doesn't complain at all. Maybe gdb would, but i don't know how to use it  :D

I'm still in the testing phase, i have already achieved to control the dot, but commented the code out to make the snake's tail work, wich is the hardest part. btw, it's a classical snake game.

If you would like to compile it, make sure you've got svgalib libraries (/lib/libvga.so.*) properly installed. It's C, so say:

# gcc ./sneek.c -o ./sneek -lvga

Or do, what i always do for my own comfort:

# gcc ./sneek.c -o ./sneek -lvga && echo " " && ./sneek

Here it goes: i appreciate all help !!! (now you know my real name  :D )


--- Code: ---
--- End code ---

foobar:
Hm. Seems my post got posted over, or nobody had a clue about my post.
I beg you to take a look at it ... plz ?

badkarma:
Sorry ... didn't notice post untill now

I'll have a look at it tomorrow after work ... it's late and I'm getting ready for bed...

btw ... vgalib? why not use SDL?

badkarma:
btw ... first run 'ulimit -c unlimited' then run your program. It should now core dump when you run your program and save it to a file (usually either called just 'core' or 'core.processid').
Now type 'gdb sneek core' (or core.processid, just do a ls -ail core* to see the correct filename), wait for gdb to load the coredump and type 'where'. Paste the output in a post here if you still can't figure it out  

badkarma:
a few thoughts:

Why redraw the whole snake body every frame? It's a lot more efficient (not that it matters much for this program, but hey, you wanted to learn right?  ;) ) to draw the new snake bit once every frame and to not clear the rest of the screen. This immediatly solves the problem of you having to keep track of the snakes path as well, all you care about is where it is going next, and you can check for a collision by comparing pixel data of where you are about to draw)

C Strings (arrays in general) are evil! They are very prone for errors (and my guess, the reason you're program segfaults). Take a small step extra and learn C++ and more importantly then STL (a collection of template classes aimed to alleviate a programmer of these problems (there are some really handy list based classes for your problem at hand, I have never used STL much but Qt also offers STL like classes))

and again ... vgalib? why did you chose it?

Navigation

[0] Message Index

[#] Next page

Go to full version