Thread: Using INT array to pass structures?

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    41

    Using INT array to pass structures?

    Just a curious question. Is it wise to try and pass objects on the heap around using an integer array by storing in each array item the address of the objects.

    Something like the following perhaps.

    Code:
    struct s1{
    int a;
    int b;
    };
    
    class c1{
    c1();
    ~c1();
    float c;
    double d;
    }
    
    s1 *struct1 = new s1;
    c1 *class1 = new c1;
    
    int *params = new int[2];
    params[1] = (int)struct1;
    params[2] = (int)class1;
    
    CreateThread(...,..., (void*)params);
    
    ThreadFunc(void* lpParams)
    {
      int *params = (int*)lpParams;
      s1 *struct1 = (s1*)params[0];
      c1 *class1 = (c1*)params[1];
      delete [] params;
    }
    I see a compiler warning in the form of:
    warning C4312: 'type cast' : conversion from 'int' to 's1 *' of greater size

    Thoughts?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why do you want to store pointers as int? it is not portable

    use array of void pointers for this matter
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Better: make a struct/class that contains all the information you want to pass to the thread.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    "Better: make a struct/class that contains all the information you want to pass to the thread."

    Yeah, but I was getting too lazy to keep making structs for passing in parameters to each thread I was creating.. it just made more code to manage.

    " use array of void pointers for this matter"

    Yes,... yes I suppose I should.. not sure what I was thinking. That'll work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM