is void* dangerous ?

This is a discussion on is void* dangerous ? within the C++ Programming forums, part of the General Programming Boards category; my book said a pointer like that could convert into some other data type. Thus it should be popular but ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Question is void* dangerous ?

    my book said a pointer like that could convert into some other data type. Thus it should be popular but Bruce Eckel said later that we'd better avoid not using it, why ???
    Never end on learning~

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    One time I was walking down the street when all of the sudden a void pointer burst out of a trash can and attacked me...

    They are not dangerous. But you need to be careful because casting pointers can get you into trouble if you are a newbie and don't know what it is that you are doing exactly.

    Code:
    void example1(void *arg) {
        char *str = (char *)arg;
        *str = 'a';
    }
    
    void example2(void *arg) {
        long *array = (long *)arg;
        *array = 'a';
    }
    
    int main() {
        char str[256];
    
        memset(str, 0, 256);  //zero out the array
        example1(str);
        cout << "string value: " << str << "\n";
    
        example2(str);
        cout << "long value: " << str << " (different much?)";
    }
    Remember that different data types have different sizes. So make sure you are familiar with pointers before playing around with void *'s.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    thanx. I'd better practice something about pointer first.
    Never end on learning~

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    it's dangerous to miscast pointers, yes.

    that is true of all pointers who have been cast in strange ways. void * is no different but that doesn't make the use of void * wrong. You just have to learn what makes miscasts dangerous by trial and error mostly. Have fun, "you've taken your first step to a larger world"
    always looking, make an offer. get me out of this place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is shared_ptr dangerous?
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-07-2008, 02:02 AM
  2. why is duplicate code dangerous?
    By agentsmith in forum C Programming
    Replies: 14
    Last Post: 01-08-2008, 12:10 AM
  3. Is sem_getvalue() dangerous?
    By Mr_Miguel in forum C Programming
    Replies: 3
    Last Post: 01-01-2008, 12:54 PM
  4. Dangerous System Functions for Servers ???
    By Moni in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2003, 10:55 PM
  5. Why is the gets function dangerous?
    By Kevin.j in forum C Programming
    Replies: 2
    Last Post: 09-27-2002, 05:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21