English 中文(简体)
Segmentation Fault With Char Array and Pointer in C on Linux
原标题:

So I have the following program:

int main(){
  char* one = "computer";
  char two[] = "another";
  two[1]= b ;
  one[1]= b ;
  return 0;
}

It segfaults on the line "one[1]= b " which makes sense because the memory that the pointer "one" points to must be in read only memory. However, the question is why doesn t the line "two[1]= b " segfault? Looking at the assembly output from gcc:

.file   "one.c"
        .section        .rodata
.LC0:
        .string "computer"
.LC1:
        .string "another"
        .text
.globl main
        .type   main, @function
main:

We see that both strings are in the rodata section so they are readonly. So then how come the line "two[1]= b does not segfault?

最佳回答

one points directly to the string located in a read-only page. On the other hand, two is an array allocated on the stack and is initialized with some constant data. At run time, the string in the read only section of the executable will be copied to the stack. What you are modifying is the copy of that string on the stack, not the read-only memory page.

At a higher level perspective, from the language point of view, "abcd" is an expression of type const char* and not char*. Thus, modifying the value pointed by such an expression results in undefined behavior. The statement char* one = "something"; merely stores the pointer to the string in a variable (unsafely, since it s casting away const modifier). The char two[] = "something"; is totally different. It s actually declaring an array and initializing it, much like int a[] = {1,2,3};. The string in quotes here is the initialization expression.

问题回答

The "another" you see in the rodata section is what will be copied in the array two when it will be initialized. On the other hand the address of the string "computer" will be assigned to one.

So, one is pointing to a read only segment (and hence the segfault on write) while two will be allocated on the stack and then "another" will be copied into it.

The second form creates an array by copying the literal string.

It is equivalent to:

char two[] = { a ,  n ,  o ,  t ,  h .  e , r ,   };

You can initialize a character array with variables, such as

char c =  a ;
char two[] = { a ,  n , c,   };




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签