+ 1

Does casting a string as char pointer automatically allocate required memory in C/C++?

So while playing with codes I discovered a new technique of assigning pointer values without allocating memory previously. #################### #include <stdio.h> int main(){ char* name; name =(char*) "String Sample"; return 0; } ####################### The code above works well without explicitly allocating required memory (using mallocs or new keywords) and neither does it throw any error or warning... So does this mean that the (char*) casting of the string automatically allocate the required memory space for the string? And is this method safe to use and recommended for a bigger scale application? This method can be useful when the actual size for allocating a string isn't known... Thanks.... Edit: Fixed a small mistake of casting the lvalue instead the string right hand side.

16th Jul 2022, 5:11 PM
CoderBittu
CoderBittu - avatar
12 odpowiedzi
+ 3
This is a standard technique. You are assigning name to point to "String Sample", which is a constant string literal. It has a type, which is already (char *), so there is no need to explicitly cast it. Since the string is a const, you may not alter any characters. So, for instance *name = 's'; would fail. But it is perfectly valid to read any element of the string (e.g., putc(name[7]);) or print the whole string (puts(name);) without causing memory violation. Its memory is preallocated and loaded as the program loads into memory. You may also freely reassign name to point to another string as needed.
16th Jul 2022, 10:58 PM
Brian
Brian - avatar
+ 1
Brian I have just noticed I misuderstood the 4th line: name = (char*) "String Sample"; I read this as writing the string to the pointer (arbitrary) address. In fact, it writes the string address to the pointer, effectively initializing it. So the memory range is used by the string itself, and the code is safe. Thanks for calling my attention on that. [B.S.] BITTU , pls see this correction.
17th Jul 2022, 6:23 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Emerson Prado thank you for clearing that up. ✌️ I was getting confused by your responses! 💁‍♂️
17th Jul 2022, 7:05 PM
Brian
Brian - avatar
0
Emerson Prado How does it successfully write when the pointer literally points toward nothing? It should have given a warning, shouldn't have it?
16th Jul 2022, 5:49 PM
CoderBittu
CoderBittu - avatar
0
[B.S.] BITTU A pointer always points somewhere. It's a variable, so it always has a value. The real issue is that this value is unknown and points to an arbitrary address that can be in use by something else. If your process can write there, it will. Then ruin this something else.
16th Jul 2022, 5:57 PM
Emerson Prado
Emerson Prado - avatar
0
Brian In fact, the question mentions writing the addresses. What happens if this write works, and the addresses are in use?
17th Jul 2022, 1:15 AM
Emerson Prado
Emerson Prado - avatar
0
Emerson Prado did I miss part of the question? I don't see where the OP asks about writing to an uninitialized pointer address.
17th Jul 2022, 4:40 AM
Brian
Brian - avatar
0
Emerson Prado I am getting a bit confused. So let's jump to the conclusion directly , this code is safe then alright? And I hope this code will not interfere somewhere else by creating bugs in my code which is gonna be large application which includes many complex techniques. and last question... can I use free() in this non explicitly allocated block?
18th Jul 2022, 4:41 AM
CoderBittu
CoderBittu - avatar
0
[B.S.] BITTU Yes, it's safe, won't create bugs. I tried in Code Playground, and free complained on "Invalid pointer". I don't know if it depends on implementation. Maybe we could try in a couple other compilers.
19th Jul 2022, 1:40 AM
Emerson Prado
Emerson Prado - avatar
0
Well, tried in tio.run and gcc 8.3.0, with the same results
19th Jul 2022, 1:47 AM
Emerson Prado
Emerson Prado - avatar
0
Emerson Prado hm... Okay 👍
19th Jul 2022, 2:26 AM
CoderBittu
CoderBittu - avatar
- 1
Not sure, but probably this does not allocate any memory - just writes wherever the pointer points to, which is highly unsafe.
16th Jul 2022, 5:46 PM
Emerson Prado
Emerson Prado - avatar