字符串指针数组对比 [英] Character String Pointer vs. Array
问题描述
我想知道是否有什么不同(在语法和性能方面)像
I was wondering if there is any difference (in terms of syntax and performance) between a string defined like
char str[200];
和
char *str;
str = calloc(200, sizeof(char));
是否有使用方面的差异? (防爆,一个就是不要跟函数strncpy
或兼容的东西),更重要的同时,有没有在性能方面的差异?
Are there differences in terms of usage? (for ex, one is not compatible with strncpy
or something) And more importantly, are there differences in terms of performance?
编辑:据我了解,由的char *定义了一个数组
和释放calloc
CAN增长和收缩,但我应该选择堆内存超过堆栈存储器或因故其他方式?这就是我真的想问问。
I understand that an array defined by char *
and calloc
can grow and shrink, but should I pick heap memory over stack memory or the other way around for any reason? That's what I was really trying to ask.
推荐答案
字符海峡[200]
在栈内存中分配,其中作为释放calloc()
分配在堆内存。
char str[200]
allocated in stack memory where as calloc()
allocates in heap memory.
,由calloc()的性质,它分配0到它分配的所有字节。
By nature of calloc(), it assigns 0 to all the bytes allocated by it.
请参照栈和堆比较以下
<一个href=\"http://stackoverflow.com/questions/161053/c-which-is-faster-stack-allocation-or-heap-allocation\">C++这是更快:堆栈分配或堆分配
的http://www.linuxquestions.org/questions/programming-9/stack-faster-than-heap-685004/
这篇关于字符串指针数组对比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!