简体   繁体   English

一个小而奇怪的<Segmentation Fault>

[英]A small yet weird <Segmentation Fault>

I am not very experienced with C memory concepts. 我对C内存概念不是很熟悉。 I tried searching for a solution, but could not find one. 我试图寻找解决方案,但找不到一个。

I am just trying to create dynamic array in C. I tried to create it in this way while trying to check whether the addresses are contiguous. 我只是想在C中创建动态数组。我试图以这种方式创建它,同时试图检查地址是否连续。 The program ran fine. 程序运行正常。

However, I got a segmentation fault after the statement system("pause") . 但是,在语句system("pause")之后我遇到了分段错误。 I also tried to debug using the debugger, with no luck! 我也尝试使用调试器进行调试,没有运气! I'm using Dev CPP. 我正在使用Dev CPP。

Can anybody guide me? 任何人都可以指导我吗?

#include<stdio.h>
#include<stdlib.h>

main()
{
 int a[0], *ptr, i;

 printf("%d", sizeof(a[0]));
 a[0]=1;
 for(i=1;i<10;i++)
 {
  ptr=(int *) malloc(sizeof(int));
  printf("Enter a[%d]:  ", i);
  a[i]= *ptr;
  scanf("%d", &a[i]);

 }
 i=0;
 while(i<10)
 {printf("\n%u", &a[i++]);}
 free(ptr);
 system("pause");
}

int a[0]

doesn't have any space allocated to it (its 0 width) 没有分配任何空间(其宽度为0)

Yet you write to it on this line: 然而你在这一行写到:

a[0]=1;

You also assume it has 10 elements here: 你还假设它有10个元素:

while(i<10)
{printf("\n%u", &a[i++]);}
free(ptr);

Practically speaking, as this is just stack memory, you're just writing to another piece of the stack. 实际上,因为这只是堆栈内存,所以你只需要写入堆栈的另一部分。 You could, for example, be overwriting the value of ptr . 例如,你可以覆盖ptr的值。 Often this can go undetected until the stack is unwound and part of it is apparently corruptted. 通常这可能会被检测到,直到堆栈被解开并且其中一部分明显被破坏。 Here the stack is unwound right after system("pause") , when main returns. 在主系统返回时,堆栈在system("pause")之后立即展开。

(Also, if ptr is overwritten by your writes to a you can't be sure that free does anything reasonable.) (另外,如果ptr是由您写改写到a你无法确保free做任何事情合理的。)

To allocate a 10 integer array, use the syntax: 要分配10个整数数组,请使用以下语法:

int a[10];

Then you can use a[0] up to a[9]. 然后你可以使用[0]到[9]。 But this is C don't expect anything to protect you when you try to read/write to a[10]. 但是当你尝试读/写[10]时,这是C不要指望任何保护你的东西。

There are lots of problems in your code. 您的代码中存在许多问题。

  1. int a[0]. int a [0]。 You are defining an array of 0 elements. 您正在定义一个包含0个元素的数组。 In C this will not work. 在C中这不起作用。 You cannot do a[0]=1 since it will work only if a is an array of 1 or more elements. 你不能做[0] = 1,因为它只有在a是1个或更多元素的数组时才有效。
  2. It gets worse with a[i]=*ptr. a [i] = * ptr会变得更糟。 2 problems here, as pointed above a[1], a[2], etc don't exist. 这里有2个问题,如上所述a [1],a [2]等不存在。 and the second problem is you allocated ptr but assigning *ptr which might be having some garbage. 第二个问题是你分配了ptr但是分配了可能有一些垃圾的* ptr。

It is just the beginning. 这只是一个开始。

int a[0], ...;
...
a[0]=1;

You are writing out of bounds. 你正在写出界限。 This is undefined behavior. 这是未定义的行为。

First your a[0] should be a[10] for the way your are trying to use it. 首先你的a[0]应该是你尝试使用它的方式a[10] Second, just malloc to the pointer and it is basically an array. 其次,只是malloc到指针,它基本上是一个数组。 Though you don't need to do it in a for loop. 虽然你不需要在for循环中执行它。 You can just say ptr = malloc(10 * sizeof(int)); 你可以说ptr = malloc(10 * sizeof(int)); and you will be good. 你会很好的。 No need to cast it. 不需要施放它。 You can access it's elements through the array subscript like ptr[9] . 您可以通过数组下标访问它的元素,如ptr[9]

Well, your dealing with a is epical: 那么,你处理a是史诗般的:

int a[0];  // no-items array
...

a[0]=1;  // WHAT?
for(i=1;i<10;i++)
{
  ptr=(int *) malloc(sizeof(int));
  a[i]= *ptr; // a was int or pointer? And A[9]?
}

Several problems: 1) Your expecting your array to be 10 elements long but you're declaring it with 0, and make it an array of pointers; 几个问题:1)你期望你的数组长10个元素,但你用0表示它,并使它成为一个指针数组;

int *a[10];

then 然后

a[i] = (int*)malloc(sizeof(int));

and when freeing; 并在释放时;

free(a[i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM