Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.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 ADRESS是大小块后的0字节,ADRESS不是堆栈';d、 马洛克';d或免费';D_C - Fatal编程技术网

C ADRESS是大小块后的0字节,ADRESS不是堆栈';d、 马洛克';d或免费';D

C ADRESS是大小块后的0字节,ADRESS不是堆栈';d、 马洛克';d或免费';D,c,C,因此,我试图运行以下程序,但当我试图在跳过列表中插入一个值时,我得到了一个分段错误。我的目标是在已排序的跳过列表中插入所有值 int main(int argc, char const *argv[]) { int x; record *ptrs[5]; int i; for ( i = 0; i < 5; i++) { ptrs[i] = malloc(sizeof(record)); } node_ptr skip_list = creat_skip

因此,我试图运行以下程序,但当我试图在跳过列表中插入一个值时,我得到了一个分段错误。我的目标是在已排序的跳过列表中插入所有值

 int main(int argc, char const *argv[]) {
  int x;
  record *ptrs[5];
  int i;

  for ( i = 0; i < 5; i++) {
    ptrs[i] = malloc(sizeof(record));
  }
  node_ptr skip_list = creat_skip_list();

  for ( i = 0; i < 5; i++) {
    ptrs[i] = malloc(sizeof(record));
    scanf("%d", &x);
    ptrs[i] -> x = x;
    insert_skip_list(skip_list, x, ptrs[i]);

  }
这是什么原因造成的?因为我似乎已经把我要进入的每个街区都安排好了。MaxValue为200,MaxLevel为20。

这是您的错误:

for ( i = MaxLevel; i >= 0; i--) {
    while (temp->forward[i]->key < searchKey) {

这样做的好处是,数组[i]的第一次访问不会超出数组。这就是valgrind在这里发现的。另外,这样我可以不签名-在您的代码中,它必须被签名,否则,i>=0将始终为真。

main
中,您分配了
ptr
两次,内存泄漏。我删除了第一次分配,但仍然得到了一个分段错误
srand(时间(NULL));返回rand()%(MaxLevel-1)
可能会生成与使用相同时间戳重复调用
srand()时相同的随机数。Beter调用
srand()建议在C++中使用“<代码> > MalOC < /C++ >之前,请删除C+标签。在C++中,不使用C++标签的另一个原因是所提供的代码不是C++。实际上,问题是我从Max级开始而不是Max Leave1启动。MaxLevel,然后不以这种或那种方式递减。这个问题有很多解决方案,有些更好,有些更糟。我给你看了一个,可以防止你再次犯这种错误。但你不必采取这种解决方案。无论如何,如果我的回答对你有帮助,请接受它。
 ==5124== Invalid read of size 8
==5124==    at 0x400A25: insert_skip_list (skip_list.c:55)
==5124==    by 0x4007E0: main (test_skip.c:23)
==5124==  Address 0x51e02d0 is 0 bytes after a block of size 176 alloc'd
==5124==    at 0x4C28C20: malloc (vg_replace_malloc.c:296)
==5124==    by 0x400898: creat_skip_list (skip_list.c:20)
==5124==    by 0x400766: main (test_skip.c:16)
==5124== 
==5124== Invalid read of size 4
==5124==    at 0x400A29: insert_skip_list (skip_list.c:55)
==5124==    by 0x4007E0: main (test_skip.c:23)
==5124==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==5124== 
==5124== 
==5124== Process terminating with default action of signal 11 (SIGSEGV)
==5124==  Access not within mapped region at address 0x0
==5124==    at 0x400A29: insert_skip_list (skip_list.c:55)
==5124==    by 0x4007E0: main (test_skip.c:23)
for ( i = MaxLevel; i >= 0; i--) {
    while (temp->forward[i]->key < searchKey) {
for (i = MaxLevel; i--; ) {