|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
|
Posted: Fri May 21, 2004 8:06 am |
|
|
stdlibm.h belongs release 3.177.
Where's the wrong? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri May 21, 2004 12:02 pm |
|
|
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
|
|
Posted: Fri May 21, 2004 4:10 pm |
|
|
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
|
|
Posted: Mon May 24, 2004 2:52 am |
|
|
THANKS A LOT!!! |
|
|
|
|
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
|