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

malloc
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








PostPosted: Fri May 21, 2004 8:06 am     Reply with quote

stdlibm.h belongs release 3.177.
Where's the wrong?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri May 21, 2004 12:02 pm     Reply with quote

This discussion is going on way too long, so let's see if we can bring it to an end.

You are asking us to test your example code, is this because you don't have test equipment like an in-circuit debugger (ICD) ?
These kind of devices make your life so much easier because you can set breakpoints and examine variables.

I tried your example code on PCH v3.187 and came up with the following:
1) In my version I had to remove the "#pragma USE DYNAMIC_MEMORY"
2) You can pass a constant string to strcpy() but not to strlen(), doing so will return a 0 length. This is different from standard C and a compiler warning would have been nice here....

The following code works great (no compiler errors and no memory overlaps)

Code:

#include <18F452.H>
// #pragma USE DYNAMIC_MEMORY
#include <stdlibm.h>
#include <string.h>
#ZERO_RAM

unsigned char *stringa1, *stringa2, *stringa3;
int num;


void main()
{
num = 8 + 1;
stringa1 = malloc(num);
strcpy(stringa1, "STRINGA1");

num = 8 + 1;
stringa2 = malloc(num);
strcpy(stringa2, "STRINGA2");

num = 8 + 1;
stringa3 = malloc(num);
strcpy(stringa3, "STRINGA3");

for(;;)
  ; // Loop forever
}
Charlie U



Joined: 09 Sep 2003
Posts: 183
Location: Somewhere under water in the Great Lakes

View user's profile Send private message

PostPosted: Fri May 21, 2004 4:10 pm     Reply with quote

Here's a quick edit of your latest code, still using strlen, but adding a temporary array to hold the string:

Code:

#include <18F452.H>
#pragma USE DYNAMIC_MEMORY
#include <stdlibm.h>
#include <string.h>
#ZERO_RAM

unsigned char *stringa1, *stringa2, *stringa3;
int num;
int string_temp[10] = {0,0,0,0,0,0,0,0,0,0};

void main()
{
   strcpy(string_temp, "STRINGA1");
   num = strlen(string_temp)+1;
   stringa1 = malloc(num);
   strcpy(stringa1, "STRINGA1");

   strcpy(string_temp, "STRINGA2");
   num = strlen(string_temp)+1;
   stringa2 = malloc(num);
   strcpy(stringa2, "STRINGA2");

   strcpy(string_temp, "STRINGA3");
   num = strlen(string_temp)+1;
   stringa3 = malloc(num);
   strcpy(stringa3, "STRINGA3");

   while(1)
   {
   }
   return;
}


I compiled it with 3.190 and simulated it in MPLAB 6.40 and it appears to work fine.
Guest








PostPosted: Mon May 24, 2004 2:52 am     Reply with quote

THANKS A LOT!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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