简体   繁体   中英

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];

and

char *str;
str = calloc(200, sizeof(char));

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?

EDIT: 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.

char str[200] allocated in stack memory where as calloc() allocates in heap memory.

By nature of calloc(), it assigns 0 to all the bytes allocated by it.

Pls refer the following for stack and heap comparison

Which is faster: Stack allocation or Heap allocation

http://www.linuxquestions.org/questions/programming-9/stack-faster-than-heap-685004/

What and where are the stack and heap?

Are there differences in terms of usage? (for ex, one is not compatible with strncpy or something)

I am surprised that no one mentioned that first str ie array name evaluates to a constant pointer and cannot be reassigned , where as the second one is a pointer variable which can be reassigned.

So

char str[SIZE];
char * b = malloc(SIZE);

str = b; // This is a compilation error

b = str; // where as this is perfectly legal (ignoring the fact 
         // that we are losing malloced memory without actually freeing it)

First, allocates memory on stack, while second allocates dynamic memory. stack memory is auto managed while dynamic memory needs manual management.

When you have a choice, You should always prefer the first:

  • You don't have to remember to free anything
  • dynamic memory has little overhead in terms of performance.

Are there differences in terms of usage? (for ex, one is not compatible with strncpy or something)

In terms of usage with functions both are similar, in simple sense while using with functions both are pointers which point to a consecutive blocks of memory. When you pass a array to function, it decays as pointer to first element.
Difference is where they are stored and whether they are auto managed or manually managed.

char str[200];

  • Is a simple declaration (not dynamic allocation),
  • By default values are garbage,
  • Fast in access (in stack segment) ,
  • Scope is local (with in {}).

char *str;
str = calloc(200, sizeof(char));

  • All elements values are zero (because calloc() ),
  • Slow to access (heap segment use),
  • dynamic allocation use that is efficient use of memory.
  • Need to de-allocate explicitly,
  • Memory Scope is global , you can return eg return str from a function`.

You should use stack allocation whenever possible: it is easier to maintain for the programmer and it is also less demanding performance-wise.

There are many cases where array stack allocation is not possible.

  • If you do not know at compile time the size of your array. However recent C standard has some support for this,
  • you have an array of arrays whose size are not all the same,
  • you need the allocated memory to persist after your function has returned.

Also note that char str[200] “knows” its size (ie sizeof(str) == 200*sizeof(char) ) whereas you'll have to memorise the size of the allocated array in a auxiliary variable to work with it ( sizeof(str) == sizeof(char*) , typically 4 or 8).

Char array memory is allocated on stack . as soon as control moves out of function containing the array , the memory is deallocated and array can't be accessed now.

While calloc function allocates memory on heap and remains till program is in in execution or memory is deallocated manually

Once you create the string, there is no difference in usage.

char str[100] allocates the string on stack, while the other approach uses heap. Allocations on stack are always faster than on heap (see this discussion: Which is faster: Stack allocation or Heap allocation )

Additionally, calloc() sets all elements of an array to 0/NULL, lowering the performance even further. If you program in C++ and need to use heap, always write:

char *str = new char[200];

This has additional benefits, eg raising an error if the heap is full.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM