ByteArray manipulation issue
-
What is wrong with my code and how to fix it?
#include <QByteArray> const char* getString() { QByteArray ba("Sample output"); return ba.constData(); } int main(int argc, char *argv[]) { const char *str = getString(); printf("%s\n", str); return 0; } -
What is wrong with my code and how to fix it?
#include <QByteArray> const char* getString() { QByteArray ba("Sample output"); return ba.constData(); } int main(int argc, char *argv[]) { const char *str = getString(); printf("%s\n", str); return 0; }https://doc.qt.io/qt-5/qbytearray.html#constData
The pointer remains valid as long as the byte array isn't reallocated or destroyed.
the byte array is destroyed as soon as the code reaches
}therefore the returned pointer becomes invalid -
Thanks for your quick comments. I am beginner to Qt APIs. How to fix this code? Please advise me here.
-
Thanks for your quick comments. I am beginner to Qt APIs. How to fix this code? Please advise me here.
@Joseph22121 said in ByteArray manipulation issue:
Thanks for your quick comments. I am beginner to Qt APIs. How to fix this code? Please advise me here.
well, thats not really a Qt issue but a C++ one
you have to make your data persistent.
- make the byte array a member variable
- make it static //bad idea
- memcpy it to heap allocated char array
- return the byte array instead of a pointer to the internal data
- ask yourself do you really want/need printf or could you simply use something else, maybe QDebug
-
@Joseph22121 said in ByteArray manipulation issue:
Thanks for your quick comments. I am beginner to Qt APIs. How to fix this code? Please advise me here.
well, thats not really a Qt issue but a C++ one
you have to make your data persistent.
- make the byte array a member variable
- make it static //bad idea
- memcpy it to heap allocated char array
- return the byte array instead of a pointer to the internal data
- ask yourself do you really want/need printf or could you simply use something else, maybe QDebug
@J-Hilk Do you mean to say to dynamically allocate the memory using new operator in c++?
-
@J-Hilk Do you mean to say to dynamically allocate the memory using new operator in c++?
@Joseph22121 this is clearly a broken down example of your actual issue. And as long as I don't know your real situation, I can not recommend a solution for your.
You could simply new the QByteArray, yes, but that will cause for this simple function:
const char* getString() { QByteArray *ba(newQByteArray("Sample output")); return ba->constData(); }a memory leak
why do you go the way over the byte array anyway?
const char* getString() { return "Sample output"; } int main() { const char *str = getString(); printf("%s\n", str); delete str; return 0; } -
@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;
}
-
@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.