Dynamic array in MikroC
Dynamic array in MikroC
Is it possible to create dynamic array in MikroC? I would need this for receiving a string via USART, the string received via USART could vary in length and would never exceeds 50 bytes. I know that this is possible in C++, but as I read also in ANSI C, with help of malloc, calloc, realloc and free functions. The problem is I haven't trace down those functions in MikroC.
MikroC does not have any such memory allocation functions built in. The main reasons are that they create a headache for memory management (you wouldn't want your PIC to suddenly get stuck because it ran out of memory of because memory fragmentation became to high) and would add significant program overhead as well as unpredictable timing (e.g. sudden slowdowns when garbage collection needs to kick in to allocate a chunk of memory for malloc) - bad for any real-time system.
In your case, since the max. length is known, why don't you simply define (and thus allocate) a global array of 50 bytes to begin with? You need to have that much space available anyway at any time because otherwise you would run out of memory when receiving a maximum length string.
If you really really need dynamic memory management (in your case there isn't a need), you could of course write your own malloc/free functions that would return pointers to locations within some large global array (large in PIC terms means a few 100 bytes...).
Wolfgang
In your case, since the max. length is known, why don't you simply define (and thus allocate) a global array of 50 bytes to begin with? You need to have that much space available anyway at any time because otherwise you would run out of memory when receiving a maximum length string.
If you really really need dynamic memory management (in your case there isn't a need), you could of course write your own malloc/free functions that would return pointers to locations within some large global array (large in PIC terms means a few 100 bytes...).
Wolfgang
malloc/free implementation
If you want to try to implement your own malloc/free functions I suggest you to have a look Kernighan/Ritchie book "The C Programming Language". There is an example of a memory allocator.
Re: Dynamic array in MikroC
If it never exceeds 50 bytes then you have no problem: use a string of 50 characters..wuu wrote:Is it possible to create dynamic array in MikroC? I would need this for receiving a string via USART, the string received via USART could vary in length and would never exceeds 50 bytes.
This is also not available in mikroPascal. However, I made something myself, see http://www.mikroe.com/forum/viewtopic.p ... &start=144. Perhaps You can use this implementation as an example for one made in C.wuu wrote:I know that this is possible in C++, but as I read also in ANSI C, with help of malloc, calloc, realloc and free functions. The problem is I haven't trace down those functions in MikroC.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
Re: Dynamic array in MikroC
hello Wuu,
Can you post the code which you have solve the problem, so that i can use it as a guide?
I need to allocate memory using malloc function , since it is not supported by mikroc, may be your solution will help me.
Thank you.
Can you post the code which you have solve the problem, so that i can use it as a guide?
I need to allocate memory using malloc function , since it is not supported by mikroc, may be your solution will help me.
Thank you.
Re: Dynamic array in MikroC
I've solved my problem by setting array fixed lenght witch is never exceded. This is my code:
Code: Select all
char rx_data[64];
void interrupt() {
char r = 0;
unsigned timeOut = 0;
if(PIR1.RCIF){
while(timeOut < 5000){
if(UART1_Data_Ready()){
rx_data[r] = UART1_Read();
r++;
rx_data[r] = 0;
timeOut = 0; // reset timeout
}
timeOut++;
}
rx_flag = 1;
}
}
Re: Dynamic array in MikroC
Salutatuions from turkeyto you pic programmers!
i have a problem in mikro c-pic16f877...
about arrays!!! for example; mikro c is included lcd(charrrays) but not lcd(intarrays)(it is compiling but now working)
i wanna send integer values to lcd(4x16). but,mikro c just allows to char values...
for example:
int i;
int x=0;
int a[40];
for(i=0;i<20;i++)
{
i+=1;
a=i;
Lcd_Out(1,2,a)
}
can u send me a simple sample code about sending to lcd integer values...
thanx for a lot....
i have a problem in mikro c-pic16f877...
about arrays!!! for example; mikro c is included lcd(charrrays) but not lcd(intarrays)(it is compiling but now working)
i wanna send integer values to lcd(4x16). but,mikro c just allows to char values...
for example:
int i;
int x=0;
int a[40];
for(i=0;i<20;i++)
{
i+=1;
a=i;
Lcd_Out(1,2,a)
}
can u send me a simple sample code about sending to lcd integer values...
thanx for a lot....
- janko.kaljevic
- Posts: 3565
- Joined: 16 Jun 2011 13:48
Re: Dynamic array in MikroC
Hello,ugurvar wrote:Salutatuions from turkeyto you pic programmers!
i have a problem in mikro c-pic16f877...
about arrays!!! for example; mikro c is included lcd(charrrays) but not lcd(intarrays)(it is compiling but now working)
i wanna send integer values to lcd(4x16). but,mikro c just allows to char values...
for example:
LCD library takes pointer to char as input parameter.
If you want to display array of int variables, you will need to convert them to char values before, and create string.
Please check conversion library in Help file for more details.
Best regards.
Re: Dynamic array in MikroC
thanx, i think u help me more!:D:D:D
i will share my codes...
i trying a big project(i will pic instead of PLC)
my e-mail:
ugur.liverpool@hotmail.com
i think,my e-mail is not insight!!!:D:D:D
i will share my codes...
i trying a big project(i will pic instead of PLC)
my e-mail:
ugur.liverpool@hotmail.com
i think,my e-mail is not insight!!!:D:D:D