View previous topic :: View next topic |
Author |
Message |
tavioman
Joined: 22 Feb 2006 Posts: 65
|
I have some trouble with malloc... |
Posted: Wed Jul 11, 2007 3:18 pm |
|
|
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
|
|
Posted: Wed Jul 11, 2007 3:56 pm |
|
|
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
|
|
Posted: Thu Jul 12, 2007 1:15 am |
|
|
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
|
|
Posted: Thu Jul 12, 2007 5:31 am |
|
|
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
|
|
Posted: Thu Jul 12, 2007 12:09 pm |
|
|
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
|
|
Posted: Thu Jul 12, 2007 12:15 pm |
|
|
Thanks.
This will help me. |
|
|
Pret
Joined: 18 Jul 2006 Posts: 92 Location: Iasi, Romania
|
|
Posted: Fri Jul 13, 2007 12:44 am |
|
|
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
|
|
Posted: Fri Jul 13, 2007 12:50 am |
|
|
I only need to malloc for maximum 6 * 32bit at once.
After that I will use the malloc'ed array trough all the runtime. |
|
|
|