无法了解C代码中未处理异常背后的原因 [英] Unable to know the cause behind unhandled exception in C code

查看:59
本文介绍了无法了解C代码中未处理异常背后的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临一种独特的错误情况.
我写了一个简单的代码.这是我正在调用dll函数的c程序.
IDE是Visual Studio 2008&代码在C编译器(TC)中编译
代码是:

Hi I am facing one unique error situation.
i have written a simple code. This is c program in which I am calling a dll''s function.
The IDE is Visual studio 2008 & code is compiled in C compiler(TC)
The code is :

#include <conio.h>
#include <stdio.h>
#include <string.h>
#include "API_DLL.h"
 
int main()
{
    int iFuncStatus;
    int iChoice;
    int iChk=0;
    char strIP[50];
    char cChoice=''n'';
    struct PingInfoC *pi;
 
st:
    do
    {
    printf("\n***************************************************************************");
    printf("\n API dll Testing Application v1.0");
    printf("\n___________________________________________________________________________\n");
    printf("\n 1. GetLinkConfiguration() \n 2. SetLinkConfiguration() \n 3. GetLinkStatus() ");
    printf("\n 4. GetList() \n 5. SetCurrent() \n 6. SetCurrentName() ");
    printf("\n 7. GetInformation() \n 8. GetStatus() \n 9. SetDefaultConfiguration() ");
    printf("\n 10. Reset() \n 11. Ping() \n 12. UpdateFirmware() ");
    printf("\n 13. UpdateFirmwareStatus() \n 0. Exit ");
    printf("\n****************************************************************************");
    printf("\n Please enter your choice to call the respective function(1...13) :");
    scanf("%d", &iChoice);
 
    if(iChoice<1 && iChoice>13)
    {
        printf("\n Invalid entry !!");
        iChk=0;
    }
    else
        iChk=1;
    printf("\n****************************************************************************");
 
    } while (iChk==0);
 
    switch(iChoice)
    {
       case 1: //GetLinkConfiguration();
               break;
       case 2: //SetLinkConfiguration();
               break;
       case 3: //GetLinkStatus();
               break;
       case 4: //GetList();
               break;
       case 5: //SetCurrent();
               break;
       case 6: //SetCurrentName();
               break;
       case 7: //GetInformation();
               break;
       case 8: //GetStatus();
               break;
       case 9: //SetDefaultConfiguration();
               break;
       case 10: //Reset();
                break;
       case 11: //Ping();
                memset(strIP,0,sizeof(strIP));
                printf("\n Calling Ping() ...");
                printf("\n Please enter IP Address : ");
    //          /*gets(strIP);*/
                //scanf("%s", strIP);
                ////scanf("%s", strIP);
                ////strIP[]="192.168.36.120";
                /*pi->M_ID =strIP;*/
                /*pi->M_ID="192.168.36.120";*/
                pi->M_ID="192.168.36.120";
 
                iFuncStatus=Ping(pi);
 
                if(iFuncStatus<0)
                    printf("Ping Failed=%d",iFuncStatus);
                else
                    {
                        printf("Ping Success=%d",iFuncStatus);
                        printf("\nID=%s \nAnswer_Delay=%ld \nTime_Out=%d \nTimeout_Delay=%d",
                                 pi->M_ID,pi->Answer_Delay,pi->Timeout,pi->Timeout_Delay);
                    }
                break;
       case 12: //UpdateFirmware();
                break;
       case 13: //UpdateFirmwareStatus();
                break;
       case 0: return 0;
 
       default : printf("\n Invalid entry !!");
                 iChk=0;
                 goto st;
 
    }
 
    do
    {
        printf("\n Do you want to continue (Y/N) ? : ");
        scanf("%c",cChoice);
        if(cChoice==''y''|| cChoice==''Y'')
                goto st;
        else if(cChoice==''n''|| cChoice==''N'')
                exit(0);
    }while (cChoice!=''y'' || cChoice!=''Y'');
 
    getch();
}


当我尝试执行此代码时,在此出现未处理的异常错误
语句[iFuncStatus = Ping(pi);]

甚至我无法通过直接分配值或通过输入将值分配给"strIP" (请参阅注释的gets(strIP)& scanf语句)

另一个代码:
这也是可以正常运行并在没有任何异常的情况下执行的相同代码.


when I am trying to execute this code I am getting an Unhandled exception error at this
statement [ iFuncStatus=Ping(pi);]

even I am not able to assign a value to "strIP" by directly assigning the value or by taking input (Please refer to the commented gets(strIP) & scanf statements)

Another Code:
This is also the same code which is running properly and executing without having any exceptions.

#include <conio.h>
#include <stdio.h>
#include "API_DLL.h"
int main()
{
 
    struct PingInfoC *pi;
    int iFuncStatus;
pi->M_ID ="192.168.36.120";
    iFuncStatus=Ping(pi);
    printf("iFuncStatus=%d",iFuncStatus);
    if(iFuncStatus<0)
        printf("Ping Failed=%d",iFuncStatus);
    else
    {printf("Ping Success=%d",iFuncStatus);
    printf("\nID=%s \nAnswer_Delay=%ld \nTime_Out=%d \nTimeout_Delay=%d",pi->M_ID,pi->Answer_Delay,pi->Timeout,pi->Timeout_Delay);
    }
 
    getch();
}


谁能告诉我这两个代码之间的区别是什么,以及为什么第一个代码生成未处理的异常而第二个代码没有生成.我向您保证,除了这两个代码外,其余所有设置,名称,IDE甚至解决方案都相同.
等待您的答案.
谢谢
Dinesh


Can anyone tell me what is the difference between these two codes and why the first one is generating unhandled exception and 2nd one is not generating. I am assuring you that except these 2 codes, rest all settings, names, IDE and even the solution is same for both.
waiting for your answers.
Thanks
Dinesh

推荐答案

struct PingInfoC *pi;
// ...no allocation of memory for *pi is done here...
pi->M_ID ="192.168.36.120";


错了!
这两个程序都错误地使用了变量pi:它们为struct成员分配了一个值,而没有先为struct本身分配内存(pi指向垃圾).后一个程序可能有点幸运".
您可以通过这种方式修复它


Wrong!
Both programs uses variable the pi incorrectly: they assign a value to the struct member without first allocating memory for the struct itself (pi points to garbage). Probably the latter program is just a bit ''lucky''.
You fix it, for instance, this way

struct PingInfoC pi;
pi.M_ID = "192.168.36.120";
iFuncStatus=Ping(&pi);
//...


您确定这是C而不是C ++吗?因此,请记住该语言没有例外.但是,您仍然可以使用Windows API使用异常机制.

方法如下:
不使用C ++的C中的异常处理 http://www.on-time.com/ddj0011. htm [ ^ ].

您可以在栈顶(如果创建线程,则在每个线程)上捕获所有异常,并使用Debugger进入处理程序.这样,您可以获得所有异常信息.在调试"配置中,您可以获得带有代码行号的异常堆栈.

—SA
Are you sure this is C, not C++? It so, remember there are no exceptions in the language. However, you still can use exception mechanism using Windows API.

Here is how:
Exception Handling in C without C++, http://www.on-time.com/ddj0011.htm[^].

You can catch all exceptions on top of the stack (of every thread if you create threads) and get in the handler using Debugger. In this way, you can get all exception information. In Debug configuration, you can get exception stack with the code line numbers.

—SA


这篇关于无法了解C代码中未处理异常背后的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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