|
|
View previous topic :: View next topic |
Author |
Message |
haxan7
Joined: 27 Jul 2013 Posts: 79
|
Very Long Array Problem |
Posted: Wed Jun 25, 2014 2:25 pm |
|
|
Recently I had to declare a very long array (approximately 700 entries) and this caused the code to behave abnormally, compiler did not generate any error or warning and It was a pain in the [spam] to pin point the problem. After spending much time i realized that the pic's memory could be fragmented so I split the array in 7 parts and the problem was solved.
Is there an elegant way to solve this problem.
How do I know the maximum length of array I could define, since compiler doesn't warn and all.
Compiler Version: 5.008
PIC: 18F46K22
Last edited by haxan7 on Wed Jun 25, 2014 4:35 pm; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Wed Jun 25, 2014 2:51 pm |
|
|
yes... though you should help yourself by telling us WHICH PIC !!
if you read the PIC's datasheet it tells in detail the 'memory map' and how RAM is configured as well as how to access it.
Nothing is hidden.....
hth
jay |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Jun 25, 2014 4:17 pm |
|
|
show your code -and recall that the commonest problem with
an array is other parts of your program allowing illegal index values !!
CCS does no runtime bounds checking!!
ie: you can crash from writing outside the array bounds by accident. |
|
|
haxan7
Joined: 27 Jul 2013 Posts: 79
|
|
Posted: Wed Jun 25, 2014 4:37 pm |
|
|
asmboy wrote: | show your code -and recall that the commonest problem with
an array is other parts of your program allowing illegal index values !!
CCS does no runtime bounds checking!!
ie: you can crash from writing outside the array bounds by accident. |
I am 100% certain I wasn't going out of bounds.
During debugging I was't even using the array, just defining it caused problems. |
|
|
haxan7
Joined: 27 Jul 2013 Posts: 79
|
|
Posted: Wed Jun 25, 2014 4:38 pm |
|
|
temtronic wrote: | yes... though you should help yourself by telling us WHICH PIC !!
if you read the PIC's datasheet it tells in detail the 'memory map' and how RAM is configured as well as how to access it.
Nothing is hidden.....
hth
jay |
I have updated original post with Compiler Version, and PIC details. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Wed Jun 25, 2014 5:02 pm |
|
|
I agree with asmboy, we have to see your code.That is a short, compilable program that shows the fault.
Also which 'debugger' are you using ?
hth
jay |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Jun 26, 2014 12:44 am |
|
|
PIC16 processors and smaller have fragmented memory (bank switching) and might be problematic on larger arrays. Your PIC18 is not known to have any problems.
We have all been in a situation like this where we were 100% sure there was no bug in our code but somehow it didn't work. It almost never was the compiler that was the problem originator.
Without any code from you to demonstrate the problem it is not do-able to help you. Just some possible problem causes that come to mind are the already mentioned out-of-bounds array problem type. But even more likely I suspect an int8 index type being used somewhere. Int8 is the default integer size and can hold a maximum value of 255, too small for your 700 item array. It might even be an implicit int8 cast somewhere (these can be difficult to spot when you have looked at the code already for a long time). |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
|
Posted: Thu Jun 26, 2014 4:45 am |
|
|
We use that PIC, and have a 1k array:
Code: | static unsigned char display[128][8]; |
We have had no problems with this over the last 2 years of CCS updates. You must be doing it wrong. Post your non working code. |
|
|
haxan7
Joined: 27 Jul 2013 Posts: 79
|
|
Posted: Thu Jun 26, 2014 7:33 am |
|
|
This is how i was defining the array before.
Code: |
long adc2dis_SR[] = {900,900,900,900,900,900,900,900,900,900,900,.............,34,33,33,33,33,33,33};
|
This Code fixed the problem.
Code: |
long array1[100] = {900,900,900,900,900,900,900,900,900,900,900,900,900,871,843,816,792,768,746,726,706,688,670,653,637,622,608,594,581,568,556,544,533,522,512,502,493,484,475,466,458,450,443,435,428,421,414,408,402,395,389,384,378,373,367,362,357,352,348,343,339,334,330,326,322,318,314,310,306,303,299,296,293,289,286,283,280,277,274,271,268,265,263,260,257,255,252,250,247,245,243,241,238,236,234,232,230,228,226,224};
long array2[100] = {222,220,218,216,214,213,211,209,207,206,204,202,201,199,198,196,195,193,192,190,189,188,186,185,183,182,181,180,178,177,176,175,173,172,171,170,169,168,167,165,164,163,162,161,160,159,158,157,156,155,154,153,152,152,151,150,149,148,147,146,145,145,144,143,142,141,141,140,139,138,137,137,136,135,135,134,133,132,132,131,130,130,129,128,128,127,126,126,125,124,124,123,123,122,121,121,120,120,119,119};
long array3[100] = {118,117,117,116,116,115,115,114,114,113,113,112,112,111,111,110,110,109,109,108,108,107,107,106,106,105,105,105,104,104,103,103,102,102,102,101,101,100,100,99,99,99,98,98,97,97,97,96,96,96,95,95,94,94,94,93,93,93,92,92,92,91,91,91,90,90,90,89,89,89,88,88,88,87,87,87,86,86,86,86,85,85,85,84,84,84,83,83,83,83,82,82,82,82,81,81,81,80,80,80};
long array4[100] = {80,79,79,79,79,78,78,78,78,77,77,77,77,76,76,76,76,75,75,75,75,74,74,74,74,74,73,73,73,73,72,72,72,72,72,71,71,71,71,71,70,70,70,70,69,69,69,69,69,68,68,68,68,68,68,67,67,67,67,67,66,66,66,66,66,65,65,65,65,65,65,64,64,64,64,64,64,63,63,63,63,63,63,62,62,62,62,62,62,61,61,61,61,61,61,60,60,60,60,60};
long array5[100] = {60,60,59,59,59,59,59,59,58,58,58,58,58,58,58,57,57,57,57,57,57,57,56,56,56,56,56,56,56,56,55,55,55,55,55,55,55,54,54,54,54,54,54,54,54,53,53,53,53,53,53,53,53,53,52,52,52,52,52,52,52,52,51,51,51,51,51,51,51,51,51,50,50,50,50,50,50,50,50,50,49,49,49,49,49,49,49,49,49,49,48,48,48,48,48,48,48,48,48,48};
long array6[100] = {47,47,47,47,47,47,47,47,47,47,46,46,46,46,46,46,46,46,46,46,46,45,45,45,45,45,45,45,45,45,45,45,44,44,44,44,44,44,44,44,44,44,44,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,42,42,42,42,42,41,41,41,41,41,41,41,41,41,41,41,41,41,40,40,40,40,40,40,40,40,40,40,40,40,40,40,39,39,39,39,39};
long array7[100] = {39,39,39,39,39,39,39,39,39,39,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,33,33,33,33,33,33};
#inline
signed long adc2disSR(long adc){
if(adc>24) adc = adc-24;
long distance;
if (adc < 100){
distance = array1[adc];
}
else if (adc < 200){
distance = array2[adc-100];
}
else if (adc < 300){
distance = array3[adc-200];
}
else if (adc < 400){
distance = array4[adc-300];
}
else if (adc < 500){
distance = array5[adc-400];
}
else if (adc < 600){
distance = array6[adc-500];
}
else if (adc<700){
distance = array7[adc-600];
}
return distance;
}
|
|
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
|
Posted: Thu Jun 26, 2014 7:57 am |
|
|
Post the code you used to access the array before. |
|
|
haxan7
Joined: 27 Jul 2013 Posts: 79
|
|
Posted: Thu Jun 26, 2014 8:00 am |
|
|
oxo wrote: | Post the code you used to access the array before. |
Like I said before, I didn't access the array, just declaring it would cause problems. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Jun 26, 2014 8:04 am |
|
|
if you are not changing array values
have you thought of declaring them ain program memory space,
as one big array of archetype: CONST ??
to see how you like that ?
hint: it frees your SRAM and banking stops being a foreground issue for you..... |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Thu Jun 26, 2014 8:07 am |
|
|
Hi,
I think the problem in your original code is that you don't declare a size for your array. In C, the usual methods to declare an array, and initialize it with values are like this:
Code: |
int myArray[SIZE] = {1,2,3,4....};
|
or
Code: |
int myArray[SIZE];
myArray = {1,2,3,4....};
|
As ckielstra pointed out, you may have also been trying to reference your array with an int8 index variable? Now that you have broken your large array into several smaller arrays that won't be a problem. I'd go back and give the large array another try if for no other reason than to satisfy your intellectual curiosity!
John |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Jun 26, 2014 10:43 am |
|
|
5.008, also might be a 'borderline' version.
The first few versions of any CCS release often don't work right.
5.008, was before I consider V5 to really start working. 4.141, was a better compiler at this time. It might well be that a different problem with this version, is 'showing up' in the array... |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Jun 27, 2014 1:58 am |
|
|
To summarize:
- You didn't post a version of the program that failed, so we can't reproduce.
- V5.008 of the compiler is an early v5 compiler version and CCS has a history of these early versions having problems. You are better of to downgrade to v4.141 or one of the newer v5 versions. Not only for this bug for other issues as well.
- The large array is in RAM and uses so a lot of available memory.
Many people new to the PIC are unaware of the difference between ROM and RAM. Right now, your program will have to copy all the initialized data from ROM to RAM at start-up, so effectively the data is present twice in memory. Once as fixed program code in ROM and again as modifiable data in RAM. If your array data is never going to change you can save 1400 bytes of RAM by making it a 'const' array, this will locate the data in ROM only.
Here is an example program with your 700 long integers in 1 array, compiled with v4.141 and the memory usage for array in RAM and a version in ROM.
Code: | /* Version 1: Array in RAM
ROM used: 3618 bytes (6%)
Largest free fragment is 61918
RAM used: 1404 (36%) at main() level
1408 (36%) worst case
*/
/* Version 2: Array in ROM (declared with const)
ROM used: 1520 bytes (2%)
Largest free fragment is 64016
RAM used: 4 (0%) at main() level
8 (0%) worst case
*/
#include <18F46K22.h>
long array1[] = {900,900,900,900,900,900,900,900,900,900,900,900,900,871,843,816,792,768,746,726,706,688,670,653,637,622,608,594,581,568,556,544,533,522,512,502,493,484,475,466,458,450,443,435,428,421,414,408,402,395,389,384,378,373,367,362,357,352,348,343,339,334,330,326,322,318,314,310,306,303,299,296,293,289,286,283,280,277,274,271,268,265,263,260,257,255,252,250,247,245,243,241,238,236,234,232,230,228,226,224,
222,220,218,216,214,213,211,209,207,206,204,202,201,199,198,196,195,193,192,190,189,188,186,185,183,182,181,180,178,177,176,175,173,172,171,170,169,168,167,165,164,163,162,161,160,159,158,157,156,155,154,153,152,152,151,150,149,148,147,146,145,145,144,143,142,141,141,140,139,138,137,137,136,135,135,134,133,132,132,131,130,130,129,128,128,127,126,126,125,124,124,123,123,122,121,121,120,120,119,119,
118,117,117,116,116,115,115,114,114,113,113,112,112,111,111,110,110,109,109,108,108,107,107,106,106,105,105,105,104,104,103,103,102,102,102,101,101,100,100,99,99,99,98,98,97,97,97,96,96,96,95,95,94,94,94,93,93,93,92,92,92,91,91,91,90,90,90,89,89,89,88,88,88,87,87,87,86,86,86,86,85,85,85,84,84,84,83,83,83,83,82,82,82,82,81,81,81,80,80,80,
80,79,79,79,79,78,78,78,78,77,77,77,77,76,76,76,76,75,75,75,75,74,74,74,74,74,73,73,73,73,72,72,72,72,72,71,71,71,71,71,70,70,70,70,69,69,69,69,69,68,68,68,68,68,68,67,67,67,67,67,66,66,66,66,66,65,65,65,65,65,65,64,64,64,64,64,64,63,63,63,63,63,63,62,62,62,62,62,62,61,61,61,61,61,61,60,60,60,60,60,
60,60,59,59,59,59,59,59,58,58,58,58,58,58,58,57,57,57,57,57,57,57,56,56,56,56,56,56,56,56,55,55,55,55,55,55,55,54,54,54,54,54,54,54,54,53,53,53,53,53,53,53,53,53,52,52,52,52,52,52,52,52,51,51,51,51,51,51,51,51,51,50,50,50,50,50,50,50,50,50,49,49,49,49,49,49,49,49,49,49,48,48,48,48,48,48,48,48,48,48,
47,47,47,47,47,47,47,47,47,47,46,46,46,46,46,46,46,46,46,46,46,45,45,45,45,45,45,45,45,45,45,45,44,44,44,44,44,44,44,44,44,44,44,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,42,42,42,42,42,41,41,41,41,41,41,41,41,41,41,41,41,41,40,40,40,40,40,40,40,40,40,40,40,40,40,40,39,39,39,39,39,
39,39,39,39,39,39,39,39,39,39,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,33,33,33,33,33,33};
#inline
signed long adc2disSR(long adc){
if(adc>24) adc = adc-24;
long distance;
distance = array1[adc];
return distance;
}
void main()
{
adc2disSR(500);
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|