6

Is there a way to convert dynamically allocated memory to static memory OR copy...

 2 years ago
source link: https://dev.to/baenencalin/is-there-a-way-to-convert-dynamically-allocated-memory-to-static-memory-or-copy-ptr-contents-in-cc-127d
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Calin Baenen

Posted on Nov 14

Is there a way to convert dynamically allocated memory to static memory OR copy ptr contents in C/C++?

For reference, static memory is memory that is memory that is assigned, managed, and freed for you (an example is char const* str = "Hello World!";).
C/C++ will clean that string up for you.

Dynamically allocated memory is memory you request, manage, and must clean yourself (an example is Type_t* array = malloc( sizeof(Type_t)*arrLength );).

I'm messing around with C strings in C++, even testing ideas for my own String type. - And I was creating a String +(char c) method, when I thought of something: "How can I return a String object whose str property is statically allocated, and not dynamically allocated?".
Since the only method of adding a character to an immutable string I can think of involves using malloc( (oldStrLen+1)*sizeof(char) ), which is dynamically allocated.

So, is there a way I can copy the contents of a pointer to a new statically allocated pointer, or convert dynamically allocated memory to statically allocated memory in C/C++?

Thanks!
Cheers!

Discussion (2)

pic

CollapseExpand

Static memory is not automatically cleaned up for you. It is globally shared memory and you will still need to clean it up, unless you mean at app exit, at which point more or less everything is cleaned up.

char const* my string = "";
Is not static, use std::string instead, seriously.

Also you can just make a std::string instance and assign the value of a pointer dereferenced string to it. Complex types require you copy your data to a new variable / memory block yourself. Memory management involving anything pointer that's not a smart pointer, is completely your responsibility.

For your moving memory behind a pointer question, probably std::move documentation will give you some insights.

By statically allocated pointer you mean a smart pointer, I assume? Pointers are just that, references to a block of memory. How you deal with that memory is your responsibility.

I would go as far as recommending to stay away from raw pointers and only use std:: types like smart pointers, they deal with a lot of difficult things like memory management and copy/move/manipulation of data in safe ways.

Comment button Reply

CollapseExpand

AFIK, you cannot convert dynamic memory in static or stack memory since both need to be known at compile time. You can move it into static memory but you would still need a buffer with a constant size to be prepared at compile time. Which means it won't be a "new" static memory

Comment button Reply


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK