Pointing Two Dimensional Pointer to return value
Hi
I want two dimensional pointer point to a return value.
I have done this as below
Quote:
char **argv;
char *temp = QCoreApplication::applicationFilePath().toAscii(). data(); //returns char*
argv = &temp;
If temporary pointer variable(char *temp) has to be eliminated, it can be done using type casting as below.
Quote:
char **argv = (char**)QCoreApplication::applicationFilePath().to Ascii().data();
Is there a clean way to do the same without using temporary pointer variable and type casting.
Re: Pointing Two Dimensional Pointer to return value
the first problem here is the temporary QByteArray... your pointer is invalid (on the next line)!
the next problem is that char** means an array of char* (in the context of main), not the address of some string (char* in C).
Your code will therefore produce doubly undetermined fun...
(sorry, but the code is just about as wrong as it can get in so few lines... :p)
What you want to achieve is surprisingly hard too do.
(It might be easier to pass argc/argv from main().)
Code:
QList<QByteArray> arr; // no tempory QByteArrays - the data() pointers must remain valid
std::vector<char*> v_args; // store arbitrary number of pointers in contiguous memory (like a C array)
{
arr << s.toLatin1();
v_args.push_back( arr.back().data() );
}
// vector has contig. memory, thus we can use the address of the first entry
// (yes, this code assumes that the number of arguments is not zero; which is quite safe as the program name is the first arg)
char **argv = &v_args[0];
(All this is completely untested, of course. But it should work that way.)
Lesson learned: what out whenever you want to get a char* from a QString. The danger of temporaries and dangling pointers is very real.
Re: Pointing Two Dimensional Pointer to return value
Quote:
the first problem here is the temporary QByteArray... your pointer is invalid
Thanks for alarming.
I am using Third Party library(Code Sourcery VSIPL++) which uses the application file path to setup the memory allocator,But surprisingly my app works fine with the code i posted.
Quote:
It might be easier to pass argc/argv from main()
The third party library is used in a function other than the main function.
Re: Pointing Two Dimensional Pointer to return value
Quote:
Thanks for alarming.
I am using Third Party library(Code Sourcery VSIPL++) which uses the application file path to setup the memory allocator,But surprisingly my app works fine with the code i posted.
You're welcome.
The problem with undefined behaviour (like accessing no longer valid memory) is that it might work, until you change something totally unrelated, bring the code to a different machine, have a newer Qt version, another gcc...
So, even if you have been unlucky and got no crash: do fix it.
Re: Pointing Two Dimensional Pointer to return value
Does this below macro(stolen from Qwt3d example :) ) does the same job.
#define QWT3DLOCAL8BIT(qstring) ((const char*)(qstring.toLocal8Bit()))
Re: Pointing Two Dimensional Pointer to return value
Well, an array of c-strings is a different thing than the adress of a c-string.
this will work, if you (resp. the code you are passing this to) are only interested in the first item (ie argv[0]). It is dirty anyway.
(I'd rather store argc/argv either in global variables or make it accessible via some api.)