View previous topic :: View next topic |
Author |
Message |
ThomasC
Joined: 09 Oct 2007 Posts: 62
|
Dynamic Length Array Examples? |
Posted: Fri Apr 04, 2008 10:22 am |
|
|
PIC16F690-ICD / PCM 4.60 / ICD2
Hi again,
I can't seem to implement a dynamic length array to take in a command string from the master (pic is slave of course).
This array must be able to change length on the fly as the getc() is taking in the string byte by byte.
Does anyone have an example of how I can do this? I can take in the command string fine when I set a static array size.
I've tried to use malloc() and calloc() to declare the maximum size I'll need and just fill it up to what size I need. But I get data size to big when I try GetIn485[520], because the max size I need is 520 bytes.
I've searched and do not understand what I found, or I've found the wrong articles. Thanks!
Code: |
char GetIn485[]; //works fine when GetIn485[10] for example
do //***Take in Command***
{
GetIn485[iptr++]=getc(); //Reads in Array1 from RS485 using the getc() function byte at a time
}
while (GetIn485[iptr-1]!=26); //Will keep reading until Control-Z, which 1A in hex or 26 in decimal
|
Code: |
int8 *iptr=0; //declare iptr to reserve memory for GetIn485
iptr=malloc(520); // iptr will point to a block of memory of 520 bytes.
//iptr=calloc(520,1); //A pointer to the allocated memory, if any. Returns null otherwise. Reserves memory.
// iptr will point to a block of memory of 521 bytes all initialized to 0.
ptr = 0; //ptr is just a variable not a pointer
Zero = getc(); //Purge the first byte which is 0
do //***Take in Command for Allocated Memory Space***
{
iptr[ptr++]=getc(); //Reads in Array1 from RS485 using the getc() function byte at a time
}
while (iptr[ptr-1]!=26); //Will keep reading until Control-Z, which 1A in hex or 26 in decimal
|
|
|
|
Matro Guest
|
|
Posted: Fri Apr 04, 2008 10:57 am |
|
|
Dynamic size array means nothing.
That's the kind of stuff that could be used in high level programming for PC. And even in this case it means nothing...
The compiler shall now the length of the array to allocate memory for it. SO you have to fix the length of your array.
You can virtually change the length of it for example by choosing a termination character that will terminate the data serie (like the '\0' terminates a string).
Anyways, this kind of things "int array[]" is a declaration but not an assignation (in memory). So you can't use it.
You have to fix a length for your array. No other way.
Matro. |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Dynamic Length Array Examples? |
Posted: Fri Apr 04, 2008 11:35 am |
|
|
ThomasC wrote: | PIC16F690-ICD / PCM 4.60 / ICD2
iptr=malloc(520); // iptr will point to a block of memory of 520 bytes.
[/code] |
The physical memory is fragmented. You cant just increment a pointer through it. I suggest using an 18 series chips. Also the datasheet shows only 256 bytes of RAM for this part but the RAM address range is 512. |
|
|
|