ByteArray manipulation issue
-
@J-Hilk said in ByteArray manipulation issue:
QByteArray *ba(newQByteArray("Sample output"));
return ba->constData();
Thanks for your quick response. Here QByteArray class provides an array of bytes.QByteArray can embed '\0' bytes. QByteArray makes a deep copy of the const char * data, so you can modify it later without experiencing side effects.
Do you mean to use this way to avoid memory leak issue?#include <QByteArray>
const char* getString()
{
QByteArray *ba(newQByteArray("Sample output"));
return ba->constData();
}
int main(int argc, char *argv[])
{
const char *str = getString();
printf("%s\n", str);
delete str;return 0;
}
@Joseph22121 said in ByteArray manipulation issue:
QByteArray makes a deep copy of the const char * data
No, it does not! Read https://doc.qt.io/qt-5/qbytearray.html#constData
"The pointer remains valid as long as the byte array isn't reallocated or destroyed" - and this is exactly what is happening here! -
so how to fix the issue here?
Below is what I thought to do. Is it correct?
const char* getString()
{
QByteArray ba("Sample output"); // creates byte array of data
return strdup(ba.constdata()); // this will do the copy string to heap allocated memory
} -
so how to fix the issue here?
Below is what I thought to do. Is it correct?
const char* getString()
{
QByteArray ba("Sample output"); // creates byte array of data
return strdup(ba.constdata()); // this will do the copy string to heap allocated memory
}@Joseph22121
I guess you could, but why are you doing any of this withQByteArray? Your goal seems to be to return achar *pointing to"Sample output", you can do that without going anywhere near Qt classes or the complications you are introducing.Also
const char *str = getString(); delete str;I don't think/know that you can
deleteaconst char *. In any case, you must notdeletesomething allocated viastrdup(), which usesmalloc(). -
Do you mean this code is fine? I have avoided using QByteArray here.
const char* getString()
{
return "Sample output";
} -
Do you mean this code is fine? I have avoided using QByteArray here.
const char* getString()
{
return "Sample output";
}@Joseph22121
Sure! If you just want to return some constant string, for whatever reason. Certainly so far you have nothing which needs aQByteArray, unless your real code wants to do something different from what you show. -
Can I conclude the fix for the code as below?
#include <QByteArray>
const char* getString()
{
return "Sample output";
}int main()
{
const char *str = getString(); printf("%s\n", str); return 0;}
-
I think using QByteArray object ba is not required here. Have modified the code as follows. Please review it.
#include <QByteArray>const char* getstring()
{
static const char *const MY_STRING = "Sample output"; // its a const string in static memory
return MY_STRING;
}int main()
{
const char *str = getString(); printf("%s\n", str); return 0;}
-
I think using QByteArray object ba is not required here. Have modified the code as follows. Please review it.
#include <QByteArray>const char* getstring()
{
static const char *const MY_STRING = "Sample output"; // its a const string in static memory
return MY_STRING;
}int main()
{
const char *str = getString(); printf("%s\n", str); return 0;}
@Joseph22121 Yes, static variable is one of the solutions suggested by @J-Hilk ...
-
I think using QByteArray object ba is not required here. Have modified the code as follows. Please review it.
#include <QByteArray>const char* getstring()
{
static const char *const MY_STRING = "Sample output"; // its a const string in static memory
return MY_STRING;
}int main()
{
const char *str = getString(); printf("%s\n", str); return 0;}
@Joseph22121 said in ByteArray manipulation issue:
const char* getstring()
{
static const char *const MY_STRING = "Sample output"; // its a const string in static memory
return MY_STRING;
}You are welcome to write it like this if it makes you happy/reads clearer. But just be aware this does nothing different from plain:
const char* getstring() { return "Sample output"; // this is also a const string in static memory! } -
@Joseph22121 said in ByteArray manipulation issue:
const char* getstring()
{
static const char *const MY_STRING = "Sample output"; // its a const string in static memory
return MY_STRING;
}You are welcome to write it like this if it makes you happy/reads clearer. But just be aware this does nothing different from plain:
const char* getstring() { return "Sample output"; // this is also a const string in static memory! }@JonB Thanks everyone for your inputs and suggestions. This thread can be closed.
-
@JonB Thanks everyone for your inputs and suggestions. This thread can be closed.
@Joseph22121 said in ByteArray manipulation issue:
This thread can be closed.
You can do this with the Topic Tools in your first post.