Is it possible to create class String without using heap in C++?
I would like to write me own class String which will have interface similar to std::string. String class shall not use dynamic memory allocation.
I need to have a c-tor:
String(char* ptrToFirstCharInTab, char* ptrToLastElementInTab);
And there should be tab which contains different (not know) number of element, so I do not know size while compiling.
In my opinion it's impossible, because if we do not know size of our array before compilation we can not create it without dynamic allocation - of course creating buffer for 500 char and then String class can be only 500 it' not my expections.
Do you have any idea? Maybe is any way to create buffor wchich I will shrink to fit? Thanks for help!
c++ class memory-management stack static-allocation
|
show 7 more comments
I would like to write me own class String which will have interface similar to std::string. String class shall not use dynamic memory allocation.
I need to have a c-tor:
String(char* ptrToFirstCharInTab, char* ptrToLastElementInTab);
And there should be tab which contains different (not know) number of element, so I do not know size while compiling.
In my opinion it's impossible, because if we do not know size of our array before compilation we can not create it without dynamic allocation - of course creating buffer for 500 char and then String class can be only 500 it' not my expections.
Do you have any idea? Maybe is any way to create buffor wchich I will shrink to fit? Thanks for help!
c++ class memory-management stack static-allocation
1
Your question is more or less akin to "Is it possible to create class String without using memory in C++?"
– Robert Harvey♦
Nov 21 '18 at 18:35
@RobertHarvey, do you mean "without using dynamic memory"?
– R Sahu
Nov 21 '18 at 18:36
@RSahu Why would you want to?
– Robert Harvey♦
Nov 21 '18 at 18:37
4
There is no way to avoid dynamic allocation unless you have some sort of limit. Is there a reason you want to avoid the dynamic allocation? Also, you may want to look into Small String Optimization
– NathanOliver
Nov 21 '18 at 18:37
1
I believe you are right. Dynamic size = dynamic allocation.
– Galik
Nov 21 '18 at 18:37
|
show 7 more comments
I would like to write me own class String which will have interface similar to std::string. String class shall not use dynamic memory allocation.
I need to have a c-tor:
String(char* ptrToFirstCharInTab, char* ptrToLastElementInTab);
And there should be tab which contains different (not know) number of element, so I do not know size while compiling.
In my opinion it's impossible, because if we do not know size of our array before compilation we can not create it without dynamic allocation - of course creating buffer for 500 char and then String class can be only 500 it' not my expections.
Do you have any idea? Maybe is any way to create buffor wchich I will shrink to fit? Thanks for help!
c++ class memory-management stack static-allocation
I would like to write me own class String which will have interface similar to std::string. String class shall not use dynamic memory allocation.
I need to have a c-tor:
String(char* ptrToFirstCharInTab, char* ptrToLastElementInTab);
And there should be tab which contains different (not know) number of element, so I do not know size while compiling.
In my opinion it's impossible, because if we do not know size of our array before compilation we can not create it without dynamic allocation - of course creating buffer for 500 char and then String class can be only 500 it' not my expections.
Do you have any idea? Maybe is any way to create buffor wchich I will shrink to fit? Thanks for help!
c++ class memory-management stack static-allocation
c++ class memory-management stack static-allocation
asked Nov 21 '18 at 18:34
mnurzynsmnurzyns
112
112
1
Your question is more or less akin to "Is it possible to create class String without using memory in C++?"
– Robert Harvey♦
Nov 21 '18 at 18:35
@RobertHarvey, do you mean "without using dynamic memory"?
– R Sahu
Nov 21 '18 at 18:36
@RSahu Why would you want to?
– Robert Harvey♦
Nov 21 '18 at 18:37
4
There is no way to avoid dynamic allocation unless you have some sort of limit. Is there a reason you want to avoid the dynamic allocation? Also, you may want to look into Small String Optimization
– NathanOliver
Nov 21 '18 at 18:37
1
I believe you are right. Dynamic size = dynamic allocation.
– Galik
Nov 21 '18 at 18:37
|
show 7 more comments
1
Your question is more or less akin to "Is it possible to create class String without using memory in C++?"
– Robert Harvey♦
Nov 21 '18 at 18:35
@RobertHarvey, do you mean "without using dynamic memory"?
– R Sahu
Nov 21 '18 at 18:36
@RSahu Why would you want to?
– Robert Harvey♦
Nov 21 '18 at 18:37
4
There is no way to avoid dynamic allocation unless you have some sort of limit. Is there a reason you want to avoid the dynamic allocation? Also, you may want to look into Small String Optimization
– NathanOliver
Nov 21 '18 at 18:37
1
I believe you are right. Dynamic size = dynamic allocation.
– Galik
Nov 21 '18 at 18:37
1
1
Your question is more or less akin to "Is it possible to create class String without using memory in C++?"
– Robert Harvey♦
Nov 21 '18 at 18:35
Your question is more or less akin to "Is it possible to create class String without using memory in C++?"
– Robert Harvey♦
Nov 21 '18 at 18:35
@RobertHarvey, do you mean "without using dynamic memory"?
– R Sahu
Nov 21 '18 at 18:36
@RobertHarvey, do you mean "without using dynamic memory"?
– R Sahu
Nov 21 '18 at 18:36
@RSahu Why would you want to?
– Robert Harvey♦
Nov 21 '18 at 18:37
@RSahu Why would you want to?
– Robert Harvey♦
Nov 21 '18 at 18:37
4
4
There is no way to avoid dynamic allocation unless you have some sort of limit. Is there a reason you want to avoid the dynamic allocation? Also, you may want to look into Small String Optimization
– NathanOliver
Nov 21 '18 at 18:37
There is no way to avoid dynamic allocation unless you have some sort of limit. Is there a reason you want to avoid the dynamic allocation? Also, you may want to look into Small String Optimization
– NathanOliver
Nov 21 '18 at 18:37
1
1
I believe you are right. Dynamic size = dynamic allocation.
– Galik
Nov 21 '18 at 18:37
I believe you are right. Dynamic size = dynamic allocation.
– Galik
Nov 21 '18 at 18:37
|
show 7 more comments
3 Answers
3
active
oldest
votes
You asked:
Do you have any idea? Maybe is any way to create buffor wchich I will shrink to fit?
In theory, yes you can. You can use a pre-allocated buffer as your heap memory. However, you'll have to write your own code to manage that buffer. Doable but not something I would recommend.
1
What happens if the string needs to be bigger than the buffer?
– NathanOliver
Nov 21 '18 at 18:47
+1 I was just about to make some convoluted example with placement new into a globalint8_t mem[1024*1024];
.
– Ted Lyngmo
Nov 21 '18 at 18:48
@NathanOliver I guess it should throw
– Ted Lyngmo
Nov 21 '18 at 18:48
5
@NathanOliver Same thing as if your dynamically allocated string needs to be bigger than your heap :P
– Lightness Races in Orbit
Nov 21 '18 at 18:50
1
@MarcinNurzyński, shrinking is not the right thing to do. When you use that buffer as heap memory of onlyString
or other objects also is entirely up to you.
– R Sahu
Nov 21 '18 at 20:14
|
show 2 more comments
I'm a bit confused with your question. You want to have std:: string
without a heap and without size restrictions. Sorry to bring this to you: you can't have infinite memory.
If you have an pool of memory you want to dedicate to strings without it being fixed size for each string, an allocator can do so.
The default allocator for the containers does new, however you can replace it without having to duplicate the internals of string.
add a comment |
You asked:
Is it possible to create class String without using heap in C++?
In fact, yes, it possible to dynamicly allocate memory on the stack by using _alloca
or similiar platform dependent function. See this other answer for more details:
C++ How to allocate memory dynamically on stack?
I would recommend against it and be absolutely sure that was the best alternative before commencing.
Update:
I created an example with inlined constructer for demonstration purpose using gcc
:
Compiler explorer Link:
https://godbolt.org/z/M1F5VD
Full code:
#include <alloca.h>
struct String {
__attribute__((always_inline)) inline String(size_t size) {
bytes= static_cast<char*>(alloca( size ));// alloca() memory gets allocated here
}
char* bytes;
};
int workWithString( )
{
//std::string teststr("test");
String mystrclass(1000);
mystrclass.bytes[0] = 'a';
mystrclass.bytes[1] = 0;
return 0;
} // alloca() memory only gets freed here
int main() {
return workWithString();
}
1
But usability is quite limited - how would you return the string to calling function?
– Aconcagua
Nov 21 '18 at 19:08
I saw this question before, but still I'm not sure that _alloca will solve this problem? If I use _alloca inside my c-tor it will "frees its memory when you leave a function". How I will get this char array afer c-tor end?
– mnurzyns
Nov 21 '18 at 19:10
@Aconcagua on the stack i suppose ?
– darune
Nov 21 '18 at 20:01
@MarcinNurzyński Im not sure, you just need the allocation to happend outside - everything else can be handled inside c-tor, perhaps if its always inlined - I have, myself, zero experience in using _alloca though.
– darune
Nov 21 '18 at 20:06
@darune If function f shall determine size by itself and then create the string appropriately? You'd have to split any such function into two parts ('f_init` to determine size, then alloca, thenf_fini
to fill the object). In worst case, you'd need a double hierarchy of function calls. In doubt if this is really practicable...
– Aconcagua
Nov 21 '18 at 20:28
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418519%2fis-it-possible-to-create-class-string-without-using-heap-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You asked:
Do you have any idea? Maybe is any way to create buffor wchich I will shrink to fit?
In theory, yes you can. You can use a pre-allocated buffer as your heap memory. However, you'll have to write your own code to manage that buffer. Doable but not something I would recommend.
1
What happens if the string needs to be bigger than the buffer?
– NathanOliver
Nov 21 '18 at 18:47
+1 I was just about to make some convoluted example with placement new into a globalint8_t mem[1024*1024];
.
– Ted Lyngmo
Nov 21 '18 at 18:48
@NathanOliver I guess it should throw
– Ted Lyngmo
Nov 21 '18 at 18:48
5
@NathanOliver Same thing as if your dynamically allocated string needs to be bigger than your heap :P
– Lightness Races in Orbit
Nov 21 '18 at 18:50
1
@MarcinNurzyński, shrinking is not the right thing to do. When you use that buffer as heap memory of onlyString
or other objects also is entirely up to you.
– R Sahu
Nov 21 '18 at 20:14
|
show 2 more comments
You asked:
Do you have any idea? Maybe is any way to create buffor wchich I will shrink to fit?
In theory, yes you can. You can use a pre-allocated buffer as your heap memory. However, you'll have to write your own code to manage that buffer. Doable but not something I would recommend.
1
What happens if the string needs to be bigger than the buffer?
– NathanOliver
Nov 21 '18 at 18:47
+1 I was just about to make some convoluted example with placement new into a globalint8_t mem[1024*1024];
.
– Ted Lyngmo
Nov 21 '18 at 18:48
@NathanOliver I guess it should throw
– Ted Lyngmo
Nov 21 '18 at 18:48
5
@NathanOliver Same thing as if your dynamically allocated string needs to be bigger than your heap :P
– Lightness Races in Orbit
Nov 21 '18 at 18:50
1
@MarcinNurzyński, shrinking is not the right thing to do. When you use that buffer as heap memory of onlyString
or other objects also is entirely up to you.
– R Sahu
Nov 21 '18 at 20:14
|
show 2 more comments
You asked:
Do you have any idea? Maybe is any way to create buffor wchich I will shrink to fit?
In theory, yes you can. You can use a pre-allocated buffer as your heap memory. However, you'll have to write your own code to manage that buffer. Doable but not something I would recommend.
You asked:
Do you have any idea? Maybe is any way to create buffor wchich I will shrink to fit?
In theory, yes you can. You can use a pre-allocated buffer as your heap memory. However, you'll have to write your own code to manage that buffer. Doable but not something I would recommend.
answered Nov 21 '18 at 18:44
R SahuR Sahu
164k1291184
164k1291184
1
What happens if the string needs to be bigger than the buffer?
– NathanOliver
Nov 21 '18 at 18:47
+1 I was just about to make some convoluted example with placement new into a globalint8_t mem[1024*1024];
.
– Ted Lyngmo
Nov 21 '18 at 18:48
@NathanOliver I guess it should throw
– Ted Lyngmo
Nov 21 '18 at 18:48
5
@NathanOliver Same thing as if your dynamically allocated string needs to be bigger than your heap :P
– Lightness Races in Orbit
Nov 21 '18 at 18:50
1
@MarcinNurzyński, shrinking is not the right thing to do. When you use that buffer as heap memory of onlyString
or other objects also is entirely up to you.
– R Sahu
Nov 21 '18 at 20:14
|
show 2 more comments
1
What happens if the string needs to be bigger than the buffer?
– NathanOliver
Nov 21 '18 at 18:47
+1 I was just about to make some convoluted example with placement new into a globalint8_t mem[1024*1024];
.
– Ted Lyngmo
Nov 21 '18 at 18:48
@NathanOliver I guess it should throw
– Ted Lyngmo
Nov 21 '18 at 18:48
5
@NathanOliver Same thing as if your dynamically allocated string needs to be bigger than your heap :P
– Lightness Races in Orbit
Nov 21 '18 at 18:50
1
@MarcinNurzyński, shrinking is not the right thing to do. When you use that buffer as heap memory of onlyString
or other objects also is entirely up to you.
– R Sahu
Nov 21 '18 at 20:14
1
1
What happens if the string needs to be bigger than the buffer?
– NathanOliver
Nov 21 '18 at 18:47
What happens if the string needs to be bigger than the buffer?
– NathanOliver
Nov 21 '18 at 18:47
+1 I was just about to make some convoluted example with placement new into a global
int8_t mem[1024*1024];
.– Ted Lyngmo
Nov 21 '18 at 18:48
+1 I was just about to make some convoluted example with placement new into a global
int8_t mem[1024*1024];
.– Ted Lyngmo
Nov 21 '18 at 18:48
@NathanOliver I guess it should throw
– Ted Lyngmo
Nov 21 '18 at 18:48
@NathanOliver I guess it should throw
– Ted Lyngmo
Nov 21 '18 at 18:48
5
5
@NathanOliver Same thing as if your dynamically allocated string needs to be bigger than your heap :P
– Lightness Races in Orbit
Nov 21 '18 at 18:50
@NathanOliver Same thing as if your dynamically allocated string needs to be bigger than your heap :P
– Lightness Races in Orbit
Nov 21 '18 at 18:50
1
1
@MarcinNurzyński, shrinking is not the right thing to do. When you use that buffer as heap memory of only
String
or other objects also is entirely up to you.– R Sahu
Nov 21 '18 at 20:14
@MarcinNurzyński, shrinking is not the right thing to do. When you use that buffer as heap memory of only
String
or other objects also is entirely up to you.– R Sahu
Nov 21 '18 at 20:14
|
show 2 more comments
I'm a bit confused with your question. You want to have std:: string
without a heap and without size restrictions. Sorry to bring this to you: you can't have infinite memory.
If you have an pool of memory you want to dedicate to strings without it being fixed size for each string, an allocator can do so.
The default allocator for the containers does new, however you can replace it without having to duplicate the internals of string.
add a comment |
I'm a bit confused with your question. You want to have std:: string
without a heap and without size restrictions. Sorry to bring this to you: you can't have infinite memory.
If you have an pool of memory you want to dedicate to strings without it being fixed size for each string, an allocator can do so.
The default allocator for the containers does new, however you can replace it without having to duplicate the internals of string.
add a comment |
I'm a bit confused with your question. You want to have std:: string
without a heap and without size restrictions. Sorry to bring this to you: you can't have infinite memory.
If you have an pool of memory you want to dedicate to strings without it being fixed size for each string, an allocator can do so.
The default allocator for the containers does new, however you can replace it without having to duplicate the internals of string.
I'm a bit confused with your question. You want to have std:: string
without a heap and without size restrictions. Sorry to bring this to you: you can't have infinite memory.
If you have an pool of memory you want to dedicate to strings without it being fixed size for each string, an allocator can do so.
The default allocator for the containers does new, however you can replace it without having to duplicate the internals of string.
answered Nov 21 '18 at 20:26
JVApenJVApen
5,03611134
5,03611134
add a comment |
add a comment |
You asked:
Is it possible to create class String without using heap in C++?
In fact, yes, it possible to dynamicly allocate memory on the stack by using _alloca
or similiar platform dependent function. See this other answer for more details:
C++ How to allocate memory dynamically on stack?
I would recommend against it and be absolutely sure that was the best alternative before commencing.
Update:
I created an example with inlined constructer for demonstration purpose using gcc
:
Compiler explorer Link:
https://godbolt.org/z/M1F5VD
Full code:
#include <alloca.h>
struct String {
__attribute__((always_inline)) inline String(size_t size) {
bytes= static_cast<char*>(alloca( size ));// alloca() memory gets allocated here
}
char* bytes;
};
int workWithString( )
{
//std::string teststr("test");
String mystrclass(1000);
mystrclass.bytes[0] = 'a';
mystrclass.bytes[1] = 0;
return 0;
} // alloca() memory only gets freed here
int main() {
return workWithString();
}
1
But usability is quite limited - how would you return the string to calling function?
– Aconcagua
Nov 21 '18 at 19:08
I saw this question before, but still I'm not sure that _alloca will solve this problem? If I use _alloca inside my c-tor it will "frees its memory when you leave a function". How I will get this char array afer c-tor end?
– mnurzyns
Nov 21 '18 at 19:10
@Aconcagua on the stack i suppose ?
– darune
Nov 21 '18 at 20:01
@MarcinNurzyński Im not sure, you just need the allocation to happend outside - everything else can be handled inside c-tor, perhaps if its always inlined - I have, myself, zero experience in using _alloca though.
– darune
Nov 21 '18 at 20:06
@darune If function f shall determine size by itself and then create the string appropriately? You'd have to split any such function into two parts ('f_init` to determine size, then alloca, thenf_fini
to fill the object). In worst case, you'd need a double hierarchy of function calls. In doubt if this is really practicable...
– Aconcagua
Nov 21 '18 at 20:28
|
show 4 more comments
You asked:
Is it possible to create class String without using heap in C++?
In fact, yes, it possible to dynamicly allocate memory on the stack by using _alloca
or similiar platform dependent function. See this other answer for more details:
C++ How to allocate memory dynamically on stack?
I would recommend against it and be absolutely sure that was the best alternative before commencing.
Update:
I created an example with inlined constructer for demonstration purpose using gcc
:
Compiler explorer Link:
https://godbolt.org/z/M1F5VD
Full code:
#include <alloca.h>
struct String {
__attribute__((always_inline)) inline String(size_t size) {
bytes= static_cast<char*>(alloca( size ));// alloca() memory gets allocated here
}
char* bytes;
};
int workWithString( )
{
//std::string teststr("test");
String mystrclass(1000);
mystrclass.bytes[0] = 'a';
mystrclass.bytes[1] = 0;
return 0;
} // alloca() memory only gets freed here
int main() {
return workWithString();
}
1
But usability is quite limited - how would you return the string to calling function?
– Aconcagua
Nov 21 '18 at 19:08
I saw this question before, but still I'm not sure that _alloca will solve this problem? If I use _alloca inside my c-tor it will "frees its memory when you leave a function". How I will get this char array afer c-tor end?
– mnurzyns
Nov 21 '18 at 19:10
@Aconcagua on the stack i suppose ?
– darune
Nov 21 '18 at 20:01
@MarcinNurzyński Im not sure, you just need the allocation to happend outside - everything else can be handled inside c-tor, perhaps if its always inlined - I have, myself, zero experience in using _alloca though.
– darune
Nov 21 '18 at 20:06
@darune If function f shall determine size by itself and then create the string appropriately? You'd have to split any such function into two parts ('f_init` to determine size, then alloca, thenf_fini
to fill the object). In worst case, you'd need a double hierarchy of function calls. In doubt if this is really practicable...
– Aconcagua
Nov 21 '18 at 20:28
|
show 4 more comments
You asked:
Is it possible to create class String without using heap in C++?
In fact, yes, it possible to dynamicly allocate memory on the stack by using _alloca
or similiar platform dependent function. See this other answer for more details:
C++ How to allocate memory dynamically on stack?
I would recommend against it and be absolutely sure that was the best alternative before commencing.
Update:
I created an example with inlined constructer for demonstration purpose using gcc
:
Compiler explorer Link:
https://godbolt.org/z/M1F5VD
Full code:
#include <alloca.h>
struct String {
__attribute__((always_inline)) inline String(size_t size) {
bytes= static_cast<char*>(alloca( size ));// alloca() memory gets allocated here
}
char* bytes;
};
int workWithString( )
{
//std::string teststr("test");
String mystrclass(1000);
mystrclass.bytes[0] = 'a';
mystrclass.bytes[1] = 0;
return 0;
} // alloca() memory only gets freed here
int main() {
return workWithString();
}
You asked:
Is it possible to create class String without using heap in C++?
In fact, yes, it possible to dynamicly allocate memory on the stack by using _alloca
or similiar platform dependent function. See this other answer for more details:
C++ How to allocate memory dynamically on stack?
I would recommend against it and be absolutely sure that was the best alternative before commencing.
Update:
I created an example with inlined constructer for demonstration purpose using gcc
:
Compiler explorer Link:
https://godbolt.org/z/M1F5VD
Full code:
#include <alloca.h>
struct String {
__attribute__((always_inline)) inline String(size_t size) {
bytes= static_cast<char*>(alloca( size ));// alloca() memory gets allocated here
}
char* bytes;
};
int workWithString( )
{
//std::string teststr("test");
String mystrclass(1000);
mystrclass.bytes[0] = 'a';
mystrclass.bytes[1] = 0;
return 0;
} // alloca() memory only gets freed here
int main() {
return workWithString();
}
edited Nov 22 '18 at 9:06
answered Nov 21 '18 at 18:56
darunedarune
1,050516
1,050516
1
But usability is quite limited - how would you return the string to calling function?
– Aconcagua
Nov 21 '18 at 19:08
I saw this question before, but still I'm not sure that _alloca will solve this problem? If I use _alloca inside my c-tor it will "frees its memory when you leave a function". How I will get this char array afer c-tor end?
– mnurzyns
Nov 21 '18 at 19:10
@Aconcagua on the stack i suppose ?
– darune
Nov 21 '18 at 20:01
@MarcinNurzyński Im not sure, you just need the allocation to happend outside - everything else can be handled inside c-tor, perhaps if its always inlined - I have, myself, zero experience in using _alloca though.
– darune
Nov 21 '18 at 20:06
@darune If function f shall determine size by itself and then create the string appropriately? You'd have to split any such function into two parts ('f_init` to determine size, then alloca, thenf_fini
to fill the object). In worst case, you'd need a double hierarchy of function calls. In doubt if this is really practicable...
– Aconcagua
Nov 21 '18 at 20:28
|
show 4 more comments
1
But usability is quite limited - how would you return the string to calling function?
– Aconcagua
Nov 21 '18 at 19:08
I saw this question before, but still I'm not sure that _alloca will solve this problem? If I use _alloca inside my c-tor it will "frees its memory when you leave a function". How I will get this char array afer c-tor end?
– mnurzyns
Nov 21 '18 at 19:10
@Aconcagua on the stack i suppose ?
– darune
Nov 21 '18 at 20:01
@MarcinNurzyński Im not sure, you just need the allocation to happend outside - everything else can be handled inside c-tor, perhaps if its always inlined - I have, myself, zero experience in using _alloca though.
– darune
Nov 21 '18 at 20:06
@darune If function f shall determine size by itself and then create the string appropriately? You'd have to split any such function into two parts ('f_init` to determine size, then alloca, thenf_fini
to fill the object). In worst case, you'd need a double hierarchy of function calls. In doubt if this is really practicable...
– Aconcagua
Nov 21 '18 at 20:28
1
1
But usability is quite limited - how would you return the string to calling function?
– Aconcagua
Nov 21 '18 at 19:08
But usability is quite limited - how would you return the string to calling function?
– Aconcagua
Nov 21 '18 at 19:08
I saw this question before, but still I'm not sure that _alloca will solve this problem? If I use _alloca inside my c-tor it will "frees its memory when you leave a function". How I will get this char array afer c-tor end?
– mnurzyns
Nov 21 '18 at 19:10
I saw this question before, but still I'm not sure that _alloca will solve this problem? If I use _alloca inside my c-tor it will "frees its memory when you leave a function". How I will get this char array afer c-tor end?
– mnurzyns
Nov 21 '18 at 19:10
@Aconcagua on the stack i suppose ?
– darune
Nov 21 '18 at 20:01
@Aconcagua on the stack i suppose ?
– darune
Nov 21 '18 at 20:01
@MarcinNurzyński Im not sure, you just need the allocation to happend outside - everything else can be handled inside c-tor, perhaps if its always inlined - I have, myself, zero experience in using _alloca though.
– darune
Nov 21 '18 at 20:06
@MarcinNurzyński Im not sure, you just need the allocation to happend outside - everything else can be handled inside c-tor, perhaps if its always inlined - I have, myself, zero experience in using _alloca though.
– darune
Nov 21 '18 at 20:06
@darune If function f shall determine size by itself and then create the string appropriately? You'd have to split any such function into two parts ('f_init` to determine size, then alloca, then
f_fini
to fill the object). In worst case, you'd need a double hierarchy of function calls. In doubt if this is really practicable...– Aconcagua
Nov 21 '18 at 20:28
@darune If function f shall determine size by itself and then create the string appropriately? You'd have to split any such function into two parts ('f_init` to determine size, then alloca, then
f_fini
to fill the object). In worst case, you'd need a double hierarchy of function calls. In doubt if this is really practicable...– Aconcagua
Nov 21 '18 at 20:28
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418519%2fis-it-possible-to-create-class-string-without-using-heap-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Your question is more or less akin to "Is it possible to create class String without using memory in C++?"
– Robert Harvey♦
Nov 21 '18 at 18:35
@RobertHarvey, do you mean "without using dynamic memory"?
– R Sahu
Nov 21 '18 at 18:36
@RSahu Why would you want to?
– Robert Harvey♦
Nov 21 '18 at 18:37
4
There is no way to avoid dynamic allocation unless you have some sort of limit. Is there a reason you want to avoid the dynamic allocation? Also, you may want to look into Small String Optimization
– NathanOliver
Nov 21 '18 at 18:37
1
I believe you are right. Dynamic size = dynamic allocation.
– Galik
Nov 21 '18 at 18:37