Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get the size of Data pointed by a list of pointers
Forum Updated to NodeBB v4.3 + New Features

Get the size of Data pointed by a list of pointers

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ahmed kato
    wrote on 8 Jan 2013, 17:37 last edited by
    #1

    I have this code:

    @char * str1 = "hello world !";

    QList<char *> *list = new QList<char *>;
    list->append(str1);
    
    qDebug()<<list->at(0);
    char *tmp = list->at(0);
    qDebug()<<sizeof(tmp);@
    

    the output is:

    bq. hello world !
    8

    where 8 is the size of the pointer tmp, how could I get the size of data pointed by tmp ??

    Ahmed Kato
    Computer & communications student
    Intern and MSP at Microsoft

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 8 Jan 2013, 18:00 last edited by
      #2

      Dunno why you still use annoying C style strings but you just need to use the usual strlen function to get it:
      @
      char * str1 = "hello world !";

          QList<char *> *list = new QList<char *>;
          list->append(str1);
       
          qDebug()<<list->at(0);
          char *tmp = list->at(0);
          qDebug()<<sizeof(tmp);
      

      char *pEnd;
      for (pEnd = tmp; *pEnd !='\0'; pEnd++)
      continue;
      qDebug()<< pEnd - tmp;
      @

      But it would be easier (even if slower) to use QString:
      @
      char * str1 = "hello world !";

          QList<char *> *list = new QList<char *>;
          list->append(str1);
       
          qDebug()<<list->at(0);
          char *tmp = list->at(0);
          qDebug()<<sizeof(tmp);
          qDebug()<<QString(tmp).size();
      

      @

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on 8 Jan 2013, 18:38 last edited by
        #3

        In C/C++ it generally is not possible to know the size of a memory buffer (array) only from the pointer. The pointer really is only the address where the buffer/array starts. Not more! If the array is of type char and also is guaranteed to be NULL-terminated, then (and only then) one could use strlen(). And even then it only gives the length of the string (excluding NULL terminator), not the buffer size. That's why we often pass a pointer plus a length, instead of only the pointer. It of course would be more convenient to use QString or QByteArray.

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on 8 Jan 2013, 18:47 last edited by
          #4

          One additional remark, this will work only for strings as defined above. There is no general answer to your question. In most cases you cannot do that, because the pointer simply points to the start location of memory. It does not know anything about the end of the allocated memory.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ahmed kato
            wrote on 8 Jan 2013, 19:13 last edited by
            #5

            @VRonin:
            Thank you, it worked for me
            I am using C style because this is not the original code, I am trying to proof a concept where char * is a must.

            Ahmed Kato
            Computer & communications student
            Intern and MSP at Microsoft

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MuldeR
              wrote on 8 Jan 2013, 19:39 last edited by
              #6

              [quote author="ahmed kato" date="1357672414"]@VRonin:
              Thank you, it worked for me
              I am using C style because this is not the original code, I am trying to proof a concept where char * is a must.[/quote]

              Keep in mind strlen() only gives you the number of non-NULL char's before the first NULL char.

              That's fine if you know for sure that all char-pointers in your QList will point to memory containing a NULL-terminated string. In any other case using strlen() is wrong - it can even be dangerous!

              (I say this because you originally asked for "the size of Data", not for "the length of a the String")

              My OpenSource software at: http://muldersoft.com/

              Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

              Go visit the coop: http://youtu.be/Jay...

              1 Reply Last reply
              0

              1/6

              8 Jan 2013, 17:37

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved