How to return char*
-
Hi All, How to reyurn char* in c++? My code is
extern "C" char* callFunction()
{
std::string stroutput = pObject->ProcessNumber("hi","hi");
char* strout = new char [stroutput.size()+1];
strcpy (strout, stroutput.c_str());
return strout;
delete[] strout;}
Thanks in advance...
G.Paulraj
-
Hi All, How to reyurn char* in c++? My code is
extern "C" char* callFunction()
{
std::string stroutput = pObject->ProcessNumber("hi","hi");
char* strout = new char [stroutput.size()+1];
strcpy (strout, stroutput.c_str());
return strout;
delete[] strout;}
Thanks in advance...
G.Paulraj
What you've done is almost correct. 2 things are not right. First, you cannot use
extern "C"
if you're usingstd::string
becausestring
is an object of thebasic_string
class.class
is not understood by C. Second, you cannot delete the memory inside the function. Deletion has to be done by the caller. Also there is no point in having any statements after thereturn
statement.«_Superman_» _I love work. It gives me something to do between weekends.
-
Hi All, How to reyurn char* in c++? My code is
extern "C" char* callFunction()
{
std::string stroutput = pObject->ProcessNumber("hi","hi");
char* strout = new char [stroutput.size()+1];
strcpy (strout, stroutput.c_str());
return strout;
delete[] strout;}
Thanks in advance...
G.Paulraj
The
delete[]
will never execute, because you arereturn
ing before you get to it.The difficult we do right away... ...the impossible takes slightly longer.
-
The
delete[]
will never execute, because you arereturn
ing before you get to it.The difficult we do right away... ...the impossible takes slightly longer.
Richard and Superman, Thanks for your reply. Actaully i supposed to return std::string. The thing is, this is a dll. and i am calling this dll from c# application. If i return like
return "some value";
this is working fine. but if i return like,
char* str; or std::string str;
return str;
the output is not coming correctly. from c# i am calling like
String str = somefunctionname();
How can solve this issue?
G.Paulraj
-
Richard and Superman, Thanks for your reply. Actaully i supposed to return std::string. The thing is, this is a dll. and i am calling this dll from c# application. If i return like
return "some value";
this is working fine. but if i return like,
char* str; or std::string str;
return str;
the output is not coming correctly. from c# i am calling like
String str = somefunctionname();
How can solve this issue?
G.Paulraj
Since you're doing interop, you should return a
BSTR
. So you will need to use theSysAllocString
API on the string to convert it toBSTR
.«_Superman_» _I love work. It gives me something to do between weekends.
-
Hi All, How to reyurn char* in c++? My code is
extern "C" char* callFunction()
{
std::string stroutput = pObject->ProcessNumber("hi","hi");
char* strout = new char [stroutput.size()+1];
strcpy (strout, stroutput.c_str());
return strout;
delete[] strout;}
Thanks in advance...
G.Paulraj
#include char *return_Char_Pointer(char *ptrChar)
{
ptrChar = "hello hi! I am returning a return char pointer (char *p)\n\n";
return ptrChar;
}int main(int argc, char* argv[])
{
char *ptrChar = new char[255];
char *ptrChar1;
ptrChar1 =return_Char_Pointer(ptrChar);printf("%s",ptrChar1); delete \[\]ptrChar; return 0;
}
-
What you've done is almost correct. 2 things are not right. First, you cannot use
extern "C"
if you're usingstd::string
becausestring
is an object of thebasic_string
class.class
is not understood by C. Second, you cannot delete the memory inside the function. Deletion has to be done by the caller. Also there is no point in having any statements after thereturn
statement.«_Superman_» _I love work. It gives me something to do between weekends.
«_Superman_» wrote:
First, you cannot use
extern "C"
if you're usingstd::string
The
extern "C"
merely prevents name decoration of exported function or variable names, it has nothing to do with the underlying C++ code, which may use any classes internally.Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
«_Superman_» wrote:
First, you cannot use
extern "C"
if you're usingstd::string
The
extern "C"
merely prevents name decoration of exported function or variable names, it has nothing to do with the underlying C++ code, which may use any classes internally.Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
You're right. My mistake. :sigh:
«_Superman_» _I love work. It gives me something to do between weekends.
-
You're right. My mistake. :sigh:
«_Superman_» _I love work. It gives me something to do between weekends.
«_Superman_» wrote:
My mistake.
The first I've seen from you; you must be having a bad day! ;)
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
«_Superman_» wrote:
My mistake.
The first I've seen from you; you must be having a bad day! ;)
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
The worst. :omg: !!!EVER!!! Need to cool off :doh:. Going to the bar. :-D
«_Superman_» _I love work. It gives me something to do between weekends.
-
#include char *return_Char_Pointer(char *ptrChar)
{
ptrChar = "hello hi! I am returning a return char pointer (char *p)\n\n";
return ptrChar;
}int main(int argc, char* argv[])
{
char *ptrChar = new char[255];
char *ptrChar1;
ptrChar1 =return_Char_Pointer(ptrChar);printf("%s",ptrChar1); delete \[\]ptrChar; return 0;
}
Are you posting bad code to set the enemy on the wrong track?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
#include char *return_Char_Pointer(char *ptrChar)
{
ptrChar = "hello hi! I am returning a return char pointer (char *p)\n\n";
return ptrChar;
}int main(int argc, char* argv[])
{
char *ptrChar = new char[255];
char *ptrChar1;
ptrChar1 =return_Char_Pointer(ptrChar);printf("%s",ptrChar1); delete \[\]ptrChar; return 0;
}
-
Hi All, How to reyurn char* in c++? My code is
extern "C" char* callFunction()
{
std::string stroutput = pObject->ProcessNumber("hi","hi");
char* strout = new char [stroutput.size()+1];
strcpy (strout, stroutput.c_str());
return strout;
delete[] strout;}
Thanks in advance...
G.Paulraj
-
I am surprised this will compile given the 'delete' is unreachable. :) Anyway, just remove the delete and return strout to the caller. It is then the callers responsibility to free the memory.
============================== Nothing to say.
-
Richard and Superman, Thanks for your reply. Actaully i supposed to return std::string. The thing is, this is a dll. and i am calling this dll from c# application. If i return like
return "some value";
this is working fine. but if i return like,
char* str; or std::string str;
return str;
the output is not coming correctly. from c# i am calling like
String str = somefunctionname();
How can solve this issue?
G.Paulraj
First of all, if your function is part of a DLL called from outside that DLL, then memory allocated inside your function may not be released by the caller! The reason is that the caller may not use the same memory mapping and therefore cannot manipulate the heap form your DLL. There are various ways around that: 1. the easiest is to provide a release function in addition to your original one. The only pupose of that function is to release the buffer you previously allocated, and the caller of your function should call it once it doesn't need your string anymore. 2. A somewhat better solution, and in fact widely used, is to add another parameter to your function: a string buffer that the caller must pass to your DLL, so cou can copy the resulting string into it. Of course you must verify the buffer is big enough or else issue an appropriate error or warning. You couls also provide an additional function that simply returns the size you require the buffer to be, so the caller could first call this function, then allocate the memory and pass the resulting buffer to your function. 3. There are other ways, such as using Shared memory, but I think that would be overkill. Here's a suggestion based on variant 2 above:
// yourfile.h
extern "C" std::size_t requiredSize();
extern "C" std::size_t getString(char* buffer);// yourfile.cpp
char mystring[] = "hello world"; // just an example
std::size_t requiredSize() {
return sizeof(mystring) / sizeof(char); // an upper limit to the size required for getString
}
std::size_t getString(char* buffer) {
if (!buffer) // error: no buffer provided!
return 0; // response: 0 bytes copied
strcpy(buffer, mystring); // hope for the best and just copy
return requiredSize(); // return the length of the copied string
}And here's how to use it from C++:
#include "yourfile.h"
void foo() {
std::size_t sz = requiredSize(); // inquire required size
char* mybuffer = new char[sz]; // allocate a sufficiently large buffer
getString(buffer); // get the string
puts(buffer); // do something with the string (in this case, print it)
delete [] buffer; // release the buffer
} -
I am surprised this will compile given the 'delete' is unreachable. :) Anyway, just remove the delete and return strout to the caller. It is then the callers responsibility to free the memory.
============================== Nothing to say.
The problem with that is: 1. The caller may not know what method was used to allocate the memory (e. g. malloc or new), or if it was allocated at all, or instead just points to a static buffer! (Of course you could put that kind of information into the function documentation) 2. The OP put this function into a library interface. If bound statically, that would not pose a problem, but if bound dynamically, as a DLL, this DLL may use a separate heap - that means the caller may not even be able to release that memory. At least that's my understanding of DLLs.
-
The problem with that is: 1. The caller may not know what method was used to allocate the memory (e. g. malloc or new), or if it was allocated at all, or instead just points to a static buffer! (Of course you could put that kind of information into the function documentation) 2. The OP put this function into a library interface. If bound statically, that would not pose a problem, but if bound dynamically, as a DLL, this DLL may use a separate heap - that means the caller may not even be able to release that memory. At least that's my understanding of DLLs.
Stefan_Lang wrote:
The caller may not know what method was used to allocate the memory (e. g. malloc or new), or if it was allocated at all, or instead just points to a static buffer!
Thats what documentation is for. This is actually quite common procedure, many APIs do this.
Stefan_Lang wrote:
The OP put this function into a library interface
Then the dll can expose a func that the caller can call to do the delete.
============================== Nothing to say.