Thread: Dynamic arrays

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Dynamic arrays

    I have a piece of code which performs operations on a user defined amount of channels.
    I am trying to use an array to handle the channels (following Salem's advice) which works well when testing for an array of fixed length as below:

    Code:
    class class_name: 
    	.
    	.
    
    	protected:
    
    		virtual void function(int n, float *const *in, float *const *out);
    		float *Outchan[3];     // Declare Array for 4 Channels
    
    	.
    	.
    
    
    void class_name::function(int n, float *const *in, float *const *out)
    {
    	int i,nofout;
    	for (nofout=0; nofout<num_channels;nofout++)
    	{OutChan[nofout] = out[nofout];}   // Set number of outputs in signal vector
    
    	if (First==1)
    	{
    		ULStat = cbGetStatus (BoardNum, &Status, &CurCount, &CurIndex,AIFUNCTION);
    		for (nofout=0; nofout<num_channels;nofout++)
    				{OutChan[nofout] = 0;} // Give each channel a value in turn
    		}
    		
    		else First=0;
    	}
    
    	else
    	{
    		while (n>0)
    		{
    				for (nofout=0; nofout<num_channels;nofout++)  //adapt to varying channels
    				{
    					for (i=DuplicateSample;i>0;i--) //number of duplicate same sample
    					{
    						*OutChan[nofout]++ = ADData[OutCount];
    					}
    					*OutChan[nofout]++ = (ADData[OutCount++];
    					if (OutCount==Count) OutCount=0;
    				}
    				
    				n=n-(DuplicateSample+1);
    		}
    	}
    }
    However, I can't figure out how to adapt this to work with a dynamic array.
    I replaced
    Code:
    float *Outchan[3];
    with

    Code:
    float *OutChan;  // in class declaration
    . 
    OutChan= new float[num_channels];  // in the function
    But the compiler returns conversion errors.

    I suspect my phobia & lack of understanding of pointers are letting me down again!

    Could someone please explain how I would correctly use a dynamic array here?
    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What errors are you getting? Please copy and paste exact error messages, and mark the relevant lines with line-numbers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are creating an array of float pointers, so OutChan should be a double pointer:
    Code:
    float **OutChan;  // in class declaration
    . 
    OutChan= new float*[num_channels];  // in the function
    Of course, there's little reason to use this kind of dynamic array, it will cause a lot more grief for you and your class when you can use vector instead:
    Code:
    std::vector<float *> OutChan;  // in class declaration
    . 
    class_name::class_name(int num_channels) : OutChan(num_channels)  // in constructor initialization list
    {
      // ... you could also resize it later with OutChan.resize(num_channels);
    }

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    Daved your solution worked a treat.

    The tip on vectors looks very interesting too, I'll head over to the main site and read up on them.

    Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  4. dynamic arrays and structures
    By godofbabel in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 03:45 PM