Results 1 to 6 of 6

Thread: Pointing Two Dimensional Pointer to return value

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Pointing Two Dimensional Pointer to return value

    Hi
    I want two dimensional pointer point to a return value.
    I have done this as below

    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.

    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.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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... )


    What you want to achieve is surprisingly hard too do.
    (It might be easier to pass argc/argv from main().)
    Qt Code:
    1. QList<QByteArray> arr; // no tempory QByteArrays - the data() pointers must remain valid
    2. std::vector<char*> v_args; // store arbitrary number of pointers in contiguous memory (like a C array)
    3. foreach(const QString &s, QCoreApplication::arguments())
    4. {
    5. arr << s.toLatin1();
    6. v_args.push_back( arr.back().data() );
    7. }
    8.  
    9. // vector has contig. memory, thus we can use the address of the first entry
    10. // (yes, this code assumes that the number of arguments is not zero; which is quite safe as the program name is the first arg)
    11. char **argv = &v_args[0];
    To copy to clipboard, switch view to plain text mode 
    (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.

  3. The following user says thank you to caduel for this useful post:

    babu198649 (5th March 2009)

  4. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: Pointing Two Dimensional Pointer to return value

    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.

    It might be easier to pass argc/argv from main()
    The third party library is used in a function other than the main function.

  5. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Pointing Two Dimensional Pointer to return value

    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.

  6. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default 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()))

  7. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.)

Similar Threads

  1. error with QList with a class
    By john_god in forum Newbie
    Replies: 7
    Last Post: 12th January 2009, 21:48
  2. QTableView performances
    By miraks in forum Qt Programming
    Replies: 18
    Last Post: 1st December 2008, 10:25
  3. Return a pointer from a QItemdelegate
    By SailinShoes in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2008, 20:07
  4. Once more : QAbstractItemModel woes
    By Valheru in forum Qt Programming
    Replies: 10
    Last Post: 15th January 2008, 10:44
  5. QTableView Repaint/Refresh
    By millsks in forum Newbie
    Replies: 9
    Last Post: 10th January 2008, 17:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.