2

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.

Kitchi
  • 1,724
  • 4
  • 25
  • 46
  • possible duplicate of [C++ Which is faster: Stack allocation or Heap allocation](http://stackoverflow.com/questions/161053/c-which-is-faster-stack-allocation-or-heap-allocation) – WhozCraig Dec 12 '12 at 15:31

7 Answers7

4

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?

Community
  • 1
  • 1
Jeyaram
  • 8,315
  • 5
  • 35
  • 59
3

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 i.e. 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)
fkl
  • 5,068
  • 3
  • 24
  • 66
  • 2
    There are also lots of other differences: e.g. `sizeof(str)` returns `SIZE * sizeof(char)`; whereas `sizeof(b)` returns `sizeof(char *)`; `&str` has type `char (*)[SIZE]` whereas `&b` has type `char **` – newacct Dec 12 '12 at 20:59
1

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.

Alok Save
  • 190,255
  • 43
  • 403
  • 518
  • 3
    _You should always prefer the first_ - May I respectfully disagree? If stack size is small and variable is big or there are many such variables, heap may be a better choice. – mouviciel Dec 12 '12 at 09:32
  • @mouviciel: Ofcourse, it is horses for courses. So obviously, my comment talks when you can use either.Anyways i will edit for clarity. – Alok Save Dec 12 '12 at 09:34
  • What calls for the downvote? If it is any technical reasoning, Please enlighten me. – Alok Save Dec 12 '12 at 09:37
  • I did not downvote, but usage might differ in terms that array name evaluates to a constant pointer and cannot be reassigned where as a pointer variable can be - as long as we do not leak memory. – fkl Dec 12 '12 at 09:46
  • @mouviciel - `If stack size is small and variable is big...` - Could you explain that? From other threads I've read, I understand that stack allocation and deallocation is _always_ faster since it does not have to keep a track of contiguous memory segments. – Kitchi Dec 12 '12 at 09:55
  • @Kitchi: There can be envrionments where the stack size is limited, for ex: embedded platforms. If you need huge allocations the stack cannot accomodate what you need and the only option you have is dynamic allocations. – Alok Save Dec 12 '12 at 09:57
  • Oh, yes. Okay... I misunderstood what @mouviciel was trying to say. Thanks. – Kitchi Dec 12 '12 at 10:02
1

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 e.g. return str from a function`.
Grijesh Chauhan
  • 52,958
  • 19
  • 127
  • 190
0

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 (i.e. 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).

kmkaplan
  • 17,709
  • 4
  • 48
  • 60
-1

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

Mudassir Hasan
  • 26,105
  • 18
  • 90
  • 124
-1

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, e.g. raising an error if the heap is full.

Community
  • 1
  • 1
gcvt
  • 1,442
  • 13
  • 14
  • 2
    There still is one difference in terms of usage i.e. array name cannot be reassigned where as pointer variable can be. – fkl Dec 12 '12 at 09:47
  • I was thinking more in terms of function arguments, but yes, thanks for pointing this out. – gcvt Dec 12 '12 at 09:58