8
I.  char *str = "Stack";

II. char *str = (char*) malloc(6);
    strcpy(str, "Stack");

What are the differences between above two approaches?

Do they are same or something is different behind the curtains?

VishalDevgire
  • 4,232
  • 10
  • 33
  • 59

3 Answers3

30

The code above will cause problems.

The first instance is known as static string allocation and definition. For normal variables like int, etc, and non-string data types, such a declaration would allocate data on the stack. In the case of strings being initialized via string literals (ie: "stack"), it is allocated in a read-only portion of memory.

The string itself should not be modified, as it will be stored in a read-only portion of memory. The pointer itself can be changed to point to a new location.

ie:

char strGlobal[10] = "Global";

int main(void) {
  char* str = "Stack";
  char* st2 = "NewStack";
  str = str2;  // OK
  strcpy(str, str2); // Will crash
}

To be safe, you should actually allocate as a pointer to const data, ie:

const char* str = "Stack"; // Same effect as char* str, but the compiler
                           // now provides additional warnings against doing something dangerous

The second is known as dynamic allocation, which allocates memory on the heap, not the stack. The string can be modified without hassle. At some point, you need to free this dynamically allocated memory via the free() command.

There is a third means of allocating a string, which is static allocation on the stack. This allows you to modify the contents of the array which is holding the string, and it is statically allocated.

char str[] = "Stack";

In summary:

Example:                       Allocation Type:     Read/Write:    Storage Location:
================================================================================
const char* str = "Stack";     Static               Read-only      Code segment
char* str = "Stack";           Static               Read-only      Code segment
char* str = malloc(...);       Dynamic              Read-write     Heap
char str[] = "Stack";          Static               Read-write     Stack
char strGlobal[10] = "Global"; Static               Read-write     Data Segment (R/W)

You should also read up on how data is segmented for applications in modern operating systems. It will really increase your understanding of how your code is being built.

References


  1. Data Segment, Accessed 2013-04-15, <http://en.wikipedia.org/wiki/Data_segment>
  2. Code Segment, Accessed 2013-04-15, <http://en.wikipedia.org/wiki/Code_segment>
Cloud
  • 18,753
  • 15
  • 79
  • 153
  • @ValeriAtamaniouk And you would be correct. Will udpate. Thanks for reminding me. – Cloud Apr 16 '13 at 16:55
  • string literals (and const compound-literals) certainly need not be stored in read-only memory (if such exists). Writing them, as well as depending on them (not) being pooled just causes Undefined Behavior. Also, all automatic variables might be on the stack (like possibly auto-storage-class pointers to the before-mentioned literals, which are static-storage-class). – Deduplicator Sep 11 '14 at 04:14
  • @Deduplicator Simpler chips may not support RO memory, but string literals definition go in the `.text` segment, and modifying data there is undefined behavior, or worse (OS traps it). I agree, the pointers themselves are automatic (stack) allocated, but the data they point to isn't. – Cloud Sep 11 '14 at 12:41
2

In the first case, your pointer Points to a const char* which is allocated in the read only-part of the process-memory.
In the second case, you are allocating memory dynamically and eventuall copy the string "Stack" to your allocated memory.

You must eventually release the memory from II using free.

bash.d
  • 13,029
  • 3
  • 29
  • 42
0

Forget about other answers that claiming anything about storing inside stacks since they're incorrect. (Oooh now those answers have been removed..)

case I: you have a pointer str which points to a read-only memory region(the .rodata section) whose content is "Stack".

case II: you have a pointer str which points to a dynamically allocated region (in the heap) whose content is "Stack", which is modifiable and shall be freed by invocking free(str) after using it.

starrify
  • 14,307
  • 5
  • 33
  • 50