hello!
i'm using CCS3.1 and C6416, in my project, i want to allocate space for
each pointer of a ' **g_apbyTxBuf ' tpye array, i did like this:
Uint8 *g_apbyTxBuf[40];
.........
Bool AllocateTxBuf()
{
int i,j;
for(i = 0; i < TX_BUF_NUM; i++)
{
g_apbyTxBuf[i] = NULL;
g_apbyTxBuf[i] = (Uint8 *)malloc(TX_BUF_LEN * sizeof(Uint8));
j = sizeof(g_apbyTxBuf[i]);
LOG_printf(&trace,"%d",j);
if(g_apbyTxBuf[i] == NULL)
{
LOG_printf(&trace,"BUF_allc failed!");
SYS_abort("BUF_allc failed"); ********************
return FALSE;
}
}
LOG_printf(&trace,"allocation successful!");
return TRUE;
}
when i run the project, i use the 'go main' mode, the program abort at
the line with '***********************', i'm puzzled, why the
value of g_apbyTxBuf[i] is still NULL?
and i use sizeof to check the length of g_apbyTxBuf[i], that is j, the value is
j=4.
so i cannot work out the reason.
could there anyone help me? thx a lot
memory allocate
Started by ●July 28, 2010
Reply by ●July 29, 20102010-07-29
fx,
Answers to your questions: where my answer is prefixed with ''
> when i run the project, i use the 'go main' mode, the program abort at
> the line with '***********************',
are you running with the BIOS or without the BIOS?
I suspect 'without the BIOS', so the program has no where to return to when SYS-Abort()
is called, so probably executes a 'halt' instruction.
Have you used the CCS to display a interleaved listing of the source+assembly?
Then you would know exactly what instruction caused the program halt.
> i'm puzzled, why the value of g_apbyTxBuf[i] is still NULL?
the call to malloc() returns NULL when the allocation fails.
That is why g_apbyTxBuf[i] is NULL.
What was the value of 'i' when the program fails?
Did you allocate a heap in the project parameters or the .cmd file, large enough to
contain all the area that malloc() needs?
> i use sizeof to check the length of g_apbyTxBuf[i], that is j, the value is j=4.
the 'thing' being passed to the sizeof() function is a single item in the array
g_apbyTxBuf, not the size of the area pointed to by the pointer within that item.
A pointer is 4 bytes. That is why the value in 'j' is 4.
Note:
--You already know the size of the malloc'd area is either 0 (when NULL returned by
malloc()) or is 'TX_BUF_LEN * sizeof(Uint8)'
--There is no way for the code to find the size of the area returned by malloc()
R. Williams
---------- Original Message -----------
From: fx-7413
To: c6x
Sent: Wed, 28 Jul 2010 17:44:43 +0800 (CST)
Subject: [c6x] memory allocate
> hello!
> i'm using CCS3.1 and C6416, in my project, i want to allocate space
> for each pointer of a ' **g_apbyTxBuf ' tpye array, i did like this:
> Uint8 *g_apbyTxBuf[40]; .........
Bool AllocateTxBuf()
{
int i,j;
for(i = 0; i < TX_BUF_NUM; i++)
{
g_apbyTxBuf[i] = NULL;
g_apbyTxBuf[i] = (Uint8 *)malloc(TX_BUF_LEN * sizeof(Uint8));
j = sizeof(g_apbyTxBuf[i]);
LOG_printf(&trace,"%d",j);
if(g_apbyTxBuf[i] == NULL)
{
LOG_printf(&trace,"BUF_allc failed!");
SYS_abort("BUF_allc failed"); ********************
return FALSE;
} // endif( malloc failed )
} // end for( each buffer )
LOG_printf(&trace,"allocation successful!");
return TRUE;
}
>
> when i run the project, i use the 'go main' mode, the program abort at
> the line with '***********************', i'm puzzled, why the value of
> g_apbyTxBuf[i] is still NULL? and i use sizeof to check the length of
> g_apbyTxBuf[i], that is j, the value is j=4. so i cannot work out the reason.
> could there anyone help me? thx a lot
------- End of Original Message -------
_____________________________________
Answers to your questions: where my answer is prefixed with ''
> when i run the project, i use the 'go main' mode, the program abort at
> the line with '***********************',
are you running with the BIOS or without the BIOS?
I suspect 'without the BIOS', so the program has no where to return to when SYS-Abort()
is called, so probably executes a 'halt' instruction.
Have you used the CCS to display a interleaved listing of the source+assembly?
Then you would know exactly what instruction caused the program halt.
> i'm puzzled, why the value of g_apbyTxBuf[i] is still NULL?
the call to malloc() returns NULL when the allocation fails.
That is why g_apbyTxBuf[i] is NULL.
What was the value of 'i' when the program fails?
Did you allocate a heap in the project parameters or the .cmd file, large enough to
contain all the area that malloc() needs?
> i use sizeof to check the length of g_apbyTxBuf[i], that is j, the value is j=4.
the 'thing' being passed to the sizeof() function is a single item in the array
g_apbyTxBuf, not the size of the area pointed to by the pointer within that item.
A pointer is 4 bytes. That is why the value in 'j' is 4.
Note:
--You already know the size of the malloc'd area is either 0 (when NULL returned by
malloc()) or is 'TX_BUF_LEN * sizeof(Uint8)'
--There is no way for the code to find the size of the area returned by malloc()
R. Williams
---------- Original Message -----------
From: fx-7413
To: c6x
Sent: Wed, 28 Jul 2010 17:44:43 +0800 (CST)
Subject: [c6x] memory allocate
> hello!
> i'm using CCS3.1 and C6416, in my project, i want to allocate space
> for each pointer of a ' **g_apbyTxBuf ' tpye array, i did like this:
> Uint8 *g_apbyTxBuf[40]; .........
Bool AllocateTxBuf()
{
int i,j;
for(i = 0; i < TX_BUF_NUM; i++)
{
g_apbyTxBuf[i] = NULL;
g_apbyTxBuf[i] = (Uint8 *)malloc(TX_BUF_LEN * sizeof(Uint8));
j = sizeof(g_apbyTxBuf[i]);
LOG_printf(&trace,"%d",j);
if(g_apbyTxBuf[i] == NULL)
{
LOG_printf(&trace,"BUF_allc failed!");
SYS_abort("BUF_allc failed"); ********************
return FALSE;
} // endif( malloc failed )
} // end for( each buffer )
LOG_printf(&trace,"allocation successful!");
return TRUE;
}
>
> when i run the project, i use the 'go main' mode, the program abort at
> the line with '***********************', i'm puzzled, why the value of
> g_apbyTxBuf[i] is still NULL? and i use sizeof to check the length of
> g_apbyTxBuf[i], that is j, the value is j=4. so i cannot work out the reason.
> could there anyone help me? thx a lot
------- End of Original Message -------
_____________________________________