相关疑难解决方法(0)

如何捕获空指针异常?

  try {
        int* p = 0;
        *p = 1;
    } catch (...) {
        cout << "null pointer." << endl;
    }
Run Code Online (Sandbox Code Playgroud)

我试图抓住这样的例外,但它没有用,有什么帮助吗?

c++ exception-handling try-catch

41
推荐指数
5
解决办法
7万
查看次数

在Arduino中尝试/捕获块

我正在使用Arduino中的套接字通信,我需要try/catch块来正确处理,你们有什么建议?我在互联网上的搜索没有成功.

编辑:

我正在使用的代码使用WiFly模块与移动应用程序进行交互,我正在使用Android构建一个机器人模块,其中包含对移动应用程序的一些控制.一切正常,但有时套接字断开连接,所以我需要为这种情况添加处理,类似于try/catch块,但我没有找到类似的Arduino块.

我的代码:

void loop() {
    Client client = server.available();
    if (client) {
        while (client.connected()) {
            if (client.available()) {
                // Serial.print("client connected \n");
                char c = client.read();

                if(c == 'L')
                    turnLeft();
                if(c == 'R')
                    turnRight();
                if(c == 'F')
                    goForward();
                if(c == 'B')
                    goBackward();
                if(c == 'S')
                    Stop();

                Serial.print(c);
            }
        }

        // give the web browser time to receive the data
        delay(100);
        client.stop();
    }
}
Run Code Online (Sandbox Code Playgroud)

arduino try-catch

15
推荐指数
2
解决办法
4万
查看次数

找到作为c中函数的参数接收的整数数组的大小

我想找到作为函数参数传递的整数数组的大小.这是我的代码

void getArraySize(int arr[]){
  int len = (sizeof(arr)/sizeof(arr[0])
  printf("Array Length:%d\n",len);
}

int main(){
 int array[] = {1,2,3,4,5};
 getArraySize(array);
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下警告

sizeof on array function parameter will return size of 'int *' instead of 'int []' [-Wsizeof-array-argument]

请帮忙,以便我可以在里面找到整数数组的长度function getArraySize 但是我能够找到函数array内部的大小.因为main它是作为一个传递的pointer,所以无法在中找到lengthfrom function.

我确实有一个想法.我可以把它放在一个try/catch块(C没有尝试捕获,只有操作系统依赖的跳线)和循环,直到我得到一个segmentation fault.

有没有其他方法可以用来找到integer array内部的长度function

c arrays

12
推荐指数
4
解决办法
2万
查看次数

求解用户定义的变量方程

我非常感谢C,Python,C++或Javascript中的答案.我读了几本书,做了所有的例子.现在我想写一个简单的程序.但是,我已经遇到了以下障碍:

我的目的是从用户那里得到一个等式并将其保存在变量中,例如:

-3*X+4 or pow(2,(sin(cos(x))/5))       >  [In valid C Math syntax]
Run Code Online (Sandbox Code Playgroud)

然后计算某个X值的给定表达式.像这样的东西:

printf("%g", UserFunction(3.2))   // Input 3.2 for X in User's Function and Print Result
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?对于我的生活,我无法弄清楚这一点.令我沮丧的是,解决方案可能只是一个非常简单的解决方案.先感谢您.

c variables equation solver

8
推荐指数
1
解决办法
948
查看次数

C++异常 - 抛出一个字符串

我的代码遇到了一个小问题.出于某种原因,当我尝试使用下面的代码抛出一个字符串时,我在visual studio中出错了.

#include <string>
#include <iostream>
using namespace std;

int main()
{

    char input;

    cout << "\n\nWould you like to input? (y/n): ";
    cin >> input;
    input = tolower(input);

    try
    {
        if (input != 'y')
        {
            throw ("exception ! error");
        }
    }
    catch (string e)
    {
        cout << e << endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

错误:

错误

链接:http://i.imgur.com/pYAXLuU.png

c++ error-handling exception-handling exception

8
推荐指数
2
解决办法
2万
查看次数

我可以在普通C(GCC)中抛出(并可能捕获)异常吗?

是否可以从普通C函数中抛出异常,以便在x86(可能还有x86-64)Linux中使用gcc捕获C++函数?

如何在C代码中捕获C++异常?

我希望有一种方法,因为异常帧信息已经存在.

注1:我不关心兼容性.只有gcc的黑客攻击(内部函数调用,语言扩展等)才能正常运行.

注2:由于多种原因,我无法用g ++编译应用程序的普通C部分.

c c++ gcc exception-handling

5
推荐指数
0
解决办法
342
查看次数

如何构建有很多故障点的代码?

我有一段代码在很多地方都可能失败。每增加一个操作(也可能会失败),可读性就会变差。有没有更好的方法来构建这个?有这方面的设计模式吗?

int32_t InitializeMyThingy(void) {
  int32_t retcode;

  retcode = DoA();
  if (retcode != 0) return retcode;
  retcode = DoB();
  if (retcode != 0) return retcode;
  retcode = DoC();
  if (retcode != 0) return retcode;
  retcode = DoD();

  return retcode;
}
Run Code Online (Sandbox Code Playgroud)

或者(更具可读性?):

int32_t InitializeMyThingy(void) {
  int32_t retcode;

  retcode = DoA();
  if (0 == retcode) {
    retcode = DoB();
    if (0 == retcode) {
      retcode = DoC();
      if (0 == retcode) {
        retcode = DoD();
      }
    }
  }

  return retcode;
}
Run Code Online (Sandbox Code Playgroud)

c return-value

4
推荐指数
1
解决办法
121
查看次数

调用memcpy报告错误

如果有人在代码中使用特定功能(例如memcpy),我怎么能确定它会返回错误.

我们已经从代码中删除了memcpy的所有实例,其中包含一些内部设计的函数,

我想确定的是,每当有人在将来使用memcpy时,编译器会抛出错误信息.

c

3
推荐指数
1
解决办法
156
查看次数

程序结束时的 C 错误处理

我已经阅读了很多关于 C 错误处理的教程和初学者问题。它们(大多数)似乎都朝着这个方向发展:

int main(){

if(condition){
    fprintf(stderr, "Something went wrong");
    exit(EXIT_FAILURE); // QUIT THE PROGRAM NOW, EXAMPLE: ERROR OPENING FILE
}

exit(0)
}
Run Code Online (Sandbox Code Playgroud)

我的问题:C 中是否有任何特定函数可以让我捕获错误,但只影响程序(主)退出时的状态?我的想法的例子:

int main(){

if(condition){
    fprintf(stderr, "Something went wrong");
    // Continue with code but change exit-status for the program to -1 (EXIT_FAILURE)
}

exit(IF ERROR CATCHED = -1)
}
Run Code Online (Sandbox Code Playgroud)

或者我是否必须创建一些自定义函数或使用一些指针?

c exit stderr

3
推荐指数
1
解决办法
3802
查看次数

输入两个数字的值

实际上,我正在学习C语言,并且编写了一个程序

输入2个数字的值,如下所示。

#include<stdio.h>
int main()
{
    int a, b, c;
    printf("Enter two numbers to add\n");
    scanf("%d%d", &a, &b);
    printf("Sum of the numbers = %d\n", c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我输入一个字母,我会得到一些1522222数字。取而代之的是,如果我输入字母(即a,b,c),它会抛出错误作为无效输入。

我该怎么办?

c input scanf

0
推荐指数
1
解决办法
73
查看次数