Subject: [mad-dev] malloc to static array Date: Thu, 13 Oct 2005 16:46:15 -0700
Hello Everyone,
Thanks for taking the time to read this. Im trying to replace some of the malloc's used in madlib as to avoid the overhead of malloc, since I am using a SHARC DSP with limited memory and power. Anyway as per Andre's February 2002 post,he suggests using static array's to contain the data rather than a malloc, which makes sense. My problem is that Im not sure how to do that. It sound simple but not being an overly experienced programmer, Im not sure what data type the array should be to house the data within a struct. Sorry I've tried a number of things but just am not sure how to do this. The orignal code in decoder.c, mad_decoder_run function is:
decoder->sync = malloc(sizeof(*decoder->sync));
using an unsigned char array such as:
unsigned char syncDat[2000]; //sizeof originally yields ~1650 bytes, so 2000 should be suffice
and replacing the line of code with:
decoder->sync = syncDat[0];
Produces the following error
".\decoder.c", line 558: cc0513: {D} warning: a value of type "unsigned char" cannot be assigned to an entity of type "struct <unnamed> *" decoder->sync = syncDat[0]; ^ Its obvious Im not completely sure what Im doing here. Any advice would be appreciated. Thanks!
Matt
This is really a C question. You could use either: decoder->sync = &syncDat[0]; or decoder->sync = syncDat;
And both of these assume that decoder->sync point to an array of unsigned char, ie defined unsigned char *syncDat; or equivalent in a struct.
HTH, Steve.
On Oct 13, 2005, at 7:55 PM, Steve Calfee wrote:
This is really a C question. You could use either: decoder->sync = &syncDat[0]; or decoder->sync = syncDat;
And both of these assume that decoder->sync point to an array of unsigned char, ie defined unsigned char *syncDat; or equivalent in a struct.
Further to what Steve and Andre have said:
C represents arrays as pointers; any pointer can be indexed as an array, and a reference to any array is a pointer. When you say syncDat [0], you're actually saying "Dereference the zeroth element of the array syncDat for me." & converts that back into a pointer, so &syncDat[0] means "dereference the zeroth element of syncDat, and then give me its address" - a no-op!
The reason syncDat and &syncDat[0] are the same is because syncDat already points at the zeroth element - it is just the memory address of the contiguous array in memory, and that memory address contains the zeroth element.
Understanding pointers and arrays in C is tough work, but once you've got it, you understand the language a lot more. Hope this shed some light on the subject for you.
Joe