ugh help in C again asap

FishBoy

Feeeesh
Joined
1 Aug 2004
Messages
1,685
it is dooonnne!!:D

sorry guys my project is due tomorrow and i think im almost done but the out put is bogus... it's driving me craaazy i need someone to debug or tell me what to fix... thx a million

edit: i fixed part of the problem but the output is still messed up...
edit: i removed this attachement from here and posted a newer one on top
 
This is a mess:

Missiles Away: 00 Missiles Left: 50
Current Score: 000 Last Move: N/A
Enter Targer Coordinates--> q

Program received signal EXC_BAD_ACCESS, Could not access memory.
0x90006e70 in strlen ()
(gdb) bt
#0 0x90006e70 in strlen ()
#1 0x900049fc in __vfprintf ()
#2 0x9000c66c in vfprintf ()
#3 0x9002ac1c in printf ()
#4 0x0000257c in update_data (board=Cannot access memory at address 0x7a
) at sziadeh3.c:107
#5 0x00002200 in play_battleship () at sziadeh3.c:54
#6 0x00002108 in main () at sziadeh3.c:40


It gives me a bus error, basically, you are trying to access bad memory.

Also, when i type in coords, it rescrolls by a menu, shows the wrong input, and says i made no moves yet. Which i think is what you mean by wrong output.
 
whaaat the.... this is really messed up so i'm getting the wrong array address or my arrays are too small.. do you think it's one of those.. but the funny thing is that i never got any of these errors i compiled it on my unix account at school on telnet both machine didn't give me any error, one machine is IBM unix and the other is RedHat unix.. dont know what are those but they have a great compiler, which compiler did you use
 
It compiles fine here using gcc 3.3.4 on Gentoo, but I get the same results as X, and it segmentation faults.

I haven't done any c++ programming for about 6 years so I got no idea whats wrong with it.
 
yeaa that's why im not getting X-Istence's errors it's just giving me a segmentation fault... it's C code not C++ btw im glad it's not C++ yet it is harder but i'll get used to it, so my segmentation fault is X-Istence's post right?

what do you think is causing that segmentation fault
 
hey X-I how do you use the gdb thing i forgot how to my teacher showed me once but i forgot it
 
aah found my problem it's in the update_data function in the if statement

Code:
if(check_move(move)==1){

when it checks the move the first time it it returns 1 but when it checks it in the if statement it returns 0 and the program crashes

correction: the if statement is not the only problem, how do i catonate 2 indices of the array i put
Code:
strcat(&move[0],&move[1]);
but when i print out move[0] it doens't show up as combined with move 1

... sorry guys for all the trouble
 
That error is with gdb, i did a backtrace of the stack. It happens because "place" in update_data (i think it is there), uses printf to print out "%s is a invalid move", place, well place is broken data, as it is pointing to random addresses and not the correct one, which the program is not allowed to access, so it is a bus error, or if not compiled with -g as well, it would be a segfault.

THey are allready concatenated, if you want to place them into "place" which BTW is the length of one character.

You are trying to add move[1] to move[0], but move[0] can't hold anymore as it is only one char wide, and thus can only hold move[0]. So you would need to make place a [], and then you can just copy it by doing place = move, right now it is broken.

You are also overwriting memory all over the place. Check out the screenshots attached. Sometimes you send stuff by reference, and other times you let the compiler copy the variable. You are also sending non array's to functions that expect an array (your char2INT is one, you send it two seperate pointers, when it is expecting an array to work on, bad idea). Also you use check_move() in play_battleship(), but you are not checking its return value, so it could be any random value that it wants and it would work.

You need to revise your code, ask your teacher for help. As currently i am sorry to say it is one big mess that is overwriting it's own memory all over the place.
 

Attachments

  • overwritemem.jpg
    overwritemem.jpg
    85.2 KB · Views: 131
  • overwritemem2.jpg
    overwritemem2.jpg
    84.8 KB · Views: 120
lol to think of it after reading this i did a stupid thing, well at least the due date is extended 'till saturday, im off now to school i have programming class at 1 30 he might be able to help me :D thx X-Istance, if you even need any help in programming i'm here to help you coz im just the best :rolleyes: .. thx again man
 
ok i've worked on it for like a whole day and i think it really did improve except for a few flaws, can you check it out see if you get anything bad, like overwritting memory.. thx
edit: attachment moved up again :D
 
Will look at it tommorow (Saturday), if i find anything ill let you know.
 
2 things:

1. check_move, check for strlen == 2, before you check the rest using the long if statement
2. you use check_move(); in play_battleship(); without checking it's return value.

X-Istence
 
oh cool so my program is like almost working :D??

lol btw i showed my teatcher your post in class he started laughing when he read that im overwriting memory all over the place.. haha and you should've seen when told him that's it im gonna use goto statements, he screamed he's like nooo dont use those

ok now it's running but when i enter a right coordinate the whole hidden board is unveiled from ZZ till the coordinate entered

edit: source code attached here
edit again: a newer code attached up there
 

Attachments

  • sziadeh3.txt
    4.5 KB · Views: 128
Only looked at the code, did not run it.

Well overwriting memory is a pretty common thing in C, it is also what causes the most hacks as sometimes it can be used to get other programs to run arbitrary code that is in memory.

Memory management is a pain :p.
 
when i told my teacher the guy who's helping me is like 2yrs younger and i feel pretty stupid not knowing these things i thought he'd be shocked, it turns out he started coding when he was 9 :eek:
 
wow that is pretty early, i started in grade 11 with a crapy teacher for visual basic :(
but good thing i bumped into mafia at the beg. of grade 12 i really didn't know much about computers or what he used to do on my computer but i know almost everything, i fix people's computers here at my dorm
 
hey XI in my program when i enter a coordinate can you see why i get all the 'X' to fill up the screen up to the coordinate entered instead of just one X?
edit: i finished it :D:D
 
it is dooonnne!!:D

yaay finally i finished it except for a few tiny flaws but everything is working great :D thx X-I
 

Attachments

  • Battleship.txt
    4.4 KB · Views: 130

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back