• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

ugh help in C again asap

#1
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
 

X-Istence

*
Political User
#2
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.
 
#3
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
 

j79zlr

Glaanies script monkey
Political User
#4
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.
 
#5
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
 
#7
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
 

X-Istence

*
Political User
#8
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

#9
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
 
#10
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
 

X-Istence

*
Political User
#12
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
 
#13
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

X-Istence

*
Political User
#14
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.
 
#15
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:
 
#17
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
 
#18
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
 

Members online

No members online now.

Latest posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,962
Messages
673,240
Members
89,015
Latest member
loxioalix