Why does this crash?!

Computer builds, hardware and software discussion or troubleshooting, including peripherals. Essentially a general place to talk about desktop computers.
Locked
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Why does this crash?!

Post by Red Squirrel »

Code: Select all

char username[16];
memset(username,0,16);
int position=0;
char chardata;




strcpy(message,"
Login:");
send(theClient,message,strlen(message),0);

while((chardata!='
')&&(position<16))
{
recv(theClient,&chardata,1,0);
username[position]=chardata;
send(theClient,&username[position],1,0);
position++;
}


What did I do wrong? I'm confused. It just crashes with a bunch of weird numbers and invalid addresses, basically something worse then the illegal operation. But it makes no sense the code is as simple as it gets.

Archived topic from Anythingforums, old topic ID:1125, old post ID:14166
Honk if you love Jesus, text if you want to meet Him!
megaspaz
Posts: 340
Joined: Wed Dec 18, 2002 10:08 pm

Why does this crash?!

Post by megaspaz »

put in stubs to isolate which line is crashing. stubs are basically debugging output (in c++, use cout in various places).

where is message defined at? it looks like you're just using a prompt, so why are you using strcopy for? have the client handle user prompts like logging in prompts and use cout instead of strcopy.

Archived topic from Anythingforums, old topic ID:1125, old post ID:14539
Image
Resistance is futile...

Registered Linux User #321628
Anime/Toon Avatars
Other Cool Forums

"Never hold your farts in. They travel up your spine, into your brain, and that's where you get s**tty ideas from..." - Woyaya - January 10, 2004
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Why does this crash?!

Post by Red Squirrel »

I got it sorted out, I did not know I couldnot use a char *. I figured char * was like a php variable (unlimited) but it turns out it just uses whatever memory is available, including memory already in use, so it crashed when it got to a memory address that was in use.

strcpy is just so I don't have to count all the chars for each string insertion in the send command. Once I get this simple program working I'll tweak it better and take the time to count all the chars. (the send command requires to put the number)

Archived topic from Anythingforums, old topic ID:1125, old post ID:14577
Honk if you love Jesus, text if you want to meet Him!
Locked