# Negative values in array

Started by August 25, 2008
In my project, I initialize an array as follows:

float tempC[10]={2.4, -2.4456, -0.12564, -1.456, -4.235, 3.426, -3.876, -9.235, 1.2354, 10.02};

After this, I assign the values of tempC to a global array in order to check the values in the watch window of the main program. When I check the values, I get the following numbers:

{2.4, 1.2354, 10.02, -1.456, -4.235, 3.426, -3.876, -9.235, 1.2354, 10.02};

For whatever reason, the second and third values are not correct, but rather the 9th and 10th entries copied. I don't understand why this is happening. I used this as an example because when I initialize arrays with negative numbers, I get issues where the entries are incorrect. Does anyone know why this is happening? (keep in mind I'm not really a C programming expert).
Negative values-

> In my project, I initialize an array as follows:
>
> float tempC[10]={2.4, -2.4456, -0.12564, -1.456, -4.235, 3.426, -3.876, -9.235, 1.2354, 10.02};
>
> After this, I assign the values of tempC to a global array in order to check the values in the watch window of the main program. When I check the values, I get the following numbers:
>
> {2.4, 1.2354, 10.02, -1.456, -4.235, 3.426, -3.876, -9.235, 1.2354, 10.02};
>
> For whatever reason, the second and third values are not correct, but rather the 9th and 10th entries copied. I don't understand why this is happening. I used this as an example because when I initialize arrays with negative numbers, I get issues where the entries are incorrect. Does anyone know why this is happening? (keep in mind I'm not really a C programming expert).

Didn't you know that in DSPs, negative numbers cause polymorphic orthogonal
bit-reversed addressing -- only N numbers in the secondary array will be affected,
where N = M/2 (M is the number of negative numbers you have declared).

Ok...

... just kidding. I have doubt this has anything to do with whether your values are
pos/neg.

What if your array length is just one or two? Still work? Where is tempC is
declared? Prior to main()? Inside a function? Why do you need to use "another
global array" of length 10, why isn't tempC good enough?

What is the code you use to move values from tempC to the other array? Any chance
there is a bug in that code?

-Jeff
Jeff,

I actually have a program called DSP.cpp and main.cpp, and tempC was declared locally in a function within DSP.cpp. I then tried two things: passing the values of tempC into a global array (so that I could access the values in the main program), and simply declaring tempC at the beginning of DSP.cpp outside of all functions. Both of these approaches led to the same faulty results, as detailed in my first post.

Here is the code I used to move the values of tempC.

tempcheck=&tempC[0];

for(i=0;i<10;i++) {
tempcheck2[i]=*(tempcheck+i); }

Where tempcheck is declared as a floating type pointer, and tempcheck2 is declared as "float tempcheck2[10]." Both of these declarations are outside of any function and outside of main(). Instead of using a pointer, I also took the simple approach of passing the values of tempC into tempcheck2 without the pointer, using the same for loop approach.

The reason I'm even using this 10 element array example is that I actually had 2 100 element arrays defined; one consisting of only positive numbers, and the other consisting of positive and negative numbers. The one with positive numbers contains all the correct elements, and the mixed array has all of its positive entries correct, and all of the negative entries are set to 0.

To answer your other question, I intialized a 3 element array earlier, which had a negative number and two positive decimals, and it worked fine.
NV Doc-

> I actually have a program called DSP.cpp and main.cpp, and tempC was
> declared locally in a function within DSP.cpp. I then tried two things:
> passing the values of tempC into a global array (so that I could access
> the values in the main program), and simply declaring tempC at the
> beginning of DSP.cpp outside of all functions. Both of these approaches
> led to the same faulty results, as detailed in my first post.
>
> Here is the code I used to move the values of tempC.
>
> tempcheck=&tempC[0];
>
> for(i=0;i<10;i++) {
> tempcheck2[i]=*(tempcheck+i); }

Ok, can't do that. Recall that C67x floats and ints are stored as 4 bytes... but
your code is "forcing" the memory address to increment by one byte, which will cause
non-aligned memory accesses, leading to undefined results (maybe that polymorphic

for (i=0; i<10; i++) tempcheck2[i] = *tempcheck++;

This allows the compiler to increment tempcheck by 4 each time.

-Jeff

PS. Please don't cut the previous text from your posts. When future DSPers see your
post in the archive they won't know the original question -- a solution without a
problem.
> Where tempcheck is declared as a floating type pointer, and tempcheck2
> is declared as "float tempcheck2[10]." Both of these declarations
> are outside of any function and outside of main(). Instead of using
> a pointer, I also took the simple approach of passing the values of
> tempC into tempcheck2 without the pointer, using the same for loop
> approach.
>
> The reason I'm even using this 10 element array example is that I
> actually had 2 100 element arrays defined; one consisting of only
> positive numbers, and the other consisting of positive and negative
> numbers. The one with positive numbers contains all the correct
> elements, and the mixed array has all of its positive entries correct,
> and all of the negative entries are set to 0.
>
> To answer your other question, I intialized a 3 element array earlier,
> which had a negative number and two positive decimals, and it worked
> fine.
NV Doc-

Andrew let me know that my guidance below was not correct. The way you are using the
tempcheck pointer should be fine.

Here are some links in case anyone wants to study up on a basic C coding thing, as I
found that I needed to do:

http://en.wikipedia.org/wiki/Pointer#C_arrays

Also Andrew found some more basic and useful info. Googling on "pointer arithmetic"
is another suggestion.

What is your status now? Is the problem still there or have you figured out what was
happening?

-Jeff

-------- Original Message --------
Subject: Re: [code-comp] Re: Negative values in array
Date: Wed, 27 Aug 2008 21:00:38 -0500
From: Jeff Brower
Organization: Signalogic, Inc
To: t...@yahoo.com
CC: c...

NV Doc-

> I actually have a program called DSP.cpp and main.cpp, and tempC was
> declared locally in a function within DSP.cpp. I then tried two things:
> passing the values of tempC into a global array (so that I could access
> the values in the main program), and simply declaring tempC at the
> beginning of DSP.cpp outside of all functions. Both of these approaches
> led to the same faulty results, as detailed in my first post.
>
> Here is the code I used to move the values of tempC.
>
> tempcheck=&tempC[0];
>
> for(i=0;i<10;i++) {
> tempcheck2[i]=*(tempcheck+i); }

Ok, can't do that. Recall that C67x floats and ints are stored as 4 bytes... but
your code is "forcing" the memory address to increment by one byte, which will cause
non-aligned memory accesses, leading to undefined results (maybe that polymorphic

for (i=0; i<10; i++) tempcheck2[i] = *tempcheck++;

This allows the compiler to increment tempcheck by 4 each time.

-Jeff

PS. Please don't cut the previous text from your posts. When future DSPers see your
post in the archive they won't know the original question -- a solution without a
problem.
> Where tempcheck is declared as a floating type pointer, and tempcheck2
> is declared as "float tempcheck2[10]." Both of these declarations
> are outside of any function and outside of main(). Instead of using
> a pointer, I also took the simple approach of passing the values of
> tempC into tempcheck2 without the pointer, using the same for loop
> approach.
>
> The reason I'm even using this 10 element array example is that I
> actually had 2 100 element arrays defined; one consisting of only
> positive numbers, and the other consisting of positive and negative
> numbers. The one with positive numbers contains all the correct
> elements, and the mixed array has all of its positive entries correct,
> and all of the negative entries are set to 0.
>
> To answer your other question, I intialized a 3 element array earlier,
> which had a negative number and two positive decimals, and it worked
> fine.
Jeff,

Well, I ended up playing around with the various settings of my project and increased the stack size through the DSP/BIOS cmd file's memory manager, and this ended up allowing me to properly assign the two 100 element arrays I wanted to use to begin with. It seems as though all the variables I assign values to and use throughout the program are now displaying normal behavior.

The idea behind the stack size might have been an obvious thing to check, though I'm by no means an expert in programming, C++, or programming DSP's.
Doc-

> Well, I ended up playing around with the various settings of my project
> and increased the stack size through the DSP/BIOS cmd file's memory
> manager, and this ended up allowing me to properly assign the two 100
> element arrays I wanted to use to begin with. It seems as though all
> the variables I assign values to and use throughout the program are now
> displaying normal behavior.
>
> The idea behind the stack size might have been an obvious thing to
> check, though I'm by no means an expert in programming, C++, or
> programming DSP's.

Ok great. Glad to hear it's working. Your previous stack size must have been very
small, or even zero. Otherwise you would not see such a strange bug :-)

-Jeff