Thread: how void* iterate for next element

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    how void* iterate for next element

    how void* iterate for next element
    Code:
    #include<iostream>
    
    using namespace std;
    
    void check(void *t){
    
      while(*(int*)t){
        cout << *(int*)t << endl;
    //    *(t++);
      }
    
    }
    
    int main(){
            int a[] = {5,4,7,8,0};
            check(a);
           
    }

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Cast the pointer type to an int.

    Code:
    *((int*)t)++;

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Since you can make a cast for t and use it, have you thought about, say

    int *u = reinterpret_cast<int*>(t);
    u++;

    That should iterate fine. Or, you know, stop using void* at all.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    6
    Thanks for reply, my prob is short out
    Code:
    int *u = reinterpret_cast<int*>(t);
    u++;
    but how can i find at run time which data type send to void*, above code work fine when sending data is known otherwise it give error result.
    Last edited by Salem; 05-13-2011 at 06:06 AM. Reason: Remove unreadable yellow...

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by chaman View Post
    but how can i find at run time which data type send to void*
    You can't. C/C++ basic datatypes are stored in memory as "contents only"; they do not contain any meta-information about themselves and therefore there is no way for the compiler to tell what datatype this content belongs to.

    For example, a regular int is (usually) 4 bytes, and a short 2 bytes. They are not null-terminated, meaning unless the compiler is told in advance whether a particular variable is a short int or a regular int, it cannot tell how many bytes to read from memory. This is why you must correctly cast the type of void -- if you do not cast, you will get a compiler error, and if you cast wrongly, you will cause a problem. If you want it to read two bytes and treat that as a number, use short. If you want 4 bytes, use int.

    Some languages do maintain meta-information about all variables*, and allow for typechecking of everything, but this is a performance hit and not part of C/C++. You could write such a system (and there may be some available in optional libraries), but most likely, you should not need to do so if you think about what you are doing in a way more consistent with conventional C++ programming.

    * AFAIK these are all interpreted languages, which use a common runtime interpreter to do this. The overhead for doing so is substantial.
    Last edited by MK27; 05-13-2011 at 06:06 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    MK27 is right. However, I think what you really want is something like this:
    Code:
    template<typename T>
    void check(T *t){
    
      while(*t){
        cout << *t << endl;
        t++;
      }
    
    }
    Now you can call check() for any type of array, as long as the very last item equals 0. You could even call it for your own datatypes as long as the proper functions are overloaded.

    Note that this might seem counterintuitive, as it doesn't seem to match what MK27 said. But it does; the type will be determined at compile time, not at runtime.

    Note that actually C++ does contain runtime type information for classes, but you should barely ever have to use it.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    19
    Quote Originally Posted by EVOEx View Post
    MK27 is right. However, I think what you really want is something like this:
    Code:
    template<typename T>
    void check(T *t){
    
      while(*t){
        cout << *t << endl;
        t++;
      }
    
    }
    Now you can call check() for any type of array, as long as the very last item equals 0. You could even call it for your own datatypes as long as the proper functions are overloaded.

    Note that this might seem counterintuitive, as it doesn't seem to match what MK27 said. But it does; the type will be determined at compile time, not at runtime.

    Note that actually C++ does contain runtime type information for classes, but you should barely ever have to use it.
    yes , the template programming just can solve the question !

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    The path you are on... leads to getting conked with the ban hammer.

    Soma

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    6
    How can determine length of array integer * pm in main when check (length 5) return int**

    Code:
    #include<iostream>
    
    using namespace std;
    int** check(){
      int a=10,b=2,c=3,d=4,e=5;
      int *p[5] = {&a, &b, &c, &d, &e};
      return p;
    
    }
    int main(){
      int **pm = check();
      cout << **pm << endl;
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well none of that would work anyway, since you're returning a pointer to a local variable (with pointers to yet more local variables).
    All these pointers are INVALID when the function exits.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Undefined behavior at its finest!
    Furthermore, there is no (standard) way to determine the size of an array outside the function it was defined in.
    You should use std::array or std::vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need to iterate through an std::map...
    By Programmer_P in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2011, 11:46 AM
  2. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM
  3. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  4. ::iterate
    By Coding in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2008, 05:46 AM
  5. iterate a 3d vector
    By Coding in forum C++ Programming
    Replies: 12
    Last Post: 07-31-2008, 05:35 PM