Bus error troubleshooting

别等时光非礼了梦想. 提交于 2019-11-30 09:17:39

The bus error occurs because in many (if not most or all modern) C compilers, string literals are allocated in read-only memory.

You are reversing the string in place. In your first code snippet you are trying to write into a string literal. Not a good idea.

In the second case, you malloc'd a string which put it on the heap. It is now safe to reverse that string in place.

ADDENDUM

To the commenter who asked about segfaults versus bus errors, that is a great question. I have seen both. Here is a bus error on a mac:

$ cat bus.c 
char* s = "abc"; int main() {s[0]='d'; return 0;}

$ gcc --version bus.c && ./a.out
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Bus error

On other operating systems/compilers you may indeed get a segfault.

Copying it to the heap is one option. However, if you just want to allocate a local (stack) array, you can do:

char str[] = "I am a good boy";

Then, the constant string will be copied to the stack.

Character arrays specified in form of "I am a good boy" are usually constant - you can't modify them. That's why your first variant crashes. The second doesn't, as you make a copy of data and then modify it.

char *str = "I am a good boy"; Treated as literal and trying to modify it will result in bus error. It is equivalent to const char *str = "I am a good boy", i.e. pointer to a constant string and trying to modify a constant string is not allowed.

EDIT : The moment you malloc() and copy you are playing with a copy of the original string and ptr is not of 'const char *' type, instead it is 'char *ptr' and does not complain.

Compiling with c++ (g++) shows the deprecation of assigning a string literal to a non-const char* which is designed to prevent this error:

ed@bad-horse:~/udlit_debug$ g++ ../buserr.cpp 
../buserr.cpp: In function ‘int main()’:
../buserr.cpp:5:13: warning: deprecated conversion from string constant to ‘char*’
../buserr.cpp:7:61: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘size_t’

The relevant warning is on line 5.

Changing the declaration to const char * as indicated prevents the assignments into the literal string.

This is also a lesson on why you should not ignore warnings.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!