GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Mar-2004, 16:01
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road

Problem accessing array


Hi Dave (or anyone visiting),

I have another problem today. I have an array of filter coefficients defined before the "main()" function. However, when I try and access the coefficients they all have a value 0.

CPP / C++ / C Code:
double FIRCoeff[] = 
{0, -2.1711E-3,-6.1557E-3,-6.5556E-3, 0,0.0111,0.019,0.0158,0,-0.0209,-0.0334,
-0.0265,0,0.0328,0.0513,0.0401,0,-0.0488,
-0.0762,-0.0596,0,0.0737,0.1169,0.0934,
0,-0.1235,-0.206,-0.1764,0,0.2979,0.6346,
0.8996,1,0.8996,0.6346,0.2979,0,-0.1764,
-0.206,-0.1235,0,0.0934,0.1169,0.0737,0,
-0.0596,-0.0762,-0.0488,0,0.0401,0.0513,
0.0328,0,-0.0265,-0.0334,-0.0209,0,0.0158
0.019,0.0111,0,-6.5556E-3,-6.1557E-3,
-2.1711E-3,0};  // 65 elements

//the following is in "main()"
if ( ( FileIW = fopen("Indices.txt", "w") ) == NULL ) 
    {
     	printf("Error: can't create file.\n");
     	exit(0);
    }
    else
    {
    	for (i = 0; i < 65; i++)
    	{		
		   fprintf(FileIW, "%lf\n", FIRCoeff[i]);
		}    	  
    }
    fclose(FileIW); 


The actual program needs these values to be convolved with an input array. However, I was getting erroneous results and on checking the values of FIRCoeff I see they're all zero.

What am I doing wrong?

Thanks.
  #2  
Old 30-Mar-2004, 16:32
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road
I must add that I'm encountering this problem using Code Composer Studio v2.2 which I'm using to program a DSP application.

On a stand-alone C/C++ compiler (Bloodshed) I have no problems. However, while using CCS, the array values are all zero. If I declare the array as a local variable, inside main(), the program freezes for some reason.

While I understand that this is not a forum for the DSP platform in question, I was wondering if it had something to do with the way I was declaring the array.
  #3  
Old 30-Mar-2004, 16:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by hellhammer
Hi Dave (or anyone visiting),


CPP / C++ / C Code:
double FIRCoeff[] = 
// some stuff here
0.0328,0,-0.0265,-0.0334,-0.0209,0,0.0158
// more stuff





What am I doing wrong?

Thanks.

Not sure what you are doing wrong. Note a missing comma on one of your lines in the initialization of the array.

As always, whenever you don't see something in a file that you expect, I suggest you do a printf() of the data (or at least some of the data) to see if it's really there. If the printf() looks good but fhe file contents don't, you know where to look. If the printf() looks bad, then you know where to look.

"printf() is your friend"

Here's a complete program that works OK for me. If this works for you, then you can look at some other things in your program.

Note that I use %le instead of %lf, just to show a different way of printing floats (and doubles). This is the exponential format; default %lf prints fixed-point with 6 digits after the decimal place. So the %f could print small values, such as 1.23e-10 as 0.000000. This doesn't occur in your data array, but could occur in some calculations.

CPP / C++ / C Code:
#include <stdio.h>

double FIRCoeff[] = 
{0, -2.1711E-3,-6.1557E-3,-6.5556E-3, 
 0,0.0111,0.019,0.0158,0,-0.0209,-0.0334,
-0.0265,0,0.0328,0.0513,0.0401,0,-0.0488,
-0.0762,-0.0596,0,0.0737,0.1169,0.0934,
 0,-0.1235,-0.206,-0.1764,0,0.2979,0.6346,
 0.8996,1,0.8996,0.6346,0.2979,0,-0.1764,
-0.206,-0.1235,0,0.0934,0.1169,0.0737,0,
-0.0596,-0.0762,-0.0488,0,0.0401,0.0513,
 0.0328,0,-0.0265,-0.0334,-0.0209,0,0.0158,
 0.019,0.0111,0,-6.5556E-3,-6.1557E-3,
-2.1711E-3,0};  // 65 elements

int main()
{
  FILE *FileIW;
  int i;
  int num_elements;
  num_elements = sizeof(FIRCoeff) / sizeof(FIRCoeff[0]);
  printf("Number of elements = %d\n", num_elements);
  for (i = 0; i < num_elements; i++) {
    printf("%le\n", FIRCoeff[i]);
  }
//the following is in "main()"
  if ( ( FileIW = fopen("Indices.txt", "w") ) == NULL ) {
       printf("Error: can't create file.\n");
       return 1;
  }
  else {
    for (i = 0; i < 65; i++) {    
     fprintf(FileIW, "%le\n", FIRCoeff[i]);
    }        
  }
  return 0;
}


Regards,

Dave
  #4  
Old 30-Mar-2004, 17:01
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road
Dave, thanks a lot for your reply.

The funny thing is that when I print the contents of the array right at the beginning of main(), they print fine. However, when I do it down the program, I get zeros.

I don't use those coefficients anywhere earlier in the program, so their values aren't being changed by my instructions.

I am doing this in Code Composer Studio and it seems quirky to me. When I ran my program on the Bloodshed C/C++ compiler, the program ran fine. However before I write it off as a machine/compiler error, I want to be sure that I'm not making a mistake
  #5  
Old 30-Mar-2004, 17:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by hellhammer
Dave, thanks a lot for your reply.

The funny thing is that when I print the contents of the array right at the beginning of main(), they print fine. However, when I do it down the program, I get zeros.

I don't use those coefficients anywhere earlier in the program, so their values aren't being changed by my instructions.

I am doing this in Code Composer Studio and it seems quirky to me. When I ran my program on the Bloodshed C/C++ compiler, the program ran fine. However before I write it off as a machine/compiler error, I want to be sure that I'm not making a mistake

Whenever stuff gets overwritten (as it appears to be happening in your case), it's usually because of pointers gone wrong. Writing more bytes into another array than you were supposed to; pointers set to wrong values in some pointer-arithmetic mystical magic, etc. etc.

I have been known to write array contents before and after calls to other program functions to isolate problems (if all of the array is being clobbered, you don't have to write the whole array to debug the program, just the first few values). Sprinkle printf()s all throughout the landscape until you zero in on the bad boy doing his dirty work.

To make it easier to find and delete the temporary printf() statements, I usually use something like

Code:
printf("Debug: Calling function foo()\n"); // other printf()s to print out a few array values foo(); printf("Debug: Returned from function foo()\n"); // printf()s to print out the array values to see if they got clobbered

By using "Debug:" in the printf(), I can easily spot the debug information in the program output, and later can search for "Debug:" in my text editor to allow me to zap them after I have debugged the beast.

Just a few thoughts.


"printf() is your friend" or, for C++ guys: "cout << is your friend"

Dave
  #6  
Old 30-Mar-2004, 18:57
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road
Thanks for all the help, Dave. The problem is rather odd. I have two other arrays, x[897] and Index[897] that are defined locally to main().

I used CCS' "watch" tool to find out where the problem lies. The moment I access x[], the values of FIRCoeff[] all change to values that are around 5.5*10^-312 !

I guess it has something to do with the memory map. Now, I'm hunting in the CCS help file to see how to do that.

Thanks a lot for all your help, and I might bug you again shortly with other C related issues.

I coded my app in C# initially, and that language seems to be way more forgiving to a newbie!
  #7  
Old 30-Mar-2004, 19:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by hellhammer
Thanks for all the help, Dave. The problem is rather odd. I have two other arrays, x[897] and Index[897] that are defined locally to main().

I used CCS' "watch" tool to find out where the problem lies. The moment I access x[], the values of FIRCoeff[] all change to values that are around 5.5*10^-312 !

I guess it has something to do with the memory map. Now, I'm hunting in the CCS help file to see how to do that.

Thanks a lot for all your help, and I might bug you again shortly with other C related issues.

I coded my app in C# initially, and that language seems to be way more forgiving to a newbie!

C was designed to do operating-system things down deep in the machine, so has lots of ways to let us screw things up. For us who spend most of our time on big systems, it's sometimes eye-opening to get down deep (and dirty) in an embedded system. In your case, it sounds like something else may be screwing things up. I don't know how memory is allocated in your system, but I have run into things where local memory (stack) overlays global memory. Just a little configuration problem; nothing to do with programs.

Good Luck!!!!!!!!

Dave
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Speed up C++ code about 3d array! Truong Son C++ Forum 0 16-Mar-2004 22:52
throwing an struct(with an array) through a function knakworstje C Programming Language 5 15-Feb-2004 17:20
c: array comparison jack C Programming Language 7 26-Jan-2004 12:21
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 12:52
Problem with array norok MySQL / PHP Forum 3 16-Jul-2003 03:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:28.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.