Programm crashes due to the presence of code even if it is not executed
Dear all,
I have written the following program using Visual Studio Express 2013 to figure out my problem:
#pragma comment (lib, "ws2_32.lib")
#include <stdio.h>
#include <winsock2.h>
int main (int argc, char *argv[])
{
WSADATA wsa;
if (1==2) WSAStartup(MAKEWORD(2,0),&wsa);
} // main
The programm crashes after start "Unhandled exception at 0x5DF1E71A in 3D_20.exe: 0xC0000005: Access violation writing location 0x000A0FF8."
The problem is the code WSAStartup(MAKEWORD(2,0),&wsa);
You see that the (1==2) never will be true so WSAStartup will not be excuted, but the presence of this piece of code makes the program crash.
Useless to say that compiling give no errors.
If I omit the critical line, the program runs withpout problems.
If I insert printf... or whatever after main and before WSAStartup this will not executed! The problem is the pure existence of WSAStartup in the code!
This is very strange to me. Any ideas?
Best Regards,
Andreas
Re: Programm crashes due to the presence of code even if it is not executed
Quote:
Originally Posted by
anhu
Dear all,
I have written the following program using Visual Studio Express 2013 to figure out my problem:
#pragma comment (lib, "ws2_32.lib")
#include <stdio.h>
#include <winsock2.h>
int main (int argc, char *argv[])
{
WSADATA wsa;
if (1==2) WSAStartup(MAKEWORD(2,0),&wsa);
} // main
The programm crashes after start "Unhandled exception at 0x5DF1E71A in 3D_20.exe: 0xC0000005: Access violation writing location 0x000A0FF8."
The problem is the code WSAStartup(MAKEWORD(2,0),&wsa);
You see that the (1==2) never will be true so WSAStartup will not be excuted, but the presence of this piece of code makes the program crash.
Useless to say that compiling give no errors.
If I omit the critical line, the program runs withpout problems.
If I insert printf... or whatever after main and before WSAStartup this will not executed! The problem is the pure existence of WSAStartup in the code!
This is very strange to me. Any ideas?
Best Regards,
Andreas
Rewrite your code to be convenient to debug:
Code:
int main (int argc, char *argv[])
{
WSADATA wsa;
if (1==2)
{
WSAStartup(MAKEWORD(2,0),&wsa);
}
} // main
set a breakpoint in the begin of main(...), start debugging and see what happens.
Re: Programm crashes due to the presence of code even if it is not executed
Is that your actual code? Sounds like you may have if(1 = 2)
Re: Programm crashes due to the presence of code even if it is not executed
Hi all,
I have changed the code to
#pragma comment (lib, "ws2_32.lib")
#include <stdio.h>
#include <winsock2.h>
int main (int argc, char *argv[])
{
WSADATA wsa;
int i;
for (i=10;i<5;i++)
{
WSAStartup(MAKEWORD(2, 0), &wsa);
}
}
and the same happens than before.
For those who complained about "if (1==2)":
You see, that the for loop will never be executed and still the program crashes.
I set a breakpoint at main ... and the output is
Attachment 35912
what can I learn from this? I am not familiar with debugging as in C-tutorials usually there is not chapter about "working with debuggers".
Any help is appreciated.
Best regards,
Andreas
Re: Programm crashes due to the presence of code even if it is not executed
Quote:
Originally Posted by
anhu
The problem is the pure existence of WSAStartup in the code!
Just because a section of code cannot be reached due to the program logic doesn't mean it's not present in the executable. It may still influence what else gets included that can cause problems.
I would suggest you leave this for a while and instead concentrate on getting WSAStartup to work properly. When it does work you most likely will realize what's wrong now (even though it doesn't matter anymore :)). Why no start with the example code here,
https://docs.microsoft.com/en-us/win...ock-wsastartup
Re: Programm crashes due to the presence of code even if it is not executed
Quote:
Visual Studio Express 2013
I strongly recommend that you upgrade to Visual Studio 2019 Community (free). Express 13 is now 7 years old........
The provided code compiles and runs OK on my computer using VS2019. What OS are you using? Are you compiling as 32/64 bit? Express had some limitations - but I can't remember what they were apart from no MFC.
Re: Programm crashes due to the presence of code even if it is not executed
Quote:
Originally Posted by
anhu
Hi all,
I have changed the code to
#pragma comment (lib, "ws2_32.lib")
#include <stdio.h>
#include <winsock2.h>
int main (int argc, char *argv[])
{
WSADATA wsa;
int i;
for (i=10;i<5;i++)
{
WSAStartup(MAKEWORD(2, 0), &wsa);
}
}
and the same happens than before.
For those who complained about "if (1==2)":
You see, that the for loop will never be executed and still the program crashes.
I set a breakpoint at main ... and the output is
Attachment 35912
what can I learn from this? I am not familiar with debugging as in C-tutorials usually there is not chapter about "working with debuggers".
Any help is appreciated.
Best regards,
Andreas
Your "attachment is invalid:
Quote:
Invalid Attachment specified. If you followed a valid link, please notify the administrator
Where exactly did you set a breakpoint?
Where exactly "the program crashes"? Please, copy the output and post it here.