Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are (at least) two common conventions when a <code>char*</code> is returned by a function. You cannot tell which is in force without reading the documentation of the function.</p> <ol> <li>The function returns a pointer to statically allocated memory. In which case the caller does not need to deallocate it.</li> <li>The function returns a pointer to heap allocated memory. In which case the caller does need to deallocate it. The documentation for the function must specify how the caller must deallocate the function (free, delete etc.)</li> </ol> <p>Now, since you are in charge of writing your own functions, you can choose whatever protocol you like. And in your case you should not return a <code>char*</code> from your functions. Choose a third way. Return a <code>std::string</code> and let the standard library take care of allocation and deallocation. Do it this way to make life easier for the consumer of the library.</p> <p>In fact, since you are writing C++, you should be shunning <code>char*</code>. Sure you have to use C string when interacting with the Windows API. But leave it at that. Don't pass the pain on to the consumer of your library. Hide that complexity away.</p> <p>In your situation I would make sure that you have a function that can combine two <code>std::string</code> instances. This could perhaps be implemented using <code>PathAppend</code>, but it's easy enough to roll your own. Then the only interaction you need with the Windows API is a function that returns the temporary directory in a string. That looks like this:</p> <pre><code>string GetTempDir() { char buff[MAX_PATH+1]; DWORD count = GetTempPath(MAX_PATH+1, buff); // I've omitted error checking return string(buff, count); } </code></pre> <p>The code in this function is the only code that needs to deal with C strings. You can now forget all about them in the rest of your code which can treat this as a black box. Don't let the implementation details of the lowest common denominator C interface of Win32 leak into your nicely factored C++ code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload