CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problems with dynamic memory in CCS (calloc and malloc)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
plateau



Joined: 19 Jan 2013
Posts: 9

View user's profile Send private message

Problems with dynamic memory in CCS (calloc and malloc)
PostPosted: Wed Jan 23, 2013 5:04 pm     Reply with quote

HiHo, friends, are you all fine?

After so much problems in handling the interrupts, I've decided to use the KBHIT approach ( http://www.ccsinfo.com/forum/viewtopic.php?t=49774). Now, everything is ok but I have a doubt about dynamic memory use.

In my code, I have a pair of routines to write and read data from EEPROM. Both are based on native CCS EEPROM routines.

The read routine is:
Code:
char* leStringEEPROM(int8 low, int8 high){
   //char* texto = calloc((high-low),sizeof(char));
   char texto[12];
   int8 i,y=0;   
   
   for(i=low;i<high;i++){
      texto[y] = (char)read_int16_eeprom(i);
      y++;
   }
   return texto;
}

I don't know why, but this routine only works well when I declare the variable 'TEXTO' as a static length array. When I use the dynamic array associated with calloc, the array TEXTO always returns with wrong length (per example, if the right length would be 10, it returns 12).

Can anybody help me to solve this issue?

Regards,

Pedro Rosa.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 23, 2013 8:52 pm     Reply with quote

Post a simple test program that can be run in MPLAB Simulator, so we
can test your problem. And post your compiler version.
Here's an example. Post a program similar to this. Tell us what you
expect it to display, and tell us what you actually see as the output.
Code:

#include <18F4680.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS )
#include <stdlibm.h>

char *texto;

char* leStringEEPROM(int8 low, int8 high){
int8 i, y=0;   

texto = calloc((high-low), sizeof(char));
   
for(i=low; i<high; i++){
   texto[y] = y;  // (char)read_int16_eeprom(i);
   y++;
  }

return texto;
}

//======================================
void main(void)
{
int8 i;
char *ptr;

ptr = leStringEEPROM(0, 10);

for(i = 0; i < 12; i++)
    printf("%X ", *ptr++);

printf("\r");

while(1);
}
 
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Wed Jan 23, 2013 9:48 pm     Reply with quote

The code looks OK. You are taking into account the trailing zero that C strings require?

I would seriously recommend avoiding dynamic memory on microcontrollers. PICs don't have enough memory to make it worthwhile. Those functions are just there (IMHO) to make it easier to reuse existing code. Stick with statically allocated memory.
_________________
Andrew
plateau



Joined: 19 Jan 2013
Posts: 9

View user's profile Send private message

PostPosted: Thu Jan 24, 2013 6:08 am     Reply with quote

andrewg wrote:
The code looks OK. You are taking into account the trailing zero that C strings require?

I would seriously recommend avoiding dynamic memory on microcontrollers. PICs don't have enough memory to make it worthwhile. Those functions are just there (IMHO) to make it easier to reuse existing code. Stick with statically allocated memory.


HiHo, andrewg, how are you?

First of all, thank you so much for having replied me.

At the first moment, I determined the size of array using the calloc instruction:

Code:
char* texto = calloc((high-low),sizeof(char));


But it never worked well...the array 'TEXTO' always returns the wrong size.

Looking your reply about my code doesn't ensure the null-terminated character and some related doubts in other places around the internet, I've tried to increase by 1 Byte the size of array in the calloc instruction (i.e

Code:
char* texto = calloc((high-low)+1],sizeof(char));


and now it seems to work. :-D ).

From now on, I'll not use dynamic memory in PIC :-D

One more time, thank you so much.

Best regards,

Pedro Rosa! Very Happy Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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