static-allocation

Why do Objective-C objects have to be dynamically allocated?

霸气de小男生 提交于 2019-12-30 01:53:04
问题 Why do Objective-c objects have to be dynamically allocated? Why do I have to make it a pointer to an object, unlike in C++ I can create them on stack? Thanks. 回答1: the primary reason: not knowing how much stack size to reserve. existing conventions and uses also make lifting the restriction quite difficult. dynamic messaging does not matter in this case, as setting the right 'vtable' at initialization is trivial. in c++, the size of a stack object is always known (and if it's wrong, you know

Does std::array<> guarantee allocation on the stack only?

给你一囗甜甜゛ 提交于 2019-12-18 13:53:20
问题 Is std::array<int,10> (without myself using new ) guaranteed to be allocated in the stack rather then the heap by the C++-Standard? To be clear, I do not mean new std::array<int, 10> . I mainly wonder, if the standard library is allowed to use new inside its implementation. 回答1: I could not find more explicit answer in the standard, but [array.overview]/2: An array is an aggregate ( [dcl.init.aggr] ) that can be list-initialized with up to N elements whose types are convertible to T . And

Are Sub-Arrays Guaranteed to be Allocated Linearly? [duplicate]

China☆狼群 提交于 2019-12-12 19:31:51
问题 This question already has answers here : C / C++ MultiDimensional Array Internals (4 answers) Closed 3 years ago . I know this answer is in violation of the reinterpret_cast rules but it also presumes that sub-arrays will be allocated linearly. I believed this was not guaranteed, but as I search the standard, I find my confidence wavering. If I statically allocate a 2D array, like this: int foo[][4] = { { 5, 7, 8 }, { 6, 6 }, {}, { 5, 6, 8, 9 } }; Am I allowed to assume that all elements will

Can I increase the size of a statically allocated array?

孤街醉人 提交于 2019-12-11 12:44:01
问题 I know its possible to increase the size of a dynamically allocated array. But can I increase the size of a statically allocated array? If yes,how? EDIT: Though this question is intended for C language, consider other languages too.Is it possible in any other language? 回答1: Simple answer is no, this cannot be done. Hence the name "static". Now, lots of languages have things that look like statically allocated arrays but are actually statically allocated references to a dynamically allocated

Possible to create statically allocated array in swift?

旧城冷巷雨未停 提交于 2019-12-06 05:50:20
问题 I want to create a struct in swift that has a small fixed number of values (say 16 floats) as instance data. It is required that this struct not store these values on the heap, so that the address of an instance of the struct is the address of the instance vars. It is also a requirement that these values be accessible internally to the struct via subscript, as Arrays are. In C you would simply define this kind of thing thusly: struct Matrix4x4 { float elements[16]; ... } myMatrix; With this

Possible to create statically allocated array in swift?

妖精的绣舞 提交于 2019-12-04 09:53:20
I want to create a struct in swift that has a small fixed number of values (say 16 floats) as instance data. It is required that this struct not store these values on the heap, so that the address of an instance of the struct is the address of the instance vars. It is also a requirement that these values be accessible internally to the struct via subscript, as Arrays are. In C you would simply define this kind of thing thusly: struct Matrix4x4 { float elements[16]; ... } myMatrix; With this code, sizeof(Matrix4x4) == 64 and also &myMatrix == &myMatrix.elements[0]; In swift, if I analogously

Does std::array<> guarantee allocation on the stack only?

别来无恙 提交于 2019-11-30 11:05:22
Is std::array<int,10> (without myself using new ) guaranteed to be allocated in the stack rather then the heap by the C++-Standard? To be clear, I do not mean new std::array<int, 10> . I mainly wonder, if the standard library is allowed to use new inside its implementation. I could not find more explicit answer in the standard, but [array.overview]/2 : An array is an aggregate ( [dcl.init.aggr] ) that can be list-initialized with up to N elements whose types are convertible to T . And [dcl.init.aggr]/1 : An aggregate is an array or a class (Clause [class] ) with no user-provided , explicit, or

Returning 'c_str' from a function

白昼怎懂夜的黑 提交于 2019-11-27 15:10:47
This is from a small library that I found online: const char* GetHandStateBrief(const PostFlopState* state) { static std::ostringstream out; // ... rest of the function ... return out.str().c_str() } In my code I am doing this: const char *d = GetHandStateBrief(&post); std::cout<< d << std::endl; Now, at first d contained garbage. I then realized that the C string I am getting from the function is destroyed when the function returns because std::ostringstream is allocated on the stack. So I added: return strdup( out.str().c_str()); And now I can get the text I need from the function. I have

Returning 'c_str' from a function

一世执手 提交于 2019-11-26 17:05:01
问题 This is from a small library that I found online: const char* GetHandStateBrief(const PostFlopState* state) { static std::ostringstream out; // ... rest of the function ... return out.str().c_str() } In my code I am doing this: const char *d = GetHandStateBrief(&post); std::cout<< d << std::endl; Now, at first d contained garbage. I then realized that the C string I am getting from the function is destroyed when the function returns because std::ostringstream is allocated on the stack. So I