Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
One example of dynamic allocation on cppreference.com is:
int* p1 = new int;
Is there ever a need to allocate a single int dynamically?
When all integral values are valid in your application's logic, using an int* adds the additional NULL value. This is useful when, for example, you are dealing with a NULLABLE database column of type int.
Additionally, on 16-bit systems, int operations weren't atomic, so updating an int value while reading it from another thread isn't a threadsafe operation, but sharing an int* is.
(Yeah, I'm stretching here, but I have used both in the past)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've got an std::array<std::shared_ptr<void>, N>, and I have methods for accessing parts of this buffer as different types, which I would like to use std::span<std::shared_ptr<T>> for.
Is there a way to construct a span like this without invoking UB?
No, this is impossible: regardless of the ability to convert void* to T*, you can’t convert void** (a pointer to your first pointer) to T** because there are no actual T* objects there, and you certainly can’t convert std::shared_ptr<A>* to std::shared_ptr<B>* for any distinct A and B—std::shared_ptr<T> isn’t just a T* internally (because of the control block), and even if it were you’re not allowed to “unwrap” arrays of structures and treat them as arrays of their contents (with the magic exception of std::complex).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
private:
vector<float*> m_values;
vector<float> *m_index;
vector<float*> *m_rowptr;
What would be the best way to save a large data structure, using vectors?
The way to store a large quantity of floats is:
vector<float> m_index;
The vector manages the block of floats for you.
Making a pointer to the vector is pointless (hah!).
Making a vector of pointers is (usually*) not correct for your task, and it introduces a host of performance problems.
Making both things a pointer combines the two problems.
* Depending what you mean by "index", this may be one of those times. If you want a vector of "pointers to float", then make that thing.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there any way I can append the size of a particular variable? For instance, if I have:
static_cast<char>(0x0147)
an 8 bit char and I want to increase it to say 16 bits without changing the data type, is that possible?
No.
The size of the object is not only related to its type; the size is defined by the type.
You cannot change one and not the other.
Just initialize a new int16_t from this char if that's what you want.
Or, you could have a vector<char> and add new elements to this collection as needed.
(Shifting has nothing to do with it; that's about transforming data.)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
While revising for my test the thought came to my mind that why do we exactly need to give a variable a specific datatype? Can the our computers not differentiate between the character an integer values we store in variables? If they cant why?
Consider this simple example.
int i;
int j;
if (b)
{
i = 0;
}
else
{
i = 999999;
}
Where should j be placed in memory? Depending on a run-time condition, either a small or large number is stored in i. But space for j is needed before this point is reached, so i must have an established size. Therefore compiler needs to know its basic type and therefore size.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Should a thread-safe mechanism be added when a class is developed and it is known that this class will be used in multi-threaded environment (not always however) or leave it to the user?
As a general rule, it's more flexible to leave it to the user. For example, consider a map-type container. Suppose the application needs to atomically move something from one map to another map. In this case, the user needs to lock both maps before the insert-erase sequence.
Having such a scenario be automatically taken care of somehow by your class would probably be inelegant, because it's naturally something that happens across objects, and because there may be many such scenarios, each slightly different.