Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: [C] The lousy random question thread

  1. #11
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    450
    Thanks
    96
    Thanked 296 Times in 188 Posts
    next 'problem':

    i've got a struct including an array
    this array should be variable in its length , but <= MAX_LENGTH

    PHP Code:
    #define MAX_LENGTH 256

    struct myStruct
    {
        
    int array[MAX_LENGTH];
    }; 
    infact this struct is now filled with 256 unused integers
    how do i get anything similar to this without having a big empty array?

    EDIT:
    and i dont want to allocate new memory for every single input
    so neither 'int* array;' nor 'int array[];' will work without that and without using static construction like: 'struct myStruct s = { 1 , 2 , 3 };'
    Last edited by serthy; 15th August 2013 at 21:27.

  2. #12
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,010
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    If you want to have a variable length, you need to use realloc(): http://www.cplusplus.com/reference/cstdlib/realloc/

    The function doesn't allocate new memory, it's just "extended" the old memory (so you don't need to copy the old data to a new malloc()-block etc.)

    I guess you don't want that also, but there is no other way besides using some external libraries like GLib
    timescale 0.01

  3. #13
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    450
    Thanks
    96
    Thanked 296 Times in 188 Posts
    damn... you are right

    i just read that VLA's (variable-length-arrays) arent allowed in structs either...
    i guess have to go with the fixed array length and shipping unused array-elements around
    this app is for my c++ anim system and i want my vertex to have up to MAX_BONES_PER_VERT
    bonesID's stored for vertex blending purposes

    is it worth to allocate each bonesID in my vertex-class or simply allocate the whole vertex (and its members) only once?
    kung, you are the yoda in here

    EDIT: maybe i can go with sth like that:
    PHP Code:
    class myVert
    {
        public:

            
    floatweights;
            
    unsigned charbones;
            
    unsigned char numInfluencers;

            
    //constructor
            
    myVert( const unsigned char count , const unsigned charbonesArray NULL , const floatweigthsArray NULL )
            {
                
    numInfluencers count;

                
    bones = new unsigned char [numInfluencers];
                
    weights = new float [numInfluencers];

                for( 
    int i numInfluencers i++ )
                {
                    
    bones[i] = bonesArray[i];
                    
    weights[i] = weigthsArray[i];
                }
            }

            ~
    myVert() //destructor
            
    {
                
    delete [] bones;
                
    delete [] weights;
            }
    }; 
    Last edited by serthy; 15th August 2013 at 23:27.

  4. #14
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,010
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    Quote Originally Posted by serthy View Post
    i just read that VLA's (variable-length-arrays) arent allowed in structs either...
    That's bullshit, a resizeable array is just a simple pointer and can be stored in a structure or wherever you want.

    Well, for your other question, no clue ^^
    timescale 0.01

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •