为什么一定要指向一个字符数组需要STRCPY指定字符其数组和双引号的分配将无法正常工作? [英] Why must a pointer to a char array need strcpy to assign characters to its array and double quotes assignment will not work?

查看:122
本文介绍了为什么一定要指向一个字符数组需要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:

调试断言失败前pression:_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.)

后来你传递删除[] (和的不是删除,既然你叫新[] ,而不是 )。这样,你通过它的字符串字面的地址,而不是分配的字符数组。这将,当然,搞砸了堆管理器。 (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 ::字符串,而不是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.

这篇关于为什么一定要指向一个字符数组需要STRCPY指定字符其数组和双引号的分配将无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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