Thread: Im a newbie having problems with dynamic arrays, can anyone help?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    Im a newbie having problems with dynamic arrays, can anyone help?

    Hello guys, I'm currently trying to implement a Biquad filter into my code but this involves creating a dynamic array that is constantly updated so that I can recall the current digital sample and the previous 2 digital samples at any point for many many samples.

    I figured malloc() and free() were the only way to go with this and if anyone has a better suggestion then I'm all ears as I'm still very new to this whole C coding thing.

    Anyway, the problem I encounter arises when I try to point to the previous samples stored in the dynamic array and get a "Floating Point Error: Domain" error.

    Here is a overview of what ive got:

    Code:
    main()
    {
    	int current_sample = 1;
    	float *p1;
    	float *p2;
                    
                    p1 = malloc(3);
    	p2 = malloc(3);
    
                    wave[p1] = sine_osc * amplitude;
                    filtered_wave[p2] = a0 * wave[p1] + a1 * wave[p1-1] + a2                * wave[p1-2] - b1 * filtered_wave[p2-1] - b2 * filtered_wave[p2-2]
    
                    if (current_sample > 3)
    	{
    	                current_sample = 1;
    		free(p1);
    		free(p2);
    	}
    }
    Unfortunately p1-1 doesnt seem to address the previous sample stored in the array like I hoped and using p1++ just causes errors.

    What am I doing wrong?

    Thanks

    Rhys

    PS Is there a trick to using tab when writing a message, I cant get it to work on this forum?!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Dr.Spankenstein View Post
    PS Is there a trick to using tab when writing a message, I cant get it to work on this forum?!
    Yes. Don't use tabs.

    Have you thought about a circular queue?

    Using dynamic allocation for 3 items seems silly. And your syntax doesn't make much sense.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Why are you allocating 3 bytes for a float anyway? If you want 3 floats, you should do (3 * sizeof(float)) or (3 * sizeof(*p1)), where p1 is the pointer you are setting to point to the return value of malloc().

    But as was pointed out, if you only need 3 floats, then use an array or use separate variables.

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    If the number of floats you need is not user-input dependent, you can also just write:

    Code:
    float p[3];
    instead of:

    Code:
    float *p;
    p = malloc(3 * sizeof(*p));

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    Red face

    Thanks guys with your help I've finally figured it out....
    ...and yeah the code above was scrappy but I was just taking chunks from what I've written and slapping em together to post at 1 in the morning!

    Rhys

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 arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2002, 05:51 PM
  4. Dynamic arrays
    By Led Zeppelin in forum C Programming
    Replies: 2
    Last Post: 04-22-2002, 12:28 PM
  5. Newbie Help (Arrays, Structures, Functions,Pointers)
    By tegwin in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 06:29 PM