Anonymous
No one uses std::unique_ptr just to create object, it's a bad practice. Allocating memory for every object I mean.
Need to create an object? Create it on a stack
Example:
std::unique_ptr<std::vector<int>> v; // bad
std::vector<int> v; // good
Patrick
In my case I have an object that I am storing inside a vector that owns some memory, so vector<unique_ptr<X>>
Patrick
but I was wondering if I could just somehow do vector<X> and have X act like a unique_ptr... like a unique Value, but I suppose I can't do that because I wouldn't know when it's finally out of scope
Anonymous
std::unique_ptr can be used for
1) allocating big objects that do not fit to stack
2) allocating objects with expensive move construction/assignment
3) creating arrays with static size but which known only in runtime
Anonymous
Anonymous
If you just need to disable copies, just delete copy constructor and copy assignment operator
Anonymous
Anonymous
Patrick
Say I have a class that owns a pointer
class X { Y* y; ... }
Right now I'm considering moving it around with unique_ptr and freeing y in the destructor of X, but is there a better way?
Anonymous
If class X deletes Y's objects in destructor
Anonymous
Just make Y pointer to std::unique_ptr
Anonymous
X::~X() = default;
Anonymous
And that's it, if you have no more logic in your constructors
Anonymous
Everything else a compiler will generate for you
Patrick
So right now I have
struct Function {
vector<Object*> objs;
Function ();
~Function ();
}
And you're suggesting instead of moving around Function objects I make the Function struct instead like this:
struct Function {
vector<unique_ptr<Object>> objs;
Function ();
~Function ();
}
Right?
Dev
Can anyone suggest me free courses for learning c online and learning C by a book or by online course which is better
Patrick
Anonymous
Anonymous
Patrick
It's not, but I still need to manage its lifetime
Anonymous
Anonymous
Patrick
Because the Object... also manages heap memory 😶
Anonymous
Patrick
if its destructor is called its heap memory is destroyed
Anonymous
So what?
Patrick
So I can't be copying them by value all over the place, I interact with them regularly
Anonymous
Patrick
I keep the vector of them in my Function class so when the function is destroyed the Objects are destroyed and therefore its internal pointer is destroyed
Anonymous
std::vector<std::unique_ptr<Object>> and std::vector<Object>
Objects in both cases have same lifetime
Anonymous
But the first case has overhead of memory allocation
Patrick
Hence I have been hesitant thinking that's the solution
Anonymous
Anonymous
i meant shared_ptr
Shared pointer is used to handle multiple ownership of an object
If this is the case, then yeah
Anonymous
But I don't think so
Anonymous
Anonymous
Amir
Hi. I'm sending binary data from FPGA to PC. I'm using C to receive the data. Everything works fine but I don't know how to covert the received char to binary value like "10110010". I would appreciate it if you could help me
Anonymous
Asdew
What do you want to do with the byte?
Anonymous
extract the individual bits probably
Asdew
But does he want to get it as a string, check if some bit is set or what?
Amir
I want to store then in a txt file
Amir
Amir
What I want to do is as follows. I read binary values from a text file, send them to fpga through serial port. Fpga decodes them and sends back the decoded value. Now what I want is to store the decoded data as ones and zeros into a text file.
Amir
Unfortunately the received data type is char
Amir
And I don't know how to convert it to string of ones and zeros
Anonymous
you can always use fwrite and fread
Anonymous
Amir
I need the binary value
Anonymous
Input = what you get from FPGA. output = what you want to store
Amir
Right now I'm reading packet by packet using "ReadFile" function
Amir
Anonymous
Exactly
use bitwise operator to separate the bits. then + '0' to them.
Amir
To be more precise, input=serial port outut, output=txt file
Amir
Anonymous
How can I use that?
char a = 0x12; // assumption BITS_PER_BYTE is 8
for (int i = 0; i < 8; ++i) {
char out = a & 0x1; // assumption sizeof(int) is 4
a >>= 1;
out += '0';
putchar(out); // ? do whatever you want here
}
Anonymous
lemme correct the amount to shift wait a minute
Amir
Ok. Thank you for your time
Anonymous
Amir
Thank you I will try it noe
Amir
It works
Amir
Thanks
Mar!o
Hey guys, little OT but I've made a collection with bit twiddling hacks in a single C header file with short explanations to be used as a cheat sheet or to play around.
Please take a look if you know any bitwise hacks not yet included - I want this to become a huge collection with all kinds of hacks!
If you know any new or encounter anything please submit a PR ^^
https://github.com/MarioSieg/Bit-Twiddling-Hacks-Collection
Anonymous
✋
Serenity
Is it ok to pass a string to a function as an array or must I send it as a pointer ?
Yoh
Alex
Serenity
And another question : Is there any difference between : for ( i=0 ; i< 10 ; i++ ) and for( i=0 ; i<10; --i) or they are just the same ?
Serenity
I know that it matters in other places
Anonymous
Alex
Anonymous
why?
void fn(char (*s)[40]) will only accept char (*)[40], anything else will lead to undefined behaviour
Alex