简体   繁体   English

数组和指针:分段错误

[英]Arrays and pointer : segmentation fault

I am playing with array and pointer and got this segmentation fault. 我在玩数组和指针,并得到此分段错误。 Can any one explain why I have been getting this segmentation fault in this code when I move my pointer "p" below "ptr" pointer in the code and when I comment out one of printf statement alternatively it disappears: 当我将指针“ p”移动到代码中“ ptr”指针下方时,并且当我注释掉一个printf语句时,它消失了,谁能解释为什么在此代码中出现这种段错误?

 typedef struct str{
   char* ptr;
  }str_t;

copy(str_t t){
   char a[12];
   char *p;   //  <------ no error when move below ptr pointer 
   char *ptr;

   printf("t= %s p = %d ptr = %d\n", t, p, ptr);

   strcpy(a, t.ptr);
   printf("a = %s %u\n", a, &a);

   strcpy(ptr, t.ptr);
   printf("ptr = %s %u\n", ptr, &ptr); //<--- comment it error disappears

   p= t.ptr;
   printf("p = %s %u",p, &p);  //<--- comment it error disappears
 }

int main ()
{
  str_t t;
  char app[] = "hello";
  char ap[] ="world";

  t.ptr = ap;
  copy(t);

  printf("%s\n", app);

  return 0;
}

you can compile the code here to see the result : http://codepad.org/Q7zS8NaC 您可以在此处编译代码以查看结果: http : //codepad.org/Q7zS8NaC

Thank you , for visiting this question . 谢谢您访问这个问题。

strcpy doesn't allocate space at the pointer, p , to store the string. strcpy不会在指针p处分配空间来存储字符串。 You need to declare it as an array or allocate space with malloc or calloc . 您需要将其声明为数组或使用malloccalloc分配空间。

Try this: 尝试这个:

 int len = strlen (t.ptr);         // find length of string

 char * ptr = calloc (len + 1, 1); // allocate space for ptr
 if (!ptr) return;                 // error check calloc
 strcpy (ptr, t.ptr);              // copy the string

 char * p = calloc (len + 1, 1);   // do the same thing for p
 if (!p) return;
 strcpy (p, t.ptr);

That will fix your segmentation fault. 这样可以解决您的细分错误。

You have a couple of more errors though, which are mainly format issues. 不过,您还有其他几个错误,主要是格式问题。

  1. %u prints an unsigned integer. %u输出无符号整数。 It looks like you're trying to print a pointer, so use %p instead. 看起来您正在尝试打印指针,因此请改用%p
  2. printf("t= %sp = %d ptr = %d\\n", t, p, ptr); is totally wrong. 是完全错误的。
    1. You need to reference the member of t , which is t.ptr 您需要引用t的成员,即t.ptr
    2. p is a pointer, not an integer. p是一个指针,而不是整数。 Use %p instead of %d 使用%p代替%d
    3. ptr is also a pointer. ptr也是一个指针。 Use either %p or %s 使用%p%s

Read the documentation of printf if you're ever unsure about formatting. 如果您不确定格式,请阅读printf文档 In fact, read the documentation of any function, if you are unsure how to use it - you'll save yourself a lot of headaches. 实际上,如果不确定如何使用它,请阅读任何功能的文档-这样可以避免很多麻烦。

Your code has several undefined behaviors: 您的代码具有几种未定义的行为:

  • The first printf prints a pointer using %d specifier 第一个printf使用%d说明符打印一个指针
  • The second call of strcpy call attempts to write to memory pointed to by an uninitialized pointer strcpy调用的第二次调用尝试写入未初始化指针指向的内存
  • The second and third calls of printf passes a pointer to an array to a format specifier %u printf的第二次和第三次调用将指向数组的指针传递给格式说明符%u

Removing one of the pointers makes the code not crash, but since undefined behavior is there, the code does not work correctly, and may crash at any time. 删除其中一个指针不会使代码崩溃,但是由于存在未定义的行为,因此代码无法正常工作,并且随时可能崩溃。

Here is one way of fixing it: 这是修复它的一种方法:

char a[12];
char *p;
char *ptr;

printf("t= %s p = %x ptr = %x\n", t.ptr, (void*)p, (void*)ptr);

strcpy(a, t.ptr);
printf("a = %s %x\n", a, (void*)(&a[0]));
ptr = malloc(strlen(t.ptr)+1);
// In production, check ptr for NULL
strcpy(ptr, t.ptr);
printf("ptr = %s %x\n", ptr, (void*)&ptr);

p= t.ptr;
printf("p = %s %x", p, (void*)&p);
// Release the memory when you are done
free(ptr);

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

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