0

there are many similar questions regarding this Topic, but they do not answer the following question:

Taking a swing

I am going to take a swing, if you want go straight to the question in the next heading. Please correct me if I make any wrong assumptions here.

Lets assume, I have this string declaration

char* cpHelloWorld = "Hello World!";

I understand the Compiler will make a char* to an anonymous Array stored somewhere in the Memory (by the way: where is it stored?).

If I have this declaration

char cHelloWorld[] = "Hello World!";

There will be no anonymous Array, as the Compiler will create the Array cHelloWorld right away.

The first difference between these two variables is that I can change cpHelloWorld, whereas the Array cHelloWorld is read-only, and I would have to redeclare it if I want to Change it.

My question is following

cpHelloWorld = "This is a pretty long Phrase, compared to the Hello World phrase above.";

How does my application allocate at runtime a new, bigger (anonymous) Array at runtime? Should I use this approach with the pointer, as it seems easier to use or are there any cons? On paper, I would have used malloc, if I had to work with dynamic Arrays.

My guess is that the Compiler (or runtime Environment?) creates a new anonymous Array every time I change the Content of my Array.

4
  • I hope this may answer your questions: stackoverflow.com/questions/12393888/… Commented Jan 6, 2016 at 8:46
  • @Bechir I don't understand why I can Change the char*, if it is in read-only Memory. Does it reallocate it or what happens? Commented Jan 6, 2016 at 8:54
  • char* is not a string. C does not have a string type anyway. And there are good reasons a pointer is not called "array" and vice versa. Commented Jan 6, 2016 at 9:20
  • @Olaf Thanks for the Explanation. Commented Jan 6, 2016 at 9:21

3 Answers 3

3
char* cpHelloWorld = "Hello World!";

is a String Literal stored in read-only memory. You cannot modify the contents of this string.

char cHelloWorld[] = "Hello World!";

is an array of char initialized to "Hello World!\0". (note: where the brackets are placed)

The amount of memory allocated at run-time by the compiler is set by the initialization "This is a pretty long ... phrase above."; The compiler will initialize the literal allowing 1 char for each char in the initialization string +1 for the required nul-terminating character.

Whether you use a statically declared array (e.g. char my_str[ ] = "stuff";) or you seek to dynamically allocate storage for the characters, largely depends on whether you know what, and how much, of whatever you wish to store. Obviously, if you know beforehand what the string is, using a string literal or an initialized array of type char is a simple way to go.

However, when you do NOT know what will be stored, or how much, then declaring a pointer to char (e.g. char *my_string; and then once you have the data to store, you can allocate storage for my_string (e.g. my_string = malloc (len * sizeof *my_string); (of course sizeof *my_string will be 1 for character arrays, so that can be omitted) (note: parenthesis are required with sizeof (explicit type), e.g. sizeof (int), but are optional when used with a variable)

Then simply free whatever you have allocated when the values are no longer needed.

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

9 Comments

So the Compiler looks for the biggest string literal known at compile-time and allocates Memory for it? Because I can do cpHelloWorld = "Very Very Long Phrase"; and it will Change the Content.
@ElMac See your string is constant , that is you cannot di this cpHelloWorld[0]='a'; , but _your pointer is not constant , so changing what pointer points to is valid . Therefore , cpHelloWorld = "Very Very Long Phrase"; , does not change the content of string , but it change to what pointer cpHelloWorld points to .
@ElMac In first declaration you showed , what actually happening is , a char array is initialized with that string literal in read only memory , and the pointer cpHelloWorld just points to the first element of that array . Memory is not allocated to pointer (therefore , also no need to free it).
That is a good point -- you can only free what you have allocated with malloc or calloc, etc. Some functions will also allocate storage for you. It will be plainly apparent from the man page for that function if that is the case. Otherwise, you do NOT free what you did not allocate.
Using sizeof like dynobj = malloc(sizeof(*dynobj)) is actually a good thing, as that will always use the correct size and avoids redundancy. Nonsense is to use sizeof(char), i.e. using the char type directly. This 1) is error-prone when changing the base-type of the pointer (this is true for any other type, too) 2) Superfluous for the reason you mentioned (sizeof(char) is defined by the C standard to be 1).
|
0

As a matter of fact all strings known to the compiler at compile-time are allocated in the data segment of the program. The pointer itself is located on the stack.

There is no memory allocation at run-time, so it is nothing like malloc. There are no performance drawbacks here.

Comments

0

Each of the constant "anonymous" strings used in these contexts exists at its fixed address. The only dynamic part is the actual pointer assignment. You should get the same string address each time you execute a specific pointer assignment from a specific anonymous string (each string has its own address).

3 Comments

So all the strings I declared are in the Read-Only-Memory because they are constants and the char* just changes the Destination address to the other string in the ROM?
Essentially, yes. The strings may or may not be in protected memory, but you should always treat them as if they are.
Even if they are not in protected memory, they might be in overlapping memory. For example, two identical string literals may share the same memory, or two string literals where one string literal is the tail end of another string literal (e.g. "there" and "here") may share the same memory.

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.