-1

Which is the significance of doing the dynamic allocation, as the same is achieved by the static allocation?

#include <iostream>
#include <cstring>

const char* Allocator()
{
    const char* ptr = "is visible";
    return ptr;
}

const char* Allocator1()
{
    const char* ptr = new char[strlen("is visible") + 1];
    ptr = "is visible";
    return ptr;
}

int main()
{
    std::cout << Allocator() << std::endl;
    // here later memory deallocation required i.e Okay 
    std::cout << Allocator1() << std::endl;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
USER
  • 11
  • 3
  • `ptr = "is visible";` is not doing what you think it does. – 463035818_is_not_an_ai Jul 07 '23 at 07:09
  • `ptr = "is visible"` overwrites the pointer, leaking whatever was returned by `new`. You should be using `strcpy` on this line. *"Which is the significance"* In this case, none. One difference is that manually allocated string can be modified, while a string literal can't. But in real code you wouldn't call `new` manually anyway, and would use `std::string`. – HolyBlackCat Jul 07 '23 at 07:14
  • please do not change your question to ask for something else after you received answers. I tried my best to write an answer and to explain what your code does. Now changing the code so it does something else entirely is not nice. If you didnt ask what you watned to ask you can open a new question – 463035818_is_not_an_ai Jul 07 '23 at 07:47
  • And this is why we have things like std::string, const std::string_view, std::make_unique and containers to avoid all kind of allocation bugs. (you should hardly have to use new/delete in current C++ anymore, unless you are writing datastructures from scratch). – Pepijn Kramer Jul 07 '23 at 07:58

1 Answers1

2

Here:

const char* ptr = new char[strlen("is visible") + 1];
ptr = "is visible";

The second line assigns a pointer to the string literal "is visible" to ptr. The pointer to the dynamically allocated char array you allocated in the first line is lost. Its a memory leak. Because of this bug, the two versions really do the same apart from the memory leak. They return a pointer to a string literal.


Note that string literals have static storage duration (cf Is a string literal in С++ created in static memory?), hence the usual issues of returning a pointer to a function local object does not apply. However, both of your function are rather useless. They do not, as their name suggests, allocate memory that you can then use to store other stuff.


// here later memory deallocation required i.e Okay 

Yes it would be required to deallocate the memory allocated via new. Though, it is not Okay, because you do not have the pointer to that memory anymore.


ptr = "is visible";

This copies a pointer. If you want to copy the string literal into the allocated char array you must copy the chars (eg strcpy). Arrays cannot be assigned like that. Actually ptr is not an array. ptr is just a pointer. Don't confuse arrays with pointers. new char[strlen("is visible") + 1] allocates an array and returns a pointer to its first element.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    *"should not return a pointer to a funciton local object. Never."* The wording is a bit confusing here. A string literal is not a function-local object. – HolyBlackCat Jul 07 '23 at 07:30