c字符串 [英] c string
问题描述
如果我这样做
char * mystring =" mystring" ;;
稍后我重新分配给mystring这样
mystring =" replacewithnewstring" ;;
我不是专家,但我想我需要摆脱动态
分配了mystring?根据最佳实践应该做什么?
免费(mystring)?
tx
if i do this
char *mystring="mystring";
later i reassign to mystring like this
mystring="replacewithnewstring";
i''m not a c expert, but i suppose i need to get rid of the dynamically
assigned "mystring"? what should be done according to best practices ?
free(mystring) ?
tx
推荐答案
slurper写道:
slurper wrote:
如果我这样做
char * mystring =" mystring" ;;
后来我重新分配到这样的mystring
mystring =" replacewithnewstring" ;;
我不是专家,但我想我需要摆脱动态分配的mystring?
没有人。 C中的文字字符串是静态分配的。
常量。
根据最佳实践应该做什么?
没什么。
免费(mystring)?
if i do this
char *mystring="mystring";
later i reassign to mystring like this
mystring="replacewithnewstring";
i''m not a c expert, but i suppose i need to get rid of the dynamically
assigned "mystring"?
There isn''t one. Literal strings in C are statically allocated
constants.
what should be done according to best practices ?
Nothing.
free(mystring) ?
导致未定义的行为。如果你很幸运,
你的程序会崩溃。
如果你最终失去了最后一个参考
到一个字符串,该字符串可能会或可能不会动态分配,
你不知道哪个,回溯。
-
Chris" electric hedgehog" Dollin
That results in Undefined Behaviour. If you''re very lucky,
your program will crash.
If you end up in a situation where you''re losing the last reference
to a string, and that string may or may not be dynamically allocated,
and you don''t know which, backtrack.
--
Chris "electric hedgehog" Dollin
slurper< sl ********* @ hotmail.com>写道:
slurper <sl*********@hotmail.com> writes:
如果我这样做
char * mystring =" mystring" ;;
稍后我重新分配到像这样的mystring
mystring =" replacewithnewstring" ;;
我不是专家,但我想我需要摆脱动态
分配的mystring ;?根据最佳实践应该做些什么?
free(mystring)?
if i do this
char *mystring="mystring";
later i reassign to mystring like this
mystring="replacewithnewstring";
i''m not a c expert, but i suppose i need to get rid of the dynamically
assigned "mystring"? what should be done according to best practices ?
free(mystring) ?
AFAIK这是我的编译器完成的,只有你做了以下
东西:
char * mystring = malloc(9 * sizeof(char));
my_string =" mystring" ;;
你必须释放它,通过免费电话,我想。
种类重新定位,
Nicolas
-
| Nicolas Pavlidis | Elvis Presly:| \ | __ |
| SE& s的学生KM | 进入goto | \ | __ | |
| pa****@sbox.tugraz.at | ICQ#320057056 | |
| -------------------格拉茨技术大学---------------- |
AFAIK this is done my the compiler, onlye if you make the following
thing:
char *mystring = malloc(9 * sizeof(char));
my_string = "mystring";
you''d have to free it, over a call to free, I think.
Kind regrads,
Nicolas
--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Nicolas Pavlidis< pa **** @ sbox.tugraz.at>潦草地写道:
Nicolas Pavlidis <pa****@sbox.tugraz.at> scribbled the following:
slurper< sl ********* @ hotmail.com>写道:
slurper <sl*********@hotmail.com> writes:
如果我这样做
char * mystring =" mystring" ;;
稍后我会重新分配到像这样的mystring
mystring =" replacewithnewstring" ;;
我不是专家,但我想我需要摆脱动态
分配的mystring?应该根据最佳实践做什么?
free(mystring)?
if i do this
char *mystring="mystring";
later i reassign to mystring like this
mystring="replacewithnewstring";
i''m not a c expert, but i suppose i need to get rid of the dynamically
assigned "mystring"? what should be done according to best practices ?
free(mystring) ?
AFAIK这是我的编译器完成的,只有你做了以下的事情:
char * mystring = malloc(9 * sizeof(char));
my_string =" mystring" ;;
你必须释放它,通过免费电话,我想。
AFAIK this is done my the compiler, onlye if you make the following
thing:
char *mystring = malloc(9 * sizeof(char));
my_string = "mystring";
you''d have to free it, over a call to free, I think.
不,这是错误的。赋值my_string =" mystring" ;;确实* NOT *
写下字符串mystring进入malloc分配的内存()
调用。相反,它将指针my_string移动到指向字符串
" mystring" (无论它存储在何处),完全忽略由
malloc()分配的内存。
尝试调用free(my_string)将导致未定义的行为。
正是如果你试图调用free(mystring)。
此外,所有指向malloc()分配的内存的指针都已经消失了。 ,所以它已成为我称之为鬼记忆,即分配但无法使用的内存。
你必须查找strcpy()。
-
/ - Joona Palaste(pa*****@cc.helsinki.fi)--------- ----芬兰-------- \
\ ------------------------- -------------------------------规则! -------- /
"''所谓的''意味着'''这有很长的解释,但我没有
时间在这里解释一下。''"
- JIPsoft
No, this is wrong. The assignment my_string = "mystring"; does *NOT*
write the string "mystring" into the memory allocated by the malloc()
call. Rather, it moves the pointer my_string to point at the string
"mystring" (wherever it is stored), ignoring the memory allocated by
malloc() completely.
An attempt to call free(my_string) will cause undefined behaviour.
It is exactly if you attempted to call free("mystring").
Furthermore, all pointers to the memory allocated by malloc() have
gone, so it has become what I call "ghost memory", i.e. memory that is
allocated but impossible to use.
You have to look up strcpy() instead.
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"''So called'' means: ''There is a long explanation for this, but I have no
time to explain it here.''"
- JIPsoft
这篇关于c字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!