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
If you just need to disable copies, just delete copy constructor and copy assignment operator
Patrick
If your class X is not one of the list below, change to std::vector<X>
What should I do to manage the pointers my class contains? I do know when these values are created and finally destroyed so I could manually invoke them to be freed
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
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?
Ok So there are two semantic (not syntactic) types of pointers: owning and non-owning Owning pointers owns memory they point to, if you have an owning pointer in a class, you have to delete in destructor Non-owning pointers just point to memory, they do not know where it came from (stack, heap, etc) Worth mentioning that owning RAW pointers are managed only by a programmer
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
Anonymous
Hello, i have a doubt, if i call std::vector<int> second (4); it will create a vector of 4 elements or just will create an empty vector with space for 4 elements?
btw you can do std::vector<int> second; second.reserve(4); for what you want to do. or std::array<int, 4> second; if size is going to be 4 permanently
Patrick
It's not, but I still need to manage its lifetime
Anonymous
It's not, but I still need to manage its lifetime
Why not to use std::vector<Object> then?
Patrick
Because the Object... also manages heap memory 😶
Anonymous
Or std::make_unique<int[]>(4)
ye or a pmr vector with char buffer already allocated
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
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
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
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
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
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
I need the binary value
do you want one output char from each bit in the input char?
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
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
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
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 ?
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
Is it ok to pass a string to a function as an array or must I send it as a pointer ?
you can pass it as an array, but then your function can't work with arrays of different lengths
Anonymous
why?
void fn(char (*s)[40]) will only accept char (*)[40], anything else will lead to undefined behaviour
Alex
void fn(char (*s)[40]) will only accept char (*)[40], anything else will lead to undefined behaviour
I think he means void fn(char s[40]) in your example it is pointer to array
Anonymous
I think he means void fn(char s[40]) in your example it is pointer to array
but in this case there is no way to pass an array since arrays used like this will turn into pointers, so there is no question of whether it is ok to pass as array