Loops to control a function element

This is a discussion on Loops to control a function element within the C Programming forums, part of the General Programming Boards category; I really don't know the best way to search for an answer so I am going to try to explain ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Loops to control a function element

    I really don't know the best way to search for an answer so I am going to try to explain my problem.

    I am writing a drum machine program and I have some parts in the code that I think could be made smaller. The biggest problem I have is that I don't know how to dynamically change an element in a function call.

    For example I would like to change this:

    Code:
    hitFunc(void) {
                wavPlay(sample0, vol, bal, etc);
                wavPlay(sample1 vol, bal, etc);
                wavPlay(sample2 vol, bal, etc);
                wavPlay(sample3 vol, bal, etc);
                wavPlay(sample4 vol, bal, etc);
                wavPlay(sample5 vol, bal, etc);
                wavPlay(sample6 vol, bal, etc);
                //etc…
    }
    To something like this:

    Code:
    hitFunc(void) {
                for(X=0; X < 16; X++) {
                            wavPlay(sampleX vol, bal, etc);
                }
    }
    The issue I have is that sample0 is a structure of memory and I don’t know how to point it to sampleX where X is a variable number in a loop and have it work.

    Any help would be appreciated!!!

  2. #2
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    > The issue I have is that sample0 is a structure of memory and I don’t know how to point it to sampleX where X is a variable number in a loop and have it work.

    structure of memory? do you mean struct?

    you can make it an array if you want
    Code:
     hitFunc() {
       for(x = 0; x < 16; x++) {
          wavPlay(sample[x].vol, bal, etc);
       }
    }

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If the OP hasn't thought of that, [s]he probably hasn't learned about arrays yet: CProgramming.com Tutorial: Lesson 8: Arrays.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    Yes, it is a struct with a variable size of data that hold the data for the wave audio. I thought about using an array but can you do that and make it dynamic? You can load/unload wave audio on the fly and the size can be as big as there is free ram. With an array wouldn't I have to set the data size to a fixed value?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, you would. You should use dynamically allocated arrays if you don't know the size in advance. Look up realloc().

    [edit] Here's a sample I wrote using realloc(). It's not very well written, but it might give you some idea of what to do: Reading text file twice, can it be done once? [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    Ok, I'll look up realloc, but I have one more question.

    If I create an array of my struct.
    In array element 1 I have a 1mb sample.
    In array element 2 I have a 3mb sample.
    In array element 3 I have a 0.5mb sample.

    Then I free the memory for element 1 and realloc 2mb to load a new sample. Would the pointers for element 2 and 3 move dynamically or would I need to realloc the whole array?

    Thanks for the help by the way!

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    malloc() allocates memory. realloc() resizes memory. (If no memory has been allocated, realloc() is like malloc().)

    Therefore, to create an array, you would realloc() an array slightly bigger for each element you wanted to add. The elements of the array could be pointers to 1mb of memory or whatever; these would be separate. You would malloc() these and free() them when you remove an element from the array.

    Hope that helps. You could try a man page: http://www.uwm.edu/cgi-bin/IMT/wwwma...3%29&msection=

    [edit] This might compile, I haven't tried it:
    Code:
    #include <stdio.h>
    #include <stddef.h>  /* for size_t */
    #include <stdlib.h>  /* for malloc(), realloc(), and strtol() */
    #include <ctype.h>  /* for isspace() */
    
    struct person_t {
        char *name;
        size_t len;
        long age;
    };
    
    int main(void) {
        struct person_t *array = 0, *temparray;
        size_t elements = 0, x;
        char buffer[BUFSIZ], *p;
        long age;
    
        while(fgets(buffer, sizeof(buffer), stdin)) {
            age = strtol(buffer, &p, 0);
            while(isspace(*p)) p ++;
    
            temparray = realloc(array, sizeof(struct person_t) * (elements + 1));
            if(!temparray) break;  /* out of memory */
            array = temparray;
    
            array[elements].len = strlen(p);
    
            array[elements].name = malloc(array[elements].len+1);  /* +1 for NULL */
            if(!array[elements].name) break;
            strcpy(array[elements].name, p);
    
            array[elements].age = age;
    
            elements ++;
        }
    
        for(x = 0; x < elements; x ++) {
            printf("%s [%zu chars], age %ld\n", array[x].name, array[x].len, array[x].age);
            free(array[x].name);
        }
    
        free(array);
    
        return 0;
    }
    [/edit]
    Last edited by dwks; 10-17-2006 at 11:30 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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