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.

1

7 Answers 7

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?

Sign up to request clarification or add additional context in comments.

3 Comments

I understand that... but is heap memory always going to be slower?
@Kitchi: Yes, Heap memory is going to be slower and you are more likely to be error prone while using manual dynamic memory management.
@AlokSave, I agree. Stack is faster than heap memory access.
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)

1 Comment

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 **
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.

7 Comments

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: Ofcourse, it is horses for courses. So obviously, my comment talks when you can use either.Anyways i will edit for clarity.
What calls for the downvote? If it is any technical reasoning, Please enlighten me.
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.
@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.
|
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`.

8 Comments

I may not have downvoted, but a static declaration is zeroed out by default (not garbage). Allocated heap memory is garbage. And you could have spent an extra minute formatting your answer into something more readable.
static declaration means compile time not dynamic ..I know there is key work 'static'
static declaration means not static variable.
To a c programmer, a static declaration means a variable declared in static storage. That's just the lingo, and that's why you were misunderstood.
Allocation from heap are garbage but if you allocate using calloc it would be 0 (Zero) only.
|
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).

Comments

-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

Comments

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

2 Comments

There still is one difference in terms of usage i.e. array name cannot be reassigned where as pointer variable can be.
I was thinking more in terms of function arguments, but yes, thanks for pointing this out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.