小编Lih*_*ihO的帖子

为什么没有方法查找的段错误

我正在寻找关于这段代码的澄清.对A :: hello()的调用有效(我期望一个segv).segfault确实通过对成员x的访问,所以看起来单独的方法解决方案实际上并没有取消引用bla?

我编译优化关闭,gcc 4.6.3.为什么不把bla->你好()爆炸?只是想知道'发生了什么事.谢谢.

class A
{

public:
    int x;

    A() { cout << "constructing a" << endl; }

    void hello()
    {
        cout << "hello a"  << endl;
    }

};

int main()
{
    A * bla;
    bla = NULL;
    bla->hello();    // prints "hello a"
    bla->x = 5;      // segfault
}
Run Code Online (Sandbox Code Playgroud)

c++ null pointers segmentation-fault

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

为什么在迭代C中的单链表时,NULL指针检查不起作用?

我正在尝试从堆打印.如果我遇到一个NULL指针我应该打印NULL; 否则,打印它的价值.

样本输出:

1   [2]
2   null
3   null
4   [7, 3]
5   null
6   [7]
Run Code Online (Sandbox Code Playgroud)

但是我的代码因解除引用NULL指针而不断崩溃.

这是我写的测试代码:

void printResult(IntList* intL, int nNode, int nEdge)
{
    int i;
    for (i; i <= 10; i++)
    {
        if (intRest((intL))
        {
            printf("%d", intFirst((intL)[i]));
            intRest((intL)[i]);
        }
        else
            printf(" NULL ");
    }
}

//Here is the definition of functions:
//First
int intFirst(IntList oldL)
{
    return oldL->element;
}

/** rest
 */
IntList intRest(IntList oldL)
{
    return oldL->next;
}
//=================
struct IntListNode
{
    int element;
    IntList next; …
Run Code Online (Sandbox Code Playgroud)

c null pointers loops singly-linked-list

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

是否可以在没有过载的情况下为每个函数提供可变数量的参数?

如果你想能够将1到3个参数传递给它,是否有可能使它不必重载函数定义3次?

例如,而不是:

function(class a) { //this is called if only 1 class is passed
instance 1 = a;
} 
function(class a, class b) {  //this is called if 2 classes are passed
instance1 = a;
instance2 = b;
}
function(class a, class b, class c) { //this is called if 3 classes are passed
instance1 = a;
instance2 = b;
instance3 = c;
}
Run Code Online (Sandbox Code Playgroud)

你可以有:

function(class a, <if2class>class b, <if3class>class c) //this is called
                                                        //for all passes
// <ifxclass> is …
Run Code Online (Sandbox Code Playgroud)

c++ parameters function optional-parameters c++11

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

如何从PHP中的其他.php文件访问数据?

我有login.phpmainpage.php文件,我想从中访问login.php用户的数据mainpage.php.

我应该使用requireinclude?如何连接这些文件?

require login.php;
include login.php;
Run Code Online (Sandbox Code Playgroud)

php data-access require include

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

sscanf - 从空字符串中读取

当我传递一个空字符串时sscanf,它返回零,并且它不会更改给定的变量.

char* ptr = "";
abc = sscanf(ptr, "%s", output);
// abc == 0
Run Code Online (Sandbox Code Playgroud)

你能让我知道如何解决这个问题吗?

c string io scanf

-1
推荐指数
1
解决办法
3104
查看次数

'如果'找不到标识符

我是一名初学者C++学习者,我总是在visual studio 2010中的if循环上遇到问题

#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>

using namespace std;

int main(void){

    string name;
    int money;

    cout << "Hello, Enter your name here: ";
    cin >> name;
    cout << "\n\nHello " << name << ".\n\n";

    cout << "\nEnter your salary here:L";
    cin >> money;

    If(money <= 50000 || money >= 100000 );
    {
        cout << "\nGood!\n";
        } else if(money >=49999){
               cout << "\nJust begin to work?\n"
               } else if(money <= 100000){
                      cout << "\nWow!, you're rich\n"; …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++ undeclared-identifier

-2
推荐指数
2
解决办法
880
查看次数

传递给函数的char*缩短为四个字符

void myfunc(char* passedArray)
{
    cout << sizeof(passedArray) << endl; // prints 4
}

int main(void)
{
    char charArray[] = "abcdefghiklmop";

    cout << sizeof(charArray) << endl; // prints 15
    myfunc(charArray);
    cout << sizeof(charArray) << endl; // prints 15
}
Run Code Online (Sandbox Code Playgroud)

我相信它应该仍然在该函数内打印15 ...

c++ arrays sizeof parameter-passing

-2
推荐指数
1
解决办法
231
查看次数

为什么C程序崩溃了?

请有人解释为什么以下C程序崩溃:

void changeChar(char *string);

int main(int argc, char *argv[])
{
  char *test = "word";
  changeChar(test);
  return 0;
}

void changeChar(char *string) {
  *string = 'A';
}
Run Code Online (Sandbox Code Playgroud)

而以下代码完美地运作:

void changeChar(char *string);

int main(int argc, char *argv[])
{
  char test[] = "word";
  changeChar(test);
  return 0;
}

void changeChar(char *string) {
  *string = 'A';
}
Run Code Online (Sandbox Code Playgroud)

c string pointers string-literals

-2
推荐指数
2
解决办法
136
查看次数

如何在C ++中读取二进制文件

我想编写一个打开二进制文件并使用DES对其加密的程序。

但是如何读取二进制文件?

c++ file-io binaryfiles visual-c++

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

你能帮我分解一下这段代码实际上在做什么吗?if((a = b> c?d:e)== e)a ++;

int a = 2, b = 3, c = 5, d = 4, e = 1;

if ((a = b > c ? d : e) == e) a++;
Run Code Online (Sandbox Code Playgroud)

答案是a = 2

我不明白这是在做什么.

你能帮我弄清楚这段代码是一步一步做的吗?

c c++ syntax ternary-operator

-5
推荐指数
1
解决办法
314
查看次数