Dynamic array in MikroC

General discussion on mikroC.
Post Reply
Author
Message
wuu
Posts: 115
Joined: 24 Mar 2008 00:48

Dynamic array in MikroC

#1 Post by wuu » 09 Mar 2009 19:32

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.

womai
Posts: 239
Joined: 16 Apr 2008 07:45

#2 Post by womai » 10 Mar 2009 06:01

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

wuu
Posts: 115
Joined: 24 Mar 2008 00:48

#3 Post by wuu » 10 Mar 2009 14:46

Thank you for your answer womai. I'll do as you said and test for the end of the string. When receiving it.

gdmms
Posts: 9
Joined: 08 Jun 2009 19:47

malloc/free implementation

#4 Post by gdmms » 08 Jun 2009 19:52

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. :idea:

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Dynamic array in MikroC

#5 Post by Dany » 09 Jun 2009 08:01

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.
If it never exceeds 50 bytes then you have no problem: use a string of 50 characters..
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.
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. :D
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)

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

#6 Post by jpc » 09 Jun 2009 09:19

the most efficient dynamic array for this type of application is simple circular buffer, this will do it all without all the inconveniences of real heap-management.
Au royaume des aveugles, les borgnes sont rois.

wuu
Posts: 115
Joined: 24 Mar 2008 00:48

#7 Post by wuu » 09 Jun 2009 13:07

Tnx everybody but I allredy solve my problem with static array. Like womai said dynamic array is not a good idea for microcontroller.

Anuradha
Posts: 40
Joined: 16 Apr 2010 16:13

Re: Dynamic array in MikroC

#8 Post by Anuradha » 04 May 2010 09:55

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.

wuu
Posts: 115
Joined: 24 Mar 2008 00:48

Re: Dynamic array in MikroC

#9 Post by wuu » 04 May 2010 20:58

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;
  }
}

ugurvar
Posts: 6
Joined: 23 Sep 2011 07:45

Re: Dynamic array in MikroC

#10 Post by ugurvar » 23 Sep 2011 13:17

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....

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Dynamic array in MikroC

#11 Post by janko.kaljevic » 26 Sep 2011 11:19

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:
Hello,

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.

ugurvar
Posts: 6
Joined: 23 Sep 2011 07:45

Re: Dynamic array in MikroC

#12 Post by ugurvar » 26 Sep 2011 18:55

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

Post Reply

Return to “mikroC General”