-
October 28th, 2020, 07:43 AM
#1
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
-
October 28th, 2020, 08:18 AM
#2
Re: Programm crashes due to the presence of code even if it is not executed
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.
Victor Nijegorodov
-
October 28th, 2020, 10:47 AM
#3
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)
-
October 29th, 2020, 12:37 AM
#4
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
-
October 29th, 2020, 02:51 AM
#5
Re: Programm crashes due to the presence of code even if it is not executed
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
-
October 29th, 2020, 03:19 AM
#6
Re: Programm crashes due to the presence of code even if it is not executed
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 29th, 2020, 04:22 AM
#7
Re: Programm crashes due to the presence of code even if it is not executed
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:
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.
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|