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 CCS Technical Support

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







malloc
PostPosted: Wed May 19, 2004 4:03 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 19, 2004 4:38 am     Reply with quote

The CCS change log ( http://www.ccsinfo.com/versions.shtml ) doesn't mention anything. What exactly is the problem are you talking about?
ABS
Guest







malloc returns null
PostPosted: Wed May 19, 2004 5:11 am     Reply with quote

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







PostPosted: Wed May 19, 2004 6:56 am     Reply with quote

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
PostPosted: Wed May 19, 2004 8:04 am     Reply with quote

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

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

Re: malloc
PostPosted: Thu May 20, 2004 12:17 pm     Reply with quote

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








PostPosted: Fri May 21, 2004 1:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri May 21, 2004 2:48 am     Reply with quote

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








PostPosted: Fri May 21, 2004 3:55 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri May 21, 2004 6:11 am     Reply with quote

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








PostPosted: Fri May 21, 2004 6:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri May 21, 2004 6:50 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri May 21, 2004 6:51 am     Reply with quote

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








PostPosted: Fri May 21, 2004 7:38 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Fri May 21, 2004 7:51 am     Reply with quote

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?
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 1, 2  Next
Page 1 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