passing binary data another thread [closed]











up vote
-2
down vote

favorite












I have a API that takes binary data and internally it has to push that in to a queue and another thread reads from the queue and process the data.



I would like to avoid allocating dynamic memory allocation.



My API is like



bool some_func(unsigned char* data, int length);


Should I use stringstream as argument to handle binary data?



Thank you










share|improve this question















closed as unclear what you're asking by πάντα ῥεῖ, Basile Starynkevitch, Niall, SergeyA, C-Pound Guru Nov 12 at 20:22


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • Read some Pthread tutorial then adapt that knowledge to your C++11 threads, if you use these
    – Basile Starynkevitch
    Nov 12 at 18:39






  • 1




    Maybe a std::vector<uint8_t> seems to be more suitable.
    – πάντα ῥεῖ
    Nov 12 at 18:39










  • You will have to use the dynamic memory allocation in any case (but not each time you are put data into queue, i.e. new will be called only once). Check this
    – Victor Gubin
    Nov 12 at 18:39










  • My concern was on the life time of the data passed. Since the other thread may not know whether the address is still referring to a valid data.Can I use a shared_ptr as API argument ?
    – Deepak
    Nov 12 at 19:38

















up vote
-2
down vote

favorite












I have a API that takes binary data and internally it has to push that in to a queue and another thread reads from the queue and process the data.



I would like to avoid allocating dynamic memory allocation.



My API is like



bool some_func(unsigned char* data, int length);


Should I use stringstream as argument to handle binary data?



Thank you










share|improve this question















closed as unclear what you're asking by πάντα ῥεῖ, Basile Starynkevitch, Niall, SergeyA, C-Pound Guru Nov 12 at 20:22


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • Read some Pthread tutorial then adapt that knowledge to your C++11 threads, if you use these
    – Basile Starynkevitch
    Nov 12 at 18:39






  • 1




    Maybe a std::vector<uint8_t> seems to be more suitable.
    – πάντα ῥεῖ
    Nov 12 at 18:39










  • You will have to use the dynamic memory allocation in any case (but not each time you are put data into queue, i.e. new will be called only once). Check this
    – Victor Gubin
    Nov 12 at 18:39










  • My concern was on the life time of the data passed. Since the other thread may not know whether the address is still referring to a valid data.Can I use a shared_ptr as API argument ?
    – Deepak
    Nov 12 at 19:38















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have a API that takes binary data and internally it has to push that in to a queue and another thread reads from the queue and process the data.



I would like to avoid allocating dynamic memory allocation.



My API is like



bool some_func(unsigned char* data, int length);


Should I use stringstream as argument to handle binary data?



Thank you










share|improve this question















I have a API that takes binary data and internally it has to push that in to a queue and another thread reads from the queue and process the data.



I would like to avoid allocating dynamic memory allocation.



My API is like



bool some_func(unsigned char* data, int length);


Should I use stringstream as argument to handle binary data?



Thank you







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 18:37









πάντα ῥεῖ

71.5k972134




71.5k972134










asked Nov 12 at 18:34









Deepak

144




144




closed as unclear what you're asking by πάντα ῥεῖ, Basile Starynkevitch, Niall, SergeyA, C-Pound Guru Nov 12 at 20:22


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by πάντα ῥεῖ, Basile Starynkevitch, Niall, SergeyA, C-Pound Guru Nov 12 at 20:22


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • Read some Pthread tutorial then adapt that knowledge to your C++11 threads, if you use these
    – Basile Starynkevitch
    Nov 12 at 18:39






  • 1




    Maybe a std::vector<uint8_t> seems to be more suitable.
    – πάντα ῥεῖ
    Nov 12 at 18:39










  • You will have to use the dynamic memory allocation in any case (but not each time you are put data into queue, i.e. new will be called only once). Check this
    – Victor Gubin
    Nov 12 at 18:39










  • My concern was on the life time of the data passed. Since the other thread may not know whether the address is still referring to a valid data.Can I use a shared_ptr as API argument ?
    – Deepak
    Nov 12 at 19:38




















  • Read some Pthread tutorial then adapt that knowledge to your C++11 threads, if you use these
    – Basile Starynkevitch
    Nov 12 at 18:39






  • 1




    Maybe a std::vector<uint8_t> seems to be more suitable.
    – πάντα ῥεῖ
    Nov 12 at 18:39










  • You will have to use the dynamic memory allocation in any case (but not each time you are put data into queue, i.e. new will be called only once). Check this
    – Victor Gubin
    Nov 12 at 18:39










  • My concern was on the life time of the data passed. Since the other thread may not know whether the address is still referring to a valid data.Can I use a shared_ptr as API argument ?
    – Deepak
    Nov 12 at 19:38


















Read some Pthread tutorial then adapt that knowledge to your C++11 threads, if you use these
– Basile Starynkevitch
Nov 12 at 18:39




Read some Pthread tutorial then adapt that knowledge to your C++11 threads, if you use these
– Basile Starynkevitch
Nov 12 at 18:39




1




1




Maybe a std::vector<uint8_t> seems to be more suitable.
– πάντα ῥεῖ
Nov 12 at 18:39




Maybe a std::vector<uint8_t> seems to be more suitable.
– πάντα ῥεῖ
Nov 12 at 18:39












You will have to use the dynamic memory allocation in any case (but not each time you are put data into queue, i.e. new will be called only once). Check this
– Victor Gubin
Nov 12 at 18:39




You will have to use the dynamic memory allocation in any case (but not each time you are put data into queue, i.e. new will be called only once). Check this
– Victor Gubin
Nov 12 at 18:39












My concern was on the life time of the data passed. Since the other thread may not know whether the address is still referring to a valid data.Can I use a shared_ptr as API argument ?
– Deepak
Nov 12 at 19:38






My concern was on the life time of the data passed. Since the other thread may not know whether the address is still referring to a valid data.Can I use a shared_ptr as API argument ?
– Deepak
Nov 12 at 19:38














1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted











I would like to avoid allocating dynamic memory allocation



Should I use stringstream as argument to handle binary data?




I think you will be disappointed with most of the std containers, and with stringstream in particular.





For example, on Lubuntu 18.04 (64 bit), std::stringstream uses a std::string as its buffer.



sizeof(std::string ) with no data reports 32 bytes.



sizeof(std::string) with 1,000,000 chars in it, reports 32 bytes, while s.size() reports 1,000,000.



( These are implementation details which I have investigated on 18.04. )



More clearly: the 1M chars in the std::string (in the stringstream) are allocated to dynamic memory. The container objects are simply performing the dynamic memory allocation and handling for you.





The std::vector also uses dynamic memory. sizeof (std::vector) reports 24 bytes. For "std::vector v;" with 1000 elements, sizeof(v) is 24, and v.size() is 1000. The container is performing the dynamic memory handling.





You can not use vector or string to avoid dynamic memory allocation.



Now ask yourself why you wish to avoid it, and are you sure you need to?



Consider that you can allocate your buffers once at the beginning of your effort.






share|improve this answer





















  • If, by "I want to avoid dynamic memory allocation" you mean you wish to avoid writing the new's and deletes of buffers ... then yes ... stringstream or simply string will take care of the error prone activity on your behalf, and would simplify your effort.
    – 2785528
    Nov 12 at 20:06












  • std::ostringstream inherits from basic_ostream, and thus has a binary write method. etc. You can do binary in/out to the stringstream's.
    – 2785528
    Nov 12 at 20:16


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted











I would like to avoid allocating dynamic memory allocation



Should I use stringstream as argument to handle binary data?




I think you will be disappointed with most of the std containers, and with stringstream in particular.





For example, on Lubuntu 18.04 (64 bit), std::stringstream uses a std::string as its buffer.



sizeof(std::string ) with no data reports 32 bytes.



sizeof(std::string) with 1,000,000 chars in it, reports 32 bytes, while s.size() reports 1,000,000.



( These are implementation details which I have investigated on 18.04. )



More clearly: the 1M chars in the std::string (in the stringstream) are allocated to dynamic memory. The container objects are simply performing the dynamic memory allocation and handling for you.





The std::vector also uses dynamic memory. sizeof (std::vector) reports 24 bytes. For "std::vector v;" with 1000 elements, sizeof(v) is 24, and v.size() is 1000. The container is performing the dynamic memory handling.





You can not use vector or string to avoid dynamic memory allocation.



Now ask yourself why you wish to avoid it, and are you sure you need to?



Consider that you can allocate your buffers once at the beginning of your effort.






share|improve this answer





















  • If, by "I want to avoid dynamic memory allocation" you mean you wish to avoid writing the new's and deletes of buffers ... then yes ... stringstream or simply string will take care of the error prone activity on your behalf, and would simplify your effort.
    – 2785528
    Nov 12 at 20:06












  • std::ostringstream inherits from basic_ostream, and thus has a binary write method. etc. You can do binary in/out to the stringstream's.
    – 2785528
    Nov 12 at 20:16















up vote
0
down vote



accepted











I would like to avoid allocating dynamic memory allocation



Should I use stringstream as argument to handle binary data?




I think you will be disappointed with most of the std containers, and with stringstream in particular.





For example, on Lubuntu 18.04 (64 bit), std::stringstream uses a std::string as its buffer.



sizeof(std::string ) with no data reports 32 bytes.



sizeof(std::string) with 1,000,000 chars in it, reports 32 bytes, while s.size() reports 1,000,000.



( These are implementation details which I have investigated on 18.04. )



More clearly: the 1M chars in the std::string (in the stringstream) are allocated to dynamic memory. The container objects are simply performing the dynamic memory allocation and handling for you.





The std::vector also uses dynamic memory. sizeof (std::vector) reports 24 bytes. For "std::vector v;" with 1000 elements, sizeof(v) is 24, and v.size() is 1000. The container is performing the dynamic memory handling.





You can not use vector or string to avoid dynamic memory allocation.



Now ask yourself why you wish to avoid it, and are you sure you need to?



Consider that you can allocate your buffers once at the beginning of your effort.






share|improve this answer





















  • If, by "I want to avoid dynamic memory allocation" you mean you wish to avoid writing the new's and deletes of buffers ... then yes ... stringstream or simply string will take care of the error prone activity on your behalf, and would simplify your effort.
    – 2785528
    Nov 12 at 20:06












  • std::ostringstream inherits from basic_ostream, and thus has a binary write method. etc. You can do binary in/out to the stringstream's.
    – 2785528
    Nov 12 at 20:16













up vote
0
down vote



accepted







up vote
0
down vote



accepted







I would like to avoid allocating dynamic memory allocation



Should I use stringstream as argument to handle binary data?




I think you will be disappointed with most of the std containers, and with stringstream in particular.





For example, on Lubuntu 18.04 (64 bit), std::stringstream uses a std::string as its buffer.



sizeof(std::string ) with no data reports 32 bytes.



sizeof(std::string) with 1,000,000 chars in it, reports 32 bytes, while s.size() reports 1,000,000.



( These are implementation details which I have investigated on 18.04. )



More clearly: the 1M chars in the std::string (in the stringstream) are allocated to dynamic memory. The container objects are simply performing the dynamic memory allocation and handling for you.





The std::vector also uses dynamic memory. sizeof (std::vector) reports 24 bytes. For "std::vector v;" with 1000 elements, sizeof(v) is 24, and v.size() is 1000. The container is performing the dynamic memory handling.





You can not use vector or string to avoid dynamic memory allocation.



Now ask yourself why you wish to avoid it, and are you sure you need to?



Consider that you can allocate your buffers once at the beginning of your effort.






share|improve this answer













I would like to avoid allocating dynamic memory allocation



Should I use stringstream as argument to handle binary data?




I think you will be disappointed with most of the std containers, and with stringstream in particular.





For example, on Lubuntu 18.04 (64 bit), std::stringstream uses a std::string as its buffer.



sizeof(std::string ) with no data reports 32 bytes.



sizeof(std::string) with 1,000,000 chars in it, reports 32 bytes, while s.size() reports 1,000,000.



( These are implementation details which I have investigated on 18.04. )



More clearly: the 1M chars in the std::string (in the stringstream) are allocated to dynamic memory. The container objects are simply performing the dynamic memory allocation and handling for you.





The std::vector also uses dynamic memory. sizeof (std::vector) reports 24 bytes. For "std::vector v;" with 1000 elements, sizeof(v) is 24, and v.size() is 1000. The container is performing the dynamic memory handling.





You can not use vector or string to avoid dynamic memory allocation.



Now ask yourself why you wish to avoid it, and are you sure you need to?



Consider that you can allocate your buffers once at the beginning of your effort.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 19:56









2785528

4,27811017




4,27811017












  • If, by "I want to avoid dynamic memory allocation" you mean you wish to avoid writing the new's and deletes of buffers ... then yes ... stringstream or simply string will take care of the error prone activity on your behalf, and would simplify your effort.
    – 2785528
    Nov 12 at 20:06












  • std::ostringstream inherits from basic_ostream, and thus has a binary write method. etc. You can do binary in/out to the stringstream's.
    – 2785528
    Nov 12 at 20:16


















  • If, by "I want to avoid dynamic memory allocation" you mean you wish to avoid writing the new's and deletes of buffers ... then yes ... stringstream or simply string will take care of the error prone activity on your behalf, and would simplify your effort.
    – 2785528
    Nov 12 at 20:06












  • std::ostringstream inherits from basic_ostream, and thus has a binary write method. etc. You can do binary in/out to the stringstream's.
    – 2785528
    Nov 12 at 20:16
















If, by "I want to avoid dynamic memory allocation" you mean you wish to avoid writing the new's and deletes of buffers ... then yes ... stringstream or simply string will take care of the error prone activity on your behalf, and would simplify your effort.
– 2785528
Nov 12 at 20:06






If, by "I want to avoid dynamic memory allocation" you mean you wish to avoid writing the new's and deletes of buffers ... then yes ... stringstream or simply string will take care of the error prone activity on your behalf, and would simplify your effort.
– 2785528
Nov 12 at 20:06














std::ostringstream inherits from basic_ostream, and thus has a binary write method. etc. You can do binary in/out to the stringstream's.
– 2785528
Nov 12 at 20:16




std::ostringstream inherits from basic_ostream, and thus has a binary write method. etc. You can do binary in/out to the stringstream's.
– 2785528
Nov 12 at 20:16



Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)