整数类型指针未处理的异常 [英] Integer type pointer Unhandled exception

查看:73
本文介绍了整数类型指针未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//this is server side code snippet for simple chat application , server on specified port listen for incoming connection
//from clients when there is a connection request it accept and put the returned socket discripter integer on integer type pointer here is the code 
int* sockPtr;
int result;
sockaddr_in addr;
int addrlen=sizeof(addr);
sockaddr_in client_info;
result=listen(sClient,10); //sClient is SOCKET that is bind to specific port for listening incoming connection 
sockPtr = (int*)malloc(sizeof(int)); 		
*sockPtr= accept(sClient,(SOCKADDR*)&client_info,&addrlen);//here is the exception raising on sockPtr 


但是当客户端尝试连接时,会有一个异常提示


But when a client try to connect there is an exception saying

Unhandled exception at 0x7c80979d in server.exe:
 0xC0000005: Access violation writing location 0x003d1ed0.
What shall I do?

推荐答案

该异常消息清楚地表明代码正在尝试访问零位置,这是非法的.这表明您没有正确初始化一些变量.

accept() [
The exception message clearly states that the code is trying to access location zero, which is illegal; this suggests you have not correctly initialised some variable.

The accept()[^] function returns a SOCKET not an integer. You should also check that your other structures (which you have not shown above) have been initialised correctly.

[edit]
The return value of your accept() is a pointer so should be stored directly in the variable sockPtr not in the address it points to thus:
sockPtr = accept(sClient,(SOCKADDR*)&client_info,&addrlen);


如果您始终使用Windows定义的类型,则可以避免诸如此类的问题.
[/edit]


If you always use the Windows defined types you will avoid problems such as this.
[/edit]


我自己没有使用Socket编程的经验,但是您可以在此处找到很多信息: ^ ]

据我所知,您将一个Socket(accept函数的结果)分配给了一个int.该问题可能是由于不加选择地将Socket对象重新解释为int,然后又将其重新解释为某种类型的句柄而导致的.

此外,您没有发布实际上导致异常的代码,它在哪里?
I have no experience with Socket programming myself, but you can find a lot of info here: Programming Windows TCP Sockets in C++ for the Beginner[^]

As far as I can see you assign a Socket (the result from the accept function) to an int. The problem probably results from indiscriminately reinterpreting a Socket object as int, and then reinterpreting as some kind of handle.

Besides, you didn''t post the code that actually caused the exception, where is it?


这篇关于整数类型指针未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆