Thread: initalizing arrays

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    initalizing arrays

    Hello,

    Im having a bit of trouble with one of my methods. im new to C++ but have experince in java and this seems ok to me. Basically, all im wanting to do is fill up each array with data calculated elsewhere. When i print out the value of s in the method below its obviously the same as what i put in, but when i print for example volClip[vi], or each of the the elements in the arrays once they are full i get random data. I think its something to do with the way im initializing the arrays but im not exactly sure what. what do i need to do?

    take a look at my code:

    Code:
    void clipBuffer ( float s, char mode )
    {
    	static int 		vi = 0, pi = 0, zi = 0;
    	static float 	       volClip[CLIPSIZE], pitClip[CLIPSIZE], zcrClip[CLIPSIZE];
    	
    	switch (mode) {
    		case 'v':
    			volClip[vi] = s;
    			vi++;
    			printf("Mode %c vi %d\n", mode, vi);
    			printf("data %f",volClip[vi]);
    			break;
    		case 'p':
    			pitClip[pi] = s;
    			pi++;
    			printf("Mode %c pi %d\n", mode, pi);
    			printf("data %f ",pitClip[pi]);
    			break;
    		case 'z':
    			zcrClip[zi] = s;
    			zi++;
    			printf("Mode %c zi %d\n", mode, zi);
    			printf("data %f",zcrClip[zi]);
    			break;
    		
    		default:
    			printf("Incorrect mode selected:\nOptions:\n\t");
    			printf("v - Volume\n\tp - Pitch\n\tz - ZCR\n\te - Energy ratio subband\n");
    			break;
    	}
    	// IF THEY'RE FULL THEN JUST PRINT IT OUT (FOR DEBUGGING)
    	if (vi == CLIPSIZE && pi == CLIPSIZE && zi == CLIPSIZE){
    		vi = 0;
    		pi = 0;
    		zi = 0;
    		int i;
    		printf("VOL\n");
    		for (i = 0; i < CLIPSIZE; ++i) {
    			printf("%d\n",volClip[i]);
    		}
    		printf("PIT\n");
    		for (i = 0; i < CLIPSIZE; ++i) {
    			printf("%d\n",pitClip[i]);
    		}
    		printf("ZCR\n");
    		for (i = 0; i < CLIPSIZE; ++i) {
    			printf("%d\n",zcrClip[i]);
    		}
            }
    }

    Thanks very much for looking!

    Steve

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > static float volClip[CLIPSIZE], pitClip[CLIPSIZE], zcrClip[CLIPSIZE];
    Since you said static, these are all initialised to 0.0

    > volClip[vi] = s;
    > vi++;
    > printf("Mode %c vi %d\n", mode, vi);
    > printf("data %f",volClip[vi]);
    You're printing out AFTER you've incremented the count.
    Try
    volClip[vi] = s;
    printf("Mode %c vi %d\n", mode, vi);
    printf("data %f",volClip[vi]);
    vi++;

    It's also a good idea to check whether vi is < CLIPSIZE before doing the assignment.
    Because in order to satisfy this test
    if (vi == CLIPSIZE && pi == CLIPSIZE && zi == CLIPSIZE)
    your code MUST have written off the ends of each of the arrays (not good).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    thanks, yeah they are good tips and how didnt i see that before! its those lazy sundays i guess... also noticed i was doing a %d for a float, hence the bizzare answers. School boy error!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  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. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM