This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it a good idea to return “ const char * ” from a function?
how to return char array in c++?
What is wrong with this return? I'm trying to return the current path using the following function but it doesn't seems to be correct:
Please Not: I need an char return not string.
char* getINIfile(void)
{
char buffer[MAX_PATH];
GetModuleFileName( NULL, buffer, MAX_PATH );
string::size_type pos = string( buffer ).find_last_of( "\\/" );
string path = string( buffer ).substr( 0, pos) + "\\setup.ini";
char *ini_local= (char*)path.c_str();
printf(ini_local); // so far output OK!
return ini_local;
}
main
{
printf(getINIfile()); // output Not OK!
char mybuffer[200];
GetPrivateProfileStringA( "files","DLL","0", mybuffer,200, getINIfile());
printf(mybuffer);
}
path goes out of scope at the end of the function and you are returning an internal pointer in that out of scope object. try returning an std::string instead
std::string getINIfile(void)
{
char buffer[MAX_PATH];
GetModuleFileName( NULL, buffer, MAX_PATH );
string::size_type pos = string( buffer ).find_last_of( "\\/" );
string path = string( buffer ).substr( 0, pos) + "\\setup.ini";
char *ini_local= (char*)path.c_str();
printf(ini_local); // so far output OK!
return path;
}
You're returning an address that goes out of scope when the function exits, and so it's no longer valid: std::string path is local to the function getINIFile and so it's invalid after the function exits, as is the address that you get from path.c_str().
In this case you can just return the std::string from your function. If you really need a C string later, you can use c_str() then:
std::string getINIfile(void)
{
//...
return path;
}
int main()
{
string path = getINIFile();
// do something with path.c_str():
const char *cPath = path.c_str();
}
Given your code I can't think of any reason that you must have a char* return, but if so you'll need to allocate a buffer on the heap:
char *getINIfile(void)
{
char *buffer[MAX_PATH];
GetModuleFileName(NULL, buffer, MAX_PATH);
string::size_type pos = string(buffer).find_last_of( "\\/" );
string path = string(buffer).substr( 0, pos) + "\\setup.ini";
char *ini_local = new[path.size()];
strncpy(ini_local, path.c_str(), path.size());
printf(ini_local); // so far output OK!
return ini_local;
}
But this is a really awful mix of standard C strings and std::string: just using string to manipulate the path and passing around char* everywhere else.
Using only standard C, replacing find_last_of with strrchr - note the lack of error handling:
char *getINIfile(void)
{
char *buffer = new[MAX_PATH];
char *pos = NULL;
char *ini_local = NULL;
GetModuleFileName(NULL, buffer, MAX_PATH);
pos = strrchr(buffer, "\\/");
// check for and handle pos == NULL
buffer[pos] = '\0';
strncat(buffer, "\\setup.ini", MAX_PATH - strlen(buffer));
printf(buffer);
return buffer;
}
The function is returning a pointer to a local variable, which goes out of scope, leaving you with a dangling pointer. Why not just return an std::string by value?
std::string getINIfile() {
....
return path;
}
Then you can just use the string's underlying char* on the caller side:
const std::string s = getINIfile();
const char* c = s.c_str();
Related
This is working for me...
std::string GetProgramDataPath() {
CHAR path[MAX_PATH];
HRESULT hr = SHGetFolderPathA(nullptr, CSIDL_COMMON_APPDATA, nullptr, 0, path); // path accepted as LPSTR parameter?
if (SUCCEEDED(hr)) {
return std::string(path); // then automatically cast to const char*?
}
else {
return std::string();
}
}
...but I don't know why. I try to pass LPSTR, but I get:
Error C4700 "uninitialized local variable 'path' used"
I look up how to initialize LPSTR and come up with this:
std::string GetProgramDataPath() {
LPSTR path = new CHAR[MAX_PATH];
HRESULT hr = SHGetFolderPathA(nullptr, CSIDL_COMMON_APPDATA, nullptr, 0, path);
if (SUCCEEDED(hr)) {
std::string strPath(path);
delete[] path;
return std::string(strPath);
}
else {
delete[] path;
return std::string();
}
}
Is this the 'correct' code? With new and delete it seems wrong. Am I doing something unsafe by just using CHAR[]? How come it works instead of LPSTR? I believe it has something to do with the "equivalence of pointers and arrays" in C, but it seems there are some automatic conversions from CHAR[] to LPSTR to const char * in this code I don't understand.
Instead of managing the memory your self with new and delete I'd use a std::string instead and let it manage the memory.
static std::string GetProgramDataPath()
{
std::string buffer(MAX_PATH, '\0');
const HRESULT result = SHGetFolderPathA
(
nullptr,
CSIDL_COMMON_APPDATA,
nullptr,
0,
buffer.data()
);
if (SUCCEEDED(result))
{
// Cut off the trailing null terminating characters.
// Doing this will allow you to append to the string
// in the position that you'd expect.
if (const auto pos{ buffer.find_first_of('\0') }; pos != std::string::npos)
buffer.resize(pos);
// Here is how you can append to the string further.
buffer.append(R"(\Some\Other\Directory)");
return buffer;
}
buffer.clear();
return buffer;
}
Here is one way you could do it using std::filesystem::path and SHGetKnownFolderPath.
namespace fs = std::filesystem;
static fs::path GetProgramDataPath()
{
struct buffer {
wchar_t* data{ nullptr };
~buffer() { CoTaskMemFree(data); }
} buf{};
const HRESULT result = SHGetKnownFolderPath
(
FOLDERID_ProgramData,
0,
nullptr,
&buf.data
);
return SUCCEEDED(result)
? fs::path{ buf.data }
: fs::path{};
}
int main()
{
fs::path path{ GetProgramDataPath() };
if (!path.empty())
{
// Here is one way you can append to a path.
// You can also use the append member function as well.
path /= R"(Some\Other\Directory)";
// When you're ready you can call either the generic_string or
// string member function on the path.
const std::string s1{ path.string() };
const std::string s2{ path.generic_string() };
// Prints: 'C:\ProgramData\Some\Other\Directory'.
std::cout << s1 << '\n';
// Prints: 'C:/ProgramData/Some/Other/Directory'.
std::cout << s2 << '\n';
}
}
This is working for me...but I don't know why.
LPSTR is just an alias for CHAR* (aka char*):
typedef CHAR *LPSTR;
In certain contexts, a fixed-sized CHAR[] (aka char[]) array will decay into a CHAR* (aka char*) pointer to its 1st element, such as when passing the array by value in a function parameter, as you are doing.
I try to pass LPSTR, but I get Error C4700 "uninitialized local variable 'path' used".
Because LPSTR is just a pointer, and you likely did not point it at anything meaningful.
Is this the 'correct' code?
Technically yes, that will work (though return std::string(strPath) should be return strPath instead). However, you should consider using std::string or std::vector<char> instead to manage memory for you, don't use new[]/delete[] directly, eg:
std::string GetProgramDataPath() {
std::vector<char> path(MAX_PATH);
HRESULT hr = SHGetFolderPathA(nullptr, CSIDL_COMMON_APPDATA, nullptr, 0, path.data());
if (SUCCEEDED(hr)) {
return std::string(path.data());
}
return std::string();
}
Am I doing something unsafe by just using CHAR[]?
No.
How come it works instead of LPSTR?
Because CHAR[] decays into the same type that LPSTR is an alias of.
it seems there are some automatic conversions from CHAR[] to LPSTR to const char * in this code.
Correct.
I use a function s2ws() (search from the SO,if you find something wrong please let me know)convert from string to wstring,then I use tinyxml2 to read something from xml.As we all know ,some of tinyxml2 interface use char * as input so does the return value.
The reason why convert from string to wstring is the project all using wchar_t types to deal with string.
/*
string converts to wstring
*/
std::wstring s2ws(const std::string& src)
{
std::wstring res = L"";
size_t const wcs_len = mbstowcs(NULL, src.c_str(), 0);
std::vector<wchar_t> buffer(wcs_len + 1);
mbstowcs(&buffer[0], src.c_str(), src.size());
res.assign(buffer.begin(), buffer.end() - 1);
return res;
}
/*
wstring converts to string
*/
std::string ws2s(const std::wstring & src)
{
setlocale(LC_CTYPE, "");
std::string res = "";
size_t const mbs_len = wcstombs(NULL, src.c_str(), 0);
std::vector<char> buffer(mbs_len + 1);
wcstombs(&buffer[0], src.c_str(), buffer.size());
res.assign(buffer.begin(), buffer.end() - 1);
return res;
}
The ClassES-Attribute will return char *,funciton s2ws will convert string to wstring. These two ways got different result in map m_UpdateClassification. The second method is between #if 0 and #endif. But I thinks these two ways should make no difference.
The second method will got empty string after convert,can not figure out why,If you have any clue,please let me know.
typedef std::map<std::wstring, std::wstring> CMapString;
CMapString m_UpdateClassification;
const wchar_t * First = NULL;
const wchar_t * Second = NULL;
const char *name = ClassES->Attribute( "name" );
const char *value = ClassES->Attribute( "value" );
std::wstring wname = s2ws(name);
std::wcout<< wname << std::endl;
First = wname.c_str();
std::wstring wvalue = s2ws(value);
std::wcout<< wvalue << std::endl;
Second = wvalue.c_str();
#if 0
First = s2ws(ClassES->Attribute( "name" )).c_str();
if( !First ) { m_ProdectFamily.clear(); return FALSE; }
Second = s2ws(ClassES->Attribute( "value" )).c_str();
if( !Second ) { m_ProdectFamily.clear(); return FALSE; }
#endif
m_UpdateClassification[Second] = First;
I think I found the reason,I assgin wchar_t * to wstring,After modfiy code like this,everything run well.
std::wstring First = L"";
std::wstring Second = L"";
First = s2ws(ClassES->Attribute("name"));
if( First.empty() ) { m_ProdectFamily.clear(); return FALSE; }
Second = s2ws(ClassES->Attribute("value"));
if( Second.empty() ) { m_ProdectFamily.clear(); return FALSE; }
Another question,Should I check the result of s2ws(mbstowcs) ws2s(wcstombs)?
I am a beginner cpp programmer. I am converting string value to LPCWSTR. When i am trying to access this value, it is giving a null value. Please check this code attached below. I think this is because of memory reference value is clearing out after the scope of the variable.
std::wstring string2wString(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
void main(){
string str1,str2,str3;
wstring wStr1;
LPCWSTR lpStr1[MAX_PATH];
int index=0;
for(int i=0;i<iLimit;i++)
{
str1="String 1";
//do operations
for(int j=0;j<jLimit;j++)
{
// do operations
str2=" String 2";
str3= str1+str2;
wStr1= string2wString(str3); //converting to wstring
lpStr1[index]=wStr1.c_str();
index++
}
}
cout << lpStr1[0] << endl;
}
Please help me to resolve this issue.
The pointer returned by wStr1.c_str() may become invalid when wStr1 is later modified.
The best fix is to stick to C++ types:
std::wstring strings[MAX_PATH];
// ...
MultiByteToWideChar(CP_ACP, 0, str3.c_str(), slength, buf, len);
strings[index] = buf;
delete[] buf;
// ...
or, you could postpone deleting the buffer and just use it in your array:
LPCWSTR lpStr1[MAX_PATH];
// ...
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, str3.c_str(), slength, buf, len);
lpStr1[index] = buf;
There are a few problems here:
LPCWSTR lpStr1[MAX_PATH]; is defining an array of pointers to const wchar_t, not an array of const wchar_t as you no doubt intend.
lpStr1[index]=wStr1.c_str(); is storing the pointer to the temporary buffer returned by c_str(). This does not copy the string into lpStr[index].
I'm not sure what iLimit and jLimit are, but I don't see what those loops are intending to accomplish if you really only want to convert a string value to a wide character array.
I'd suggest using the following routine for UNICODE conversion:
wstring AsciiToUtf16(const string & str)
{
if (str.empty())
return wstring();
size_t charsNeeded = ::MultiByteToWideChar(CP_ACP, 0,
str.data(), (int)str.size(), NULL, 0);
if (charsNeeded == 0)
throw runtime_error("Failed converting ASCII string to UTF-16");
vector<wchar_t> buffer(charsNeeded);
int charsConverted = ::MultiByteToWideChar(CP_ACP, 0,
str.data(), (int)str.size(), &buffer[0], buffer.size());
if (charsConverted == 0)
throw runtime_error("Failed converting ASCII string to UTF-16");
return wstring(&buffer[0], charsConverted);
}
Why not follow this:
C++ Convert string (or char*) to wstring (or wchar_t*)
and forget the LPCWSTR altogether?
I am trying to hash a string then use that hash as the name of a file.
My problem: is that all the C++ hashers/crypters I have come across hash a std::string or char* & return the hashed string as an unsigned char*?
How do I convert that unsigned char* to a char* or std::string so I can then write it to a file or filename? Or do I not need to convert it to a normal string to use it?
tstring hashString( tstring str )
{
// Post:
unsigned char hashStr[SHA256_DIGEST_SIZE];
std::string messageStr = str;
SHA256::getInstance()->digest( messageStr, hashStr );
//TCHAR *hashStrSigned = reinterpret_cast <TCHAR*>(hashStr);
// can I just use this hashStr to create a file? Or do I have to convert it to char* to use?
Handle newF = CreateFile( (LPTSTR)hashStr, GENERIC_ALL, 0, NULL, CREATE_ALWAYS,
0, NULL );
return tstring(hashStrSigned);
}
The result of the hash likely contain bytes that map to chars that are not allowed in a filename (e.g. null-char, '+', '?', etc....). So "hashStr" in your code isn't likely to be a string, but just an array of bytes that aren't null terminated.
Try this little function to convert from "binary hash to string suitable for file name"
void HashToString(unsigned char* shaHash, std::string* pStr)
{
char szHash[SHA256_DIGEST_SIZE*2+1];
char* pszOut = szHash;
for (int x = 0; x < SHA256_DIGEST_SIZE; x++)
{
sprintf(pszOut, "%.2X", shaHash[x]); // write out as hex chars
pszOut += 2; // advance 2 chars
}
*pszOut = '\0'; // null terminate
*pStr = std::string(szHash);
}
In your above code example, you'd call it as follows:
std::string str;
HashToString(hashStr, &str);
HANDLE newF = CreateFileA(str.c_str(), ...);
I have written a read function which reads values from serial port(LINUX) . It returns values as pointer to char . I am calling this function in another function and storing it again in a variable as pointer to char . I occasionally got stack over flow problem and not sure if this function is creating problem.
The sample is provided below. Please give some suggestions or criticism .
char *ReadToSerialPort( )
{
const int buffer_size = 1024;
char *buffer = (char *)malloc(buffer_size);
char *bufptr = buffer;
size_t iIn;
int iMax = buffer+buffer_size-bufptr;
if ( fd < 1 )
{
printf( "port is not open\n" );
// return -1;
}
iIn = read( fd, bufptr, iMax-1 );
if ( iIn < 0 )
{
if ( errno == EAGAIN )
{
printf( "The errror in READ" );
return 0; // assume that command generated no response
}
else
printf( "read error %d %s\n", errno, strerror(errno) );
}
else
{
// *bufptr = '\0';
bufptr[(int)iIn<iMax?iIn:iMax] = '\0';
if(bufptr != buffer)
return bufptr;
}
free(buffer);
return 0;
} // end ReadAdrPort
int ParseFunction(void)
{
// some other code
char *sResult;
if( ( sResult = ReadToSerialPort()) >= 0)
{
printf("Response is %s\n", sResult);
// code to store char in string and put into db .
}
}
Thanks and regards,
SamPrat
You do not deallocate the buffer. You need to make free after you finished working with it.
char * getData()
{
char *buf = (char *)malloc(255);
// Fill buffer
return buf;
}
void anotherFunc()
{
char *data = getData();
// Process data
free(data);
}
In your case I think you should free the buffer after printf:
if( ( sResult = ReadToSerialPort()) >= 0)
{
printf("Response is %s\n", sResult);
// code to store char in string and put into db .
free(sResult);
}
UPDATE Static buffer
Another option to use static buffers. It could increase performance a little bit, but getData method will be not a thread-safe.
char buff[1024];
char *getData()
{
// Write data to buff
return buff;
}
int main()
{
char *data = getData();
printf("%s", data);
}
UPDATE Some notes about your code
int iMax = buffer+buffer_size-bufptr; - iMax will always be 1024;
I do not see any idea of using bufptr since its value is the same as buffer and you do not change it anywhere in your function;
iIn = read( fd, bufptr, buffer_size-1 );
You can replace bufptr[(int)iIn<iMax?iIn:iMax] = '\0'; with bufptr[iIn] = '\0';
if(bufptr != buffer) is always false and this is why your pointer is incorrect and you always return 0;
Do not forget to free the buffer if errno == EAGAIN is true. Currently you just return 0 without free(buffer).
Good luck ;)
Elalfer is partially correct. You do free() your buffer, but not in every case.
For example, when you reach if ( errno == EAGAIN ) and it evaluates to true, you return without doing free on your buffer.
The best would be to pass the buffer as a parameter and make it obvious that the user must free the buffer, outside the function. (this is what basically Elalfer sais in his edited answer).
Just realized this is a C question, I blame SO filtering for this :D sorry! Disregard the following, I'm leaving it so that comments still make sense.
The correct solution should use std::vector<char>, that way the destructor handles memory deallocation for you at the end of scope.
what is the purpose of the second pointer?
char *buffer = (char *)malloc(buffer_size);
char *bufptr = buffer;
what is the purpose of this?
int iMax = buffer+buffer_size-bufptr; // eh?
What is the purpose of this?
bufptr[(int)iIn<iMax?iIn:iMax] = '\0'; // so you pass in 1023 (iMax - 1), it reads 1023, you've effectively corrupted the last byte.
I would start over, consider using std::vector<char>, something like:
std::vector<char> buffer(1500); // default constructs 1500 chars
int iRead = read(fd, &buffer[0], 1500);
// resize the buffer if valid
if (iRead > 0)
buffer.resize(iRead); // this logically trims the buffer so that the iterators begin/end are correct.
return buffer;
Then in your calling function, use the vector<char> and if you need a string, construct one from this: std::string foo(vect.begin(), vect.end()); etc.
When you are setting the null terminator "bufptr[(int)iIn
bufptr[iMax]=>bufptr[1024]=>one byte beyond your allocation since arrays start at 0.
Also int this case "int iMax = buffer+buffer_size-bufptr;" can be re-written as iMax = buffer_size. It makes the code less readable.