Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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 GDB:“是的;警告:类型<;的范围;错误类型>;“具有无效的界限”;_C_Multidimensional Array_Gdb_Warnings_Pass By Reference - Fatal编程技术网

C GDB:“是的;警告:类型<;的范围;错误类型>;“具有无效的界限”;

C GDB:“是的;警告:类型<;的范围;错误类型>;“具有无效的界限”;,c,multidimensional-array,gdb,warnings,pass-by-reference,C,Multidimensional Array,Gdb,Warnings,Pass By Reference,我有一个C/GDB编码问题,这让我发疯。(使用GCC作为我的编译器,在Linux机器上编码。) 我正在做一项工作,我必须编写一个硬件缓存,这基本上意味着我需要使用一个2D结构数组,我已经定义为structcacheLine。下面是我对代码中数组的看法: [*] [*] [*] [*] ...so here... numSet = 4 (columns) [*] [*] [*] [*] numLines = 12

我有一个C/GDB编码问题,这让我发疯。(使用GCC作为我的编译器,在Linux机器上编码。)

我正在做一项工作,我必须编写一个硬件缓存,这基本上意味着我需要使用一个2D结构数组,我已经定义为struct
cacheLine
。下面是我对代码中数组的看法:

 [*] [*] [*] [*]    ...so here...  numSet = 4            (columns)
 [*] [*] [*] [*]                   numLines = 12         (total)
 [*] [*] [*] [*]                   numLinesPerSet = 3    (rows)
所以“numLines”实际上是
cacheLine
structs的总数,而不是数组中的行数。(我的意思不是让人困惑,这与作业命名法有关。)

下面是我认为应该如何通过引用分配和传递数组:

void initCache(int numSets, int numLines, cacheLine* (*cache)[numLines], int numLinesPerSet){
    int i, j;
    for(i=0; i<numSets; i++){
            for(j=0; j<numLinesPerSet; j++){
                    // Now allocate each struct one-by-one
                    cache[i][j] = (cacheLine*) malloc(sizeof(cacheLine));
                    if(cache[i][j] == NULL){
                            printf("Error: not enough memory to malloc() cache.\n");
                            exit(EXIT_FAILURE);
                    }
            }
    }
}

int main(){
    ...
    cacheLine* myCache[numSets][numLines];                  // declare
    initCache(numSets, numLines, myCache, numLinesPerSet);  // allocate
    ...
}
到目前为止,一切顺利。对我来说,这就是事情变得越来越糟的地方。在代码的后面,我调用了另一个函数,该函数的签名与我的initCache()非常相似:

下面是我使用GDB进入
cacheThisData()
功能时发生的情况:

Breakpoint 1, main (argc=2, argv=0x7fffffffe308) at c-sim.c:105
105                                     cacheThisData(numSets, numLines, myCache, someData);
(gdb) step
cacheThisData(numSets=4, numLines=12, warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
myCache=warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
0x7fffffffdf28, data=1234) at c-sim3.h:145
145     void cacheThisData(int numSets, int numLines, cacheLine* (*myCache)[numLines], int someData) {
(gdb)
但是当我在
cacheThisData()
的签名中添加一个额外的参数时,我得到了上面的GDB警告,总是一样的。不管我是将额外的参数放在参数的开头、中间还是结尾,都可以放入
cacheThisData()
-GDB抱怨

我不知道该怎么看。GDB似乎在说我传入的
myCache
数组的维度不正确???但numset和numline并没有改变。我也不会使用realloc()或类似的方法来更改数组的大小。在
initCache()
cacheThisData()
函数之间,数组的大小不会改变,至少就我所知不是这样

有什么建议吗?以前有人遇到过这种情况吗

谢谢,
-皮特

我从未找到这个问题的根源。然而,我的代码能够处理相当大的数据集,而且还没有崩溃,所以这就是问题所在。我要把这归因于生活的神秘。。。继续前进。非常感谢所有停下来阅读这篇文章的人。

我从未找到这个问题的根源。然而,我的代码能够处理相当大的数据集,而且还没有崩溃,所以这就是问题所在。我要把这归因于生活的神秘。。。继续前进。非常感谢所有暂停阅读本文的人。

您使用的是哪种GCC、GDB版本?你如何编译,调用GDB?你能用你展示给我们的最少的代码重现这个吗?那就是删除所有的
?对不起,我的程序太大了。我故意想发布一个框架版本,只是为了让大家了解基本的想法。我现在明白了,这不是一篇好文章。你使用哪个GCC,GDB版本?你如何编译,调用GDB?你能用你展示给我们的最少的代码重现这个吗?那就是删除所有的
?对不起,我的程序太大了。我故意想发布一个框架版本,只是为了让大家了解基本的想法。我现在明白了,这不是一个好帖子。
void cacheThisData(int numSets, int numLines, cacheLine* (*myCache)[numLines], int someData) {
    // do stuff with someData
}

int main(){
    ...
    cacheLine* myCache[numSets][numLines];                  // from
    initCache(numSets, numLines, myCache, numLinesPerSet);  // before...
    ...
    int someData;    //  holds data to-be-cached
    cacheThisData(numSets, numLines, myCache, someData);
    ...
}
Breakpoint 1, main (argc=2, argv=0x7fffffffe308) at c-sim.c:105
105                                     cacheThisData(numSets, numLines, myCache, someData);
(gdb) step
cacheThisData(numSets=4, numLines=12, warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
myCache=warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
warning: Range for type <error type> has invalid bounds 0..-72057594037927936
0x7fffffffdf28, data=1234) at c-sim3.h:145
145     void cacheThisData(int numSets, int numLines, cacheLine* (*myCache)[numLines], int someData) {
(gdb)
void cacheThisData(int numSets, int numLines, cacheLine* (*myCache)[numLines]) {}

int main(){
    cacheThisData(numSets, numLines, myCache);   // GDB likes this
}