为什么同时使用堆分配的内存我的程序抛出一个分割的错吗? [英] Why does my program throw a segmentation fault while using heap-allocated memory?

查看:247
本文介绍了为什么同时使用堆分配的内存我的程序抛出一个分割的错吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序来扭转一个字符串后,我无法理解为什么我得到了赛格故障而试图扭转字符串。我在下面列出我的程序。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;void反转(字符*);诠释主(){
  字符*海峡=释放calloc(1,'\\ 0');
  的strcpy(STRmystring0123456789);
  反转(STR);
  的printf(反向字符串是:%S \\ n,STR);
  返回0;
}void反转(字符*字符串){
  焦炭CH,*开始,*结束;
  INT C = 0;
  INT长度= strlen的(字符串);
  开始=字符串;
  结束=字符串;  而(C<长度-1){
    结束++;
    C ++;
  }
  C = 0;  而(℃下长度/ 2){
    CH = *结束;
    *结束= *启动;
    *开始= CH;
    启动++;
    结束 - ;
    C ++;
  }
}

第一个问题:

尽管我已经拨出只有1内存字节的字符指针
STR(释放calloc(1,'\\ 0')),和我复制一个18字节的字符串 mystring0123456789 进去,并没有抛出任何错误,程序没有任何段错误的罚款。

为什么我的程序没有抛出一个错误?理想情况下,应该抛出一些错误,因为它没有任何的内存空间来存储大串。可有人扔光这个?

该方案完美跑去给我输出反向字符串为:9876543210gnirtsym

第二个问题:

如果替换语句

 的strcpy(STRmystring0123456789);

 海峡=mystring0123456789 \\ 0;

程序给出分段错误即使我已经分配了足够的内存STR(的malloc(100))。

为什么程序扔段错误?


解决方案

  

尽管我只分配了1的内存字节的字符指针STR(释放calloc(1,'\\ 0')),和我复制一个18字节的字符串mystring0123456789进去,并没有抛出任何错误,程序没有任何段错误的罚款。


您code有一个bug - 当然它不会做你期望的。修复bug和神秘感就会消失。


  

如果替换的声明结果
  的strcpy(STRmystring0123456789);结果
  与结果
  海峡=mystring0123456789 \\ 0;结果
  程序给出分段错误,即使我已经分配了足够的内存STR(的malloc(100))。


由于当你完成这一点, STR 指向一个常数。这种丢弃 STR 的previous值,指向内存的指针你分配,并用指针,不断替换它。

您不能修改一个常量,这是什么使得它一个常数。在的strcpy 函数的拷贝的常量到一个变量,然后可以修改。

试想一下,如果你能做到这一点:

为int * H =和2;

现在,如果你做了 * H = 1; 你会试图改变这种状况的的2到code,这当然你不能这样做。

这是有效的你与海峡=mystring0123456789 \\ 0做什么; 。它使 STR 点在源$ C ​​$ C是常数,当然,你不能修改。

After writing a program to reverse a string, I am having trouble understanding why I got a seg fault while trying to reverse the string. I have listed my program below.

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

void reverse(char *);

int main() {
  char *str = calloc(1,'\0');
  strcpy(str,"mystring0123456789");
  reverse(str);
  printf("Reverse String is: %s\n",str);
  return 0;
}

void reverse(char *string) {
  char ch, *start, *end;
  int c=0;
  int length = strlen(string);
  start = string;
  end = string;

  while (c < length-1){
    end++;
    c++;
  }
  c=0;

  while(c < length/2){
    ch = *end;
    *end = *start;
    *start = ch;
    start++;
    end--;
    c++;
  }
}

1st Question:

Even though I have allocated only 1 byte of memory to the char pointer str (calloc(1,'\0')), and I copied a 18 bytes string mystring0123456789 into it, and it didn't throw any error and the program worked fine without any SEGFAULT.

Why did my program not throw an error? Ideally it should throw some error as it don't have any memory to store that big string. Can someone throw light on this?

The program ran perfectly and gives me output Reverse String is: 9876543210gnirtsym.

2nd Question:

If the replace the statement

strcpy(str,"mystring0123456789");

with

str="mystring0123456789\0";

the program gives segmentation fault even though I have allocated enough memory for str (malloc(100)).

Why the program throwing segmentation fault?

解决方案

Even though i have allocated only 1 byte of memory to the char pointer str(calloc(1,'\0')), and i copied a 18 bytes string "mystring0123456789" into it, and it didn't throw any error and the program worked fine without any SEGFAULT.

Your code had a bug -- of course it's not going to do what you expect. Fix the bug and the mystery will go away.

If the replace the statement
strcpy(str,"mystring0123456789");
with
str="mystring0123456789\0";
the program gives segmentation fault even though i have allocated enough memory for str (malloc(100)).

Because when you finish this, str points to a constant. This throws away the previous value of str, a pointer to memory you allocated, and replaces it with a pointer to that constant.

You cannot modify a constant, that's what makes it a constant. The strcpy function copies the constant into a variable which you can then modify.

Imagine if you could do this:

int* h = &2;

Now, if you did *h = 1; you'd be trying to change that constant 2 in your code, which of course you can't do.

That's effectively what you're doing with str="mystring0123456789\0";. It makes str point to that constant in your source code which, of course, you can't modify.

这篇关于为什么同时使用堆分配的内存我的程序抛出一个分割的错吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆