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.)
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
Let's say I have an array like:
{1,2,3}
So, for example, some function like:
arr.push(4);
Will make this array:
{4,1,2,3}
How do I do that?
It is not possible to push an element to an array. The size of an array remains the same through the lifetime of the array.
What can be done instead is to create a new, larger array and copy the elements from the old array. Such dynamic growable "array" data structure is provided for you in the standard library: std::vector.
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 have a std::vector<int64_t> myVec in which I want to store a string (text). My understanding is that i cannot reserve space and write to myVec.data(), as this would be undefined behavior. What would be the non-hackish way to do this? I'm assuming the last int64_t will have to be filled with padding zeroes.
I'm using C++14.
If you are constrained to vector the only solution is resize() to required size and strcpy/memcpy into data()
Why would you store text in int?
Is your intention to store digits made from string?
If so, you shall:
std::vector<int64_t> myVec;
myVec.push_back(atoll("100"))
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)
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
In the following statement:
char *myarray[] = {"Amir"};
For pointer myarray[], how many bytes of memory has been allocated?
It depends on the OS Architecture. Because it is an array of char *, It will take size equivalent to one pointer in this case.
For 32-bit addressing, it will take 4 bytes.
For 64-bit addressing, it will take 8 bytes.
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 6 years ago.
Improve this question
I am extremely new to C++ so I'm probably asking a very trivial question, but if you could help that'd be great!
I have an array[n].
Indexed from 0 to some unknown value.
I need to access the index of the array, the n value but I need to do so in binary. I am intending to do a bit reversal on it.
So, if I have an array of 2048 points how do I represent the 1024 array in binary?
If you want to write a value in binary, you can do so in C++14 with
int my_binary_value = 0b01010101;
If you'd like to test a specific bit of an int, you can do that by masking it, i.e.
bool is_bit_4_set = my_binary_value & 0b00001000;