C++ Error: Run-Time Check Failure #2 – Stack around the variable ‘cc’ was corrupted.

Run-Time Check Failure #2 – Stack around the variable ‘cc’ was corrupted.

char cc[1024]; // Here, if the index value is set small, there will be a problem. For example: char CC [1]; Is due to the creation of the array subscript overflow caused

 1 // vc2_2_4UDPserver_Txwtech.cpp : Defines the entry point of the console application.
 2 //
 3 #include "StdAfx.h"
 4 #include <WinSock2.h>
 5 #include<stdio.h>
 6 #include<Windows.h>
 7 #pragma comment(lib,"WS2_32.lib")
 8 int main()
 9 {
10     WSADATA data;
11     WORD w=MAKEWORD(2,0);
12     char sztext[]="WELCOME\r\n";
13     ::WSAStartup(w,&data);
14     SOCKET s;
15     s=::socket(AF_INET, SOCK_DGRAM,0);
16     sockaddr_in addr,addr2;
17     int n=sizeof(addr2);
18     char buff[10]={0};
19     addr.sin_family=AF_INET;
20     addr.sin_port=htons(75);
21     addr.sin_addr.S_un.S_addr=INADDR_ANY;
22     ::bind(s,(sockaddr*)&addr,sizeof(addr));
23     printf("The UDP server has started \r\n");
24 while(1)
25 {
26 char cc[1024]; // Here, if the index value is set small, something will go wrong. For example: char cc[1]; is caused by an overflow of the subscript of the array created
27             //c=getchar();
28     //    c='';
29         printf("PLEASE INPUT C:");
30         scanf("%s",cc);
31         //sztext=c;
32     //    strcpy(sztext,c);
33         if (strcmp(cc,"qq")==0)
34 
35             {
36                 printf("EXIT %s",cc);
37                 ::Sleep(1000);
38                 ::closesocket(s);
39                 ::WSACleanup();
40             //    strcpy(cc,"");
41                 return 0;
42             }
43         if(::recvfrom(s,buff,10,0,(sockaddr*)&addr2,&n)!=0)
44         {
45             printf("%sCONTEECTED \r\n",inet_ntoa(addr2.sin_addr));
46             printf("%s\r\n",buff);
47             
48             
49             ::sendto(s,sztext,sizeof(sztext),0,(sockaddr*)&addr2,n);
50         //    break;
51         }
52         
53     }
54     ::closesocket(s);
55     ::WSACleanup();
56     
57     if(getchar())
58     {
59         return 0;
60         //::sendto(s,sztext,sizeof(sztext),0,(sockaddr*)&addr2,n);
61     }
62     else
63     {
64         ::Sleep(100);
65     }
66 }

Similar Posts: