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

I have some trouble with malloc...

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



Joined: 22 Feb 2006
Posts: 65

View user's profile Send private message

I have some trouble with malloc...
PostPosted: Wed Jul 11, 2007 3:18 pm     Reply with quote

This is my code:
Code:
int32 * A = 0;
.
.
.
unsigned char i = 0;
A = (signed int32)malloc( sizeof( signed int32 ) * ( 5 ));
printf( "A array has %u elements\n\r", sizeof( A )/sizeof( signed int32 ) );
for( i = 0; i < 6; i++ ){
   A[i] = 0;   
}
A[1] = 20;
A[3] = 40;
printf( "A Array: " );
   for( i = 0; i < 5; i++ ){
      printf( "%Ld ", A[i] );
   }
printf( "\n\r" );


This is what is coming out on serial port:
Code:
.
.
.
.
.
Array has 2 elements
Array: 0 20 0 40 0
Array has 2 elements
Array : 0 20 0 40 0
Array has 2 elements
Array : 32 20 0 12648488 0
Array 2 elements
Array: 32 20 0 12648488 0
Array has 2 elements
Array: 32 20 0 12648488 0
Array has 2 elements
.
.
.
.
.
.

Problem 1: Why "Array has 2 elements", should not have 5???
Problem 2: "Array: 32 20 0 12648488 0", I only loaded A[1] and A[3] with 20 and 40.
What is happening?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 11, 2007 3:56 pm     Reply with quote

Ask yourself the questions:
What is A ? What type of object is it ?
In the PCH compiler, what's the size of that type of object ?
How many bytes does the compiler use to hold an object of that type ?

There's another problem: malloc() returns a pointer, but you're
casting to it return a 'signed int32'. That's not a pointer.

Also, you declare 'A' as a pointer to an 'int32', but then later in
the code, you treat it as a pointer to a 'signed int32'.
tavioman



Joined: 22 Feb 2006
Posts: 65

View user's profile Send private message

PostPosted: Thu Jul 12, 2007 1:15 am     Reply with quote

What I'm triyng to do is to dynamically make an array.
How can I get the number of elements in array?
ckielstra



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

View user's profile Send private message

PostPosted: Thu Jul 12, 2007 5:31 am     Reply with quote

As a general note: On embedded systems it is best to avoid malloc because of the long term memory fragmentation problems it can cause. Best is to allocate the largest possible buffer only once and then leave it like that.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jul 12, 2007 12:09 pm     Reply with quote

Quote:
What I'm triyng to do is to dynamically make an array.
How can I get the number of elements in array?

I don't think there is any function available that will tell you the size
of a malloc'ed region at runtime, after it's been created.

But you know the size of the malloc'ed region when you create it,
because you have to give it the size as a parameter. Just divide
the size of the region (in bytes) by the size of the array element.
tavioman



Joined: 22 Feb 2006
Posts: 65

View user's profile Send private message

PostPosted: Thu Jul 12, 2007 12:15 pm     Reply with quote

Thanks.
This will help me.
Pret



Joined: 18 Jul 2006
Posts: 92
Location: Iasi, Romania

View user's profile Send private message

PostPosted: Fri Jul 13, 2007 12:44 am     Reply with quote

If you have different buffers that their size may have different values in runtime, in embedded programming is using an pseudo memory alocation technique', like:
Code:
#define ALLOC_SIZE  128

int8 RawMem[ALLOC_SIZE];
int8 *RawMemPos = RawMem;

void *malloc(int8 Size)
{
   void * Result;
   if (RawMemPos + Size <= RawMem + ALLOC_SIZE)
   {
      Result = RawMemPos;
      RawMemPos+=Size;
   } else Result = 0;

   return Result;
}

Does not support Free. Only if you need to free all of them at once, or one by one in reversed order.
tavioman



Joined: 22 Feb 2006
Posts: 65

View user's profile Send private message

PostPosted: Fri Jul 13, 2007 12:50 am     Reply with quote

I only need to malloc for maximum 6 * 32bit at once.
After that I will use the malloc'ed array trough all the runtime.
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