Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么要使用此代码的段Villation_C++_Linux_Memory_Segmentation Fault - Fatal编程技术网

C++ 为什么要使用此代码的段Villation

C++ 为什么要使用此代码的段Villation,c++,linux,memory,segmentation-fault,C++,Linux,Memory,Segmentation Fault,我正在使用CentOS 6.5 64位。我运行的程序是用GCC(G++really)4.4.7编译的。当它被编译为32位时,所有的工作都很好(无论如何,通过这一点)。下面是代码(它只是安排了一个SCSI Write16命令): 我已经将堆栈大小增加到32mb(如果我没记错的话是16)。但是,我的最大内存是无限的,虚拟内存也是无限的。在这一点上,我被难住了,想要第二双眼睛 编辑 我发现代码有问题。问题在于FillRandom()函数。此函数使用random()库调用。我注意到这个函数的返回类型是

我正在使用CentOS 6.5 64位。我运行的程序是用GCC(G++really)4.4.7编译的。当它被编译为32位时,所有的工作都很好(无论如何,通过这一点)。下面是代码(它只是安排了一个SCSI Write16命令):

我已经将堆栈大小增加到32mb(如果我没记错的话是16)。但是,我的最大内存是无限的,虚拟内存也是无限的。在这一点上,我被难住了,想要第二双眼睛


编辑 我发现代码有问题。问题在于FillRandom()函数。此函数使用random()库调用。我注意到这个函数的返回类型是long int。我的致命假设是long int总是4字节宽。将传入数组填充为如下所示:

void FillRandom(const void *pBuff, UInt32 size) {
    // size is in bytes, convert to integer sized quantities
    UInt32 numLongIntObjs = size / sizeof(long int); // if the area isn't quite divisible, oh well
    long int * pTemp = reinterpret_cast<long int*>(const_cast<void*>(pBuff));

    for(UInt32 i = 0; i < numLongIntObjs; i++) {
        *pTemp = random();
        pTemp++;
    }
}
void FillRandom(const void *pBuff, UInt32 size) {
    // size is in bytes, convert to integer sized quantities
    UInt32 numDwordObjs = size / 4; // *** OH NO, not good ***
    long int * pTemp = reinterpret_cast<long int*>(const_cast<void*>(pBuff));

    for(UInt32 i = 0; i < numDwordObjs; i++) {
        *pTemp = random();
        pTemp++;
    }
}
void FillRandom(常量void*pBuff,UInt32大小){
//大小以字节为单位,转换为整数大小的数量
UInt32 numLongIntObjs=size/sizeof(long int);//如果区域不是很可分割的,那么
long int*pTemp=重新解释强制转换(const强制转换(pBuff));
对于(UInt32 i=0;i
最初,代码看起来更像这样:

void FillRandom(const void *pBuff, UInt32 size) {
    // size is in bytes, convert to integer sized quantities
    UInt32 numLongIntObjs = size / sizeof(long int); // if the area isn't quite divisible, oh well
    long int * pTemp = reinterpret_cast<long int*>(const_cast<void*>(pBuff));

    for(UInt32 i = 0; i < numLongIntObjs; i++) {
        *pTemp = random();
        pTemp++;
    }
}
void FillRandom(const void *pBuff, UInt32 size) {
    // size is in bytes, convert to integer sized quantities
    UInt32 numDwordObjs = size / 4; // *** OH NO, not good ***
    long int * pTemp = reinterpret_cast<long int*>(const_cast<void*>(pBuff));

    for(UInt32 i = 0; i < numDwordObjs; i++) {
        *pTemp = random();
        pTemp++;
    }
}
void FillRandom(常量void*pBuff,UInt32大小){
//大小以字节为单位,转换为整数大小的数量
UInt32 numwordobjs=size/4;//***哦,不,不好***
long int*pTemp=重新解释强制转换(const强制转换(pBuff));
对于(UInt32 i=0;i
而不是回答我自己的问题。我将以这种方式进行编辑,并将其干净地标记为关闭

顺便说一句,我通过valgrind运行代码无意中发现了这一点。Valgrind没有发现问题。事实上,Valgrind说它有自己的问题,想提交一份bug报告。然而,在我识别出导致SEGV的代码之前很久,它就发表了一篇关于4字节错误写入的声明,引起了我的注意,并将我带到了这里


编辑2
有趣的是,结束这个问题不是我想的。哦,好吧。我将把这个放在这里几天,也许一周,这样在搜索中点击这个的人就会看到解决方案。然后,我将删除。

当构建对象时,pBuff_uuu是否初始化为NULL?为什么要重新解释投射
?什么是
pBuff_uuu
类型?@JoachimIsaksson是的,缓冲区由对象的ctor初始化为NULL。另外,我知道它是空的,因为当达到该条件并分配内存时,问题就发生了:gdb显示的核心文件的验尸结果一样多。@VadimProzorov pBuff_uz的类型是void*。