为什么必须一个指向char数组的指针需要strcpy来为其数组赋值字符和双引号赋值不起作用? [英] Why must a pointer to a char array need strcpy to assign characters to its array and double quotes assignment will not work?

查看:268
本文介绍了为什么必须一个指向char数组的指针需要strcpy来为其数组赋值字符和双引号赋值不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你去删除指针时,第一个例子不工作。该程序在我添加空终止符或没有它时获得:

The first example does not work when you go to delete the pointer. The program either hangs when I add the null terminator or without it I get:

调试断言失败的表达式:_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse) 来自Visual Studio 2008

Debug Assertion Failed Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) from Visual Studio 2008

//Won't work when deleting pointer:
    char *at = new char [3];
    at = "tw"; //   <-- not sure what's going on here that strcpy does differently
    at[2] = '\0'; // <-- causes program to hang
    delete at;

//Works fine when deleting pointer:
    char *at = new char [3];
    strcpy(at,"t");
    at[1] = 'w';
    at[2] = '\0';
    delete at;

那么当我使用双引号而不是strcpy时会发生什么?

So what's going on when I use double quotes instead of strcpy? Both of them will cout the string perfectly and debugger does not show anything different.

推荐答案

因为 char * 不是一个字符串。它只是一个指向某个字符的指针,按照约定,可能有更多的字符跟随,最后一个后面有一个'\0'

Because a char* isn't a string. It's just a pointer to some character, with the convention that there might be more characters to follow and that after the last one there is a '\0'.

一个字符串在C(因此在C ++中)像abc只是一个字符数组,编译器默认添加一个'\0'。当您将数组分配给指针时,数组将静默地将指针转换为第一个元素。结果是

A string literal in C (and thus in C++) like "abc" is just an array of characters, with the compiler silently adding a '\0'. When you assign an array to a pointer, the array silently converts a pointer to the first element. The result is that

at = "tw";

意味着处的指针字符串文字中的第一个字符的地址tw。这样,它将失去它的旧价值。由于这是一个动态分配的字符数组的地址,你泄漏这个数组。

means, the pointer at is assigned the address of the first character in the string literal "tw". By this, it will lose its old value. Since this was the address of a dynamically allocated character array, you are leaking this array.

当您稍后分配给在现在指向的数组中的字符时,字符串中的一些字符。这是调用未定义的行为,程序挂或立即崩溃可能是最好的,当你这样做可能发生。 (在许多平台上,您正在写入只读内存。)

When you later assign to a character in the array at now points to, you are assigning a new value to some character in the string literal. That's invoking undefined behavior and the program hanging or crashing immediately is probably the best that could happen to you when you do this. (On many platforms you're writing to read-only memory doing so.)

稍后,您将传递到< delete [] (和不要删除,因为你调用 new [] ,而不是 new )。这样做,你传递的字符串文字的地址,而不是分配的字符数组。这当然会弄乱堆管理器。 (VC的运行时库在调试模式下捕获它。)

Later you pass at to delete[] (and not delete, since you called new[], not new). In doing so, you pass it the address of the string literal, instead of the allocated character array. This will, of course, mess up the heap manager. (VC's runtime library catches this in Debug mode.)

std :: strcpy 一个字符串,一个字符从一个数组到另一个数组。没有指针将被改变,只有内存被复制。指向目标数组的指针仍然指向目标数组,只有该数组中的数据已经改变。

std::strcpy, on the other hand, copies a string character by character from one array to another array. No pointers will be changed, only pieces of memory are copied. The pointer to the target array still points to the target array afterwards, only the data in that array has changed.

让我补充一下: 作为C ++的初学者,你应该使用 std :: string ,而不是C字符串。这是对你的所有肮脏的工作,并具有固执的语义。

Let me add this: As a beginner in C++, you should use std::string, rather than C strings. That does all the dirty work for you and has sane semantics.

这篇关于为什么必须一个指向char数组的指针需要strcpy来为其数组赋值字符和双引号赋值不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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