View previous topic :: View next topic |
Author |
Message |
Antony Guest
|
malloc |
Posted: Wed May 19, 2004 4:03 am |
|
|
Hi,
pls let me know whether the release 3.190 can solve the problem of the
function Malloc that is not working in a larger program.
Thanks |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
|
ABS Guest
|
malloc returns null |
Posted: Wed May 19, 2004 5:11 am |
|
|
I had problems with malloc always returning null.
Now, I moved the #include <stdlibm.h> statement to immediately follow #pragma USE DYNAMIC_MEMORY which follows the device statements at the top of the program.
#include <18F258.h>
...Device statements...
#pragma USE DYNAMIC_MEMORY
#include <stdlibm.h>
This got malloc to work where I expected it. I have not thoroughly tested the new code. |
|
|
Jeprox Guest
|
|
Posted: Wed May 19, 2004 6:56 am |
|
|
I gave up using malloc with ver 3.173.
It doesn't work, even using the #pragma USE DYNAMIC_MEMORY and #include <stdlibm.h>
If anyone can tell me it works pls post a simple code so I can try it. |
|
|
Antony Guest
|
malloc |
Posted: Wed May 19, 2004 8:04 am |
|
|
The following code doesn't allocate indipendents parts of memory for the
strings but overwrites totally or partially the locations of memory, loosing
the real content of the strings.
It's a bug of the release 3.177 or is there an error in the code?
#include <18F452.H>
#pragma USE DYNAMIC_MEMORY
#include
#include
#ZERO_RAM
unsigned char *stringa1, *stringa2, *stringa3;
void main()
{
while(1)
{
stringa1 = malloc(stringa1);
strcpy(stringa1, "STRINGA1");
stringa2 = malloc(stringa2);
strcpy(stringa2, "STRINGA2");
stringa3 = malloc(stringa3);
strcpy(stringa3, "STRINGA3");
}
return;
} |
|
|
abs
Joined: 19 May 2004 Posts: 5
|
Re: malloc |
Posted: Thu May 20, 2004 12:17 pm |
|
|
Antony wrote: | The following code doesn't allocate indipendents parts of memory for the
strings but overwrites totally or partially the locations of memory, loosing
the real content of the strings.
It's a bug of the release 3.177 or is there an error in the code?
#include <18F452.H>
#pragma USE DYNAMIC_MEMORY
#include
#include
#ZERO_RAM
unsigned char *stringa1, *stringa2, *stringa3;
void main()
{
while(1)
{
stringa1 = malloc(stringa1);
strcpy(stringa1, "STRINGA1");
stringa2 = malloc(stringa2);
strcpy(stringa2, "STRINGA2");
stringa3 = malloc(stringa3);
strcpy(stringa3, "STRINGA3");
}
return;
} |
In this code, your loop runs forever and you never free your strings. Malloc will necessarily run out of free memory, and malloc should start returning NULL. We must understand the behavior of strcpy when the destination is NULL (assuming malloc is returning NULL). The strcpy routine may be the problem with your code, not malloc, or you should free your strings. |
|
|
Guest
|
|
Posted: Fri May 21, 2004 1:32 am |
|
|
Thanks,
but also the follow code doesn't work.
#include <18F452.H>
#pragma USE DYNAMIC_MEMORY
#include <stdlibm.h>
#include <string.h>
#ZERO_RAM
unsigned char *stringa1, *stringa2, *stringa3;
void main()
{
free(stringa1);
stringa1 = NULL;
free(stringa2);
stringa2 = NULL;
free(stringa3);
stringa3 = NULL;
stringa1 = malloc(stringa1);
strcpy(stringa1, "STRINGA1");
stringa2 = malloc(stringa2);
strcpy(stringa2, "STRINGA2");
stringa3 = malloc(stringa3);
strcpy(stringa3, "STRINGA3");
while(1)
{
}
return;
} |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri May 21, 2004 2:48 am |
|
|
I don't know for the CCS compiler, but in other compilers you are NEVER allowed to call free() with an uninitialized pointer!
In this example code your calls to free() are pointless because you haven't called malloc() yet.
Furthermore your calls to malloc() are wrong. You need to specify how many bytes of storage you want to be returned:
Code: |
stringa1 = malloc(10);
strcpy(stringa1, "STRINGA1");
|
|
|
|
Guest
|
|
Posted: Fri May 21, 2004 3:55 am |
|
|
sorry,
i have forgotten a piece of code,
i have to allocate a piece of RAM that is function of the length of stringa.
Thanks
#include <18F452.H>
#pragma USE DYNAMIC_MEMORY
#include <stdlibm.h>
#include <string.h>
#ZERO_RAM
unsigned char *stringa1, *stringa2, *stringa3;
int num;
void main()
{
strcpy(stringa1, "STRINGA1");
num = strlen(stringa1);
stringa1 = malloc(num);
strcpy(stringa1, "STRINGA2");
num = strlen(stringa2);
stringa2 = malloc(num);
strcpy(stringa1, "STRINGA3");
num = strlen(stringa3);
stringa3 = malloc(num);
while(1)
{
}
return;
} |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri May 21, 2004 6:11 am |
|
|
Computers always do what you tell them to do, this is (often) not equal to what you want them to do.......
Code: |
strcpy(stringa1, "STRINGA1");
num = strlen(stringa1);
stringa1 = malloc(num);
|
First allocate the memory before calling strcpy() !!!
So change into:
Code: |
num = strlen("STRINGA1");
stringa1 = malloc(num);
strcpy(stringa1, "STRINGA1");
|
Carlo |
|
|
Guest
|
|
Posted: Fri May 21, 2004 6:35 am |
|
|
Hi,
i have changed the code (is been the first version of code), but it doesn't work.
Test the follow 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 = strlen("STRINGA1");
stringa1 = malloc(num);
strcpy(stringa1, "STRINGA1");
num = strlen("STRINGA2");
stringa2 = malloc(num);
strcpy(stringa2, "STRINGA2");
num = strlen("STRINGA3");
stringa3 = malloc(num);
strcpy(stringa3, "STRINGA3");
while(1)
{
}
return;
} |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Fri May 21, 2004 6:50 am |
|
|
I am not very familiar with the malloc() function, but there may be a problem with the basic use of strlen(). According to K&R, strlen(s) returns the length of the string EXCLUDING the terminating null. Therefore, num will be the number of characters in your string, not the total amount of memory required for the string. If you allocate that much memory for the string, and then copy the string to that location, won't the treminating null be outside of the allocated memory? It seems that you should be using something like the following:
Code: |
num = strlen("STRINGA1") +1;
stringa1 = malloc(num);
strcpy(stringa1, "STRINGA1");
|
|
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Fri May 21, 2004 6:51 am |
|
|
Just a quick note: You'll need to allocate one byte more than the length of your string to cater for the terminating NULL character.
Change your code to:
Code: |
num = strlen("STRINGA1")+1; //Add a one here
stringa1 = malloc(num);
strcpy(stringa1, "STRINGA1");
|
and so forth. |
|
|
Guest
|
|
Posted: Fri May 21, 2004 7:38 am |
|
|
OK!
Test the follow code: it doesn't work!
#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 = strlen("STRINGA1")+1;
stringa1 = malloc(num);
strcpy(stringa1, "STRINGA1");
num = strlen("STRINGA2")+1;
stringa2 = malloc(num);
strcpy(stringa2, "STRINGA2");
num = strlen("STRINGA3")+1;
stringa3 = malloc(num);
strcpy(stringa3, "STRINGA3");
while(1)
{
}
return;
} |
|
|
Darren Rook
Joined: 06 Sep 2003 Posts: 287 Location: Milwaukee, WI
|
|
Posted: Fri May 21, 2004 7:51 am |
|
|
stdlibm.h is open. If you're convinced there is a bug, why don't you look at stdlibm.h and figure out whats wrong? |
|
|
|