- vector of string to array of c string - 10 Updates
- Converting local time to UTC - 1 Update
- how to calculate size of an object - 1 Update
Christopher Pisz <nospam@notanaddress.com>: Oct 16 10:49AM -0500 I am trying to pass a vector of strings to my wrapper method, which wraps some icky Windows API calls. I cannot seem to get the conversion from the vector of strings to the array of icky windows type, which after digging through defines is really just const wchar_t * Can someone help me out? Here is the original code that worked: LPCWSTR types[4]; types[0] = L"text/html"; types[1] = L"text/xml"; types[2] = L"application/json"; types[3] = 0; HINTERNET requestHandle = WinHttpOpenRequest(m_connectHandle, L"POST", wideResource.c_str(), NULL, WINHTTP_NO_REFERER, types, m_secure? WINHTTP_FLAG_SECURE : NULL); Here is my attempt at getting the data in the form of a vector of strings: // Ermehgerd Windows API casting to silly string types std::vector<std::string> things{"text/html", "text/xml", "application/json"}; LPCWSTR * typesArray = new LPCWSTR[things.size()+ 1]; for(size_t index = 0; index < things.size(); ++index) { std::wstring temp = Shared::MultibyteToWide(things[index].c_str()); typesArray[index] = new wchar_t[temp.size() + 1]; StringCchCopyW(const_cast<wchar_t *>(typesArray[index]), temp.size() + 1, temp.c_str()); } typesArray[things.size()] = 0; HINTERNET requestHandle = WinHttpOpenRequest(m_connectHandle, L"POST", wideResource.c_str(), NULL, WINHTTP_NO_REFERER, typesArray, m_secure? WINHTTP_FLAG_SECURE : NULL); When I compile, I get no errors. However, as I debug through, I get the first string into the array and on the second iteration through the for loop it becomes empty for some reason. If someone can help, but doesn't like looking at the Windows types, here is a snippet what I am trying to do, translated to the best of my ability: const wchar_t ** Convert(std::vector<string> & data) { // What goes here? } -- I have chosen to troll filter/ignore all subthreads containing the words: "Rick C. Hodgins", "Flibble", and "Islam" So, I won't be able to see or respond to any such messages --- |
Christopher Pisz <nospam@notanaddress.com>: Oct 16 11:40AM -0500 On 10/16/2015 10:49 AM, Christopher Pisz wrote: > types, > m_secure? > WINHTTP_FLAG_SECURE : NULL); Here is a more friendly compilable example, where I am just having some problems where the comments are: // Standard Library #include <iostream> #include <string> #include <vector> // Windows Includes #include "Strsafe.h" // We can replace with C strcpy or some such //-------------------------------------------------------------------------------------------------- char ** Convert(const std::vector<std::string> & data) { char ** out = new char *[data.size() + 1]; for(size_t index = 0; index < data.size(); ++index) { const std::string & element = data[index]; out[index] = new char[element.size() + 1]; StringCchCopy(out[index], element.size() + 1, element.c_str()); } out[data.size()] = 0; return out; } //-------------------------------------------------------------------------------------------------- std::vector<std::string> Convert(const char ** data) { std::vector<std::string> out; // What kind of magic goes here? return out; } //-------------------------------------------------------------------------------------------------- int main() { std::vector<std::string> data; data.push_back("String1"); data.push_back("String2"); data.push_back("String3"); char ** yucky = Convert(data); // Cleanup allocated memory // How do we do such a thing? We have to iterate through somehow return 0; } |
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Oct 16 07:47PM +0200 On 10/16/2015 5:49 PM, Christopher Pisz wrote: [snip] > types[1] = L"text/xml"; > types[2] = L"application/json"; > types[3] = 0; [snip] > temp.size() + 1, temp.c_str()); > } > typesArray[things.size()] = 0; Just create a vector of pointers. vector< wchar_t const* > types( things.size() ); for( auto& p : types ) { p = things.c_str(); } Then in the call to some C style function supply &types[0]. That's it, roughly. You do need to be careful with zero size array and lifetimes and modifying operations and such. Cheers & hth., - Alf |
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Oct 16 07:49PM +0200 On 10/16/2015 5:49 PM, Christopher Pisz wrote: > // Ermehgerd Windows API casting to silly string types > std::vector<std::string> things{"text/html", "text/xml", > "application/json"}; Make that vector<wstring>, and use wide string literals. |
mark <mark@invalid.invalid>: Oct 16 08:02PM +0200 On 2015-10-16 18:40, Christopher Pisz wrote: > char ** yucky = Convert(data); > // Cleanup allocated memory > // How do we do such a thing? We have to iterate through somehow Are you hung up on the pointer array syntax? std::vector<std::string> ConvertAndCleanup(char** data) { std::vector<std::string> out; for(char** elem_ptr = data; *elem_ptr != nullptr; ++elem_ptr) { out.emplace_back(std::string(*elem_ptr)); delete[] *elem_ptr; }; delete[] data; return out; } You put a sentinel 0 / nullptr at the end of the char* array that you can check against for the loop termination. |
crisdunbar@gmail.com: Oct 16 11:20AM -0700 On Friday, October 16, 2015 at 8:50:16 AM UTC-7, Christopher Pisz wrote: > { > // What goes here? > } I think this basically does what you want: <code> #include <iostream> #include <string> #include <vector> //-------------------------------------------------------------------------------------------------- std::vector<const char*> Convert(const std::vector<std::string> & data) { std::vector<const char *> out; for(const std::string& s : data) out.push_back(s.c_str()); return out; } void IckyFunction(const char ** in, size_t size) { for(size_t i = 0; i < size; ++i) std::cout << in[i] << std::endl; return; } //-------------------------------------------------------------------------------------------------- int main() { std::vector<std::string> data; data.push_back("String1"); data.push_back("String2"); data.push_back("String3"); std::vector < const char * > out = Convert(data); IckyFunction(out.data(), out.size()); // Cleanup allocated memory // How do we do such a thing? We have to iterate through somehow return 0; } </code> Critiques more than welcome. I use very little C++ and am trying to dabble a little more ... |
Christopher Pisz <nospam@notanaddress.com>: Oct 16 02:35PM -0500 On 10/16/2015 12:47 PM, Alf P. Steinbach wrote: > vector< wchar_t const* > types( things.size() ); > for( auto& p : types ) { p = things.c_str(); } p = things.c_str(); Assigning std::string::c_str() to type char * does not work.. When the string goes out of scope, so does its buffer, I believe. The vector has to have each element point to allocated memory, or where are we going to store the string? Nor does variable 'things' have a c_str() method, it's a vector. So, the for(auto...syntax can't be used, because you need an index, no? -- I have chosen to troll filter/ignore all subthreads containing the words: "Rick C. Hodgins", "Flibble", and "Islam" So, I won't be able to see or respond to any such messages --- |
Christopher Pisz <nospam@notanaddress.com>: Oct 16 02:43PM -0500 > std::vector<const char *> out; > for(const std::string& s : data) > out.push_back(s.c_str());' I don't think the above line is safe, because the string can have its buffer changed or it can go out of scope. -- I have chosen to troll filter/ignore all subthreads containing the words: "Rick C. Hodgins", "Flibble", and "Islam" So, I won't be able to see or respond to any such messages --- |
crisdunbar@gmail.com: Oct 16 02:14PM -0700 On Friday, October 16, 2015 at 12:44:04 PM UTC-7, Christopher Pisz wrote: > > out.push_back(s.c_str());' > I don't think the above line is safe, because the string can have its > buffer changed or it can go out of scope. The line itself is safe. You just have to be aware. The buffer won't change if you don't change the string. The same is true of _any_ use of c_str or iterators or data(), etc., etc. I believe that my usage in my example is perfectly safe. |
Ian Collins <ian-news@hotmail.com>: Oct 17 10:28AM +1300 > I think this basically does what you want: A couple of critiques as requested! > std::vector<const char *> out; > for(const std::string& s : data) > out.push_back(s.c_str()); Could be written for( const auto& s : data) out.push_back(s.c_str()); > data.push_back("String1"); > data.push_back("String2"); > data.push_back("String3"); Could be written std::vector<std::string> data {"String1","String2","String3"}; > std::vector < const char * > out = Convert(data); This one would definitely benefit from auto: const auto out = Convert(data); > IckyFunction(out.data(), out.size()); > // Cleanup allocated memory > // How do we do such a thing? We have to iterate through somehow I don't thank you have allocated any? -- Ian Collins |
legalize+jeeves@mail.xmission.com (Richard): Oct 16 06:52PM [Please do not mail me a copy of your followup] Mark <i@dontgetlotsofspamanymore.net> spake the secret code >using the following which works in many circumstance but a client in >the US has found it not working correctly: >void convertToUTC(const char *input, char *output) C-style string char * arguments and C library functions? Are you sure you're posting to the correct newsgroup? Did you mean comp.lang.c instead? This sort of stuff is *trivial* with boost.datetime. They even have an explicit example demonstrating this exact conversion. <http://www.boost.org/doc/libs/1_59_0/doc/html/date_time/examples.html#date_time.examples.local_utc_conversion> No need to use crufty, error-prone, non-typesafe hacks with sprintf. For well known problems, always prefer a time-tested well known implementation. -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Computer Graphics Museum <http://computergraphicsmuseum.org> The Terminals Wiki <http://terminals.classiccmp.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com> |
scott@slp53.sl.home (Scott Lurndal): Oct 16 01:18PM >first get the pointer returned by sbrk(0), then allocate a bunch of >stuff, and then get a new pointer from sbrk(0), and the difference >between these two pointers is how much the heap has increased.) On linux, /proc/<pid>/maps has the heap size: $ cat /proc/1799/maps 00400000-00561000 r-xp 00000000 fd:00 2061943 /vsim 00760000-00764000 rw-p 00160000 fd:00 2061943 /vsim 00764000-008e4000 rw-p 00000000 00:00 0 00d51000-01f89000 rw-p 00000000 00:00 0 [heap] ... Along with everything else: $ cat /proc/1799/maps | wc -l 311 |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment