Dynamic and Static Allocation

M

Muhammed Shafi

Hi ,

What is the corresponding static allocation of this statement which
dynamically allocates some memory.

for(int i=0;i<2;i++)
{
TxControlBuffers.Data = malloc(sizeof(char) * 64);
}

where 'Data' is of type 'void * '.

Is this right?

TxControlBuffers.Data=OurTxData[sizeof(char) * 64];

TxControlBuffers.Data=(s32_t*)&(OurTxData[i*64]);

OurTxData is an integer array of size 128.
 
I

Ian Collins

Hi ,

What is the corresponding static allocation of this statement which
dynamically allocates some memory.

for(int i=0;i<2;i++)
{
TxControlBuffers.Data = malloc(sizeof(char) * 64);


sizeof(char) is one by definition, so you can save some typing.
}

where 'Data' is of type 'void * '.

Is this right?

TxControlBuffers.Data=OurTxData[sizeof(char) * 64];


Given OurTxData is an int array, that will fail to compile.
TxControlBuffers.Data=(s32_t*)&(OurTxData[i*64]);


That will assign the address of an array element to Data, which is
possibly what you want. Although the dynamic allocation allocates a
block of 64 bytes, this will get addresses in steps of 64*sizeof(int).

The cast is unnecessary.
 
S

Shao Miller

Hi ,

What is the corresponding static allocation of this statement which
dynamically allocates some memory.

for(int i=0;i<2;i++)
{
TxControlBuffers.Data = malloc(sizeof(char) * 64);
}


static unsigned char OurTxData[64];
int i;

for (i = 0; i < 2; i++) {
TxControlBuffers.Data = OurTxData;
}
where 'Data' is of type 'void * '.

Is this right?

TxControlBuffers.Data=OurTxData[sizeof(char) * 64];


If the elements are 'int', you probably don't wish to assign an 'int' to
the 'void *' here.
TxControlBuffers.Data=(s32_t*)&(OurTxData[i*64]);

OurTxData is an integer array of size 128.


No need to cast because you are assigning to a 'void *'. Also:

&(OurTxData[i * 64])

is the same as:

OurTxData + i * 64

And which leads to a question: Are you trying to jump ahead 64 bytes on
each iteration? If so, OurTxData[] ought to be larger than 64 bytes.
Otherwise, after one iteration, you've already passed all elements.
 
S

Shao Miller

Oops. I'm sorry. My post had errors. :)

Hi ,

What is the corresponding static allocation of this statement which
dynamically allocates some memory.

for(int i=0;i<2;i++)
{
TxControlBuffers.Data = malloc(sizeof(char) * 64);
}


static unsigned char OurTxData[64];
int i;

for (i = 0; i < 2; i++) {
TxControlBuffers.Data = OurTxData;
}


I should have written:

static s32_t OurTxData[2][64];
int i;

for (i = 0; i < 2; i++) {
TxControlBuffers.Data = OurTxData;
}
where 'Data' is of type 'void * '.

Is this right?

TxControlBuffers.Data=OurTxData[sizeof(char) * 64];


If the elements are 'int', you probably don't wish to assign an 'int' to
the 'void *' here.
TxControlBuffers.Data=(s32_t*)&(OurTxData[i*64]);

OurTxData is an integer array of size 128.


No need to cast because you are assigning to a 'void *'. Also:

&(OurTxData[i * 64])

is the same as:

OurTxData + i * 64

And which leads to a question: Are you trying to jump ahead 64 bytes on
each iteration? If so, OurTxData[] ought to be larger than 64 bytes.
Otherwise, after one iteration, you've already passed all elements.


I got scrambled while reading your post. Again, sorry about that.

Just to confirm: Are you trying to use a static buffer which contains
two sets of 64, 32-bit, signed integers?
 
M

Muhammed Shafi

Oops.  I'm sorry.  My post had errors. :)

Hi ,
What is the corresponding static allocation of this statement which
dynamically allocates some memory.
for(int i=0;i<2;i++)
{
TxControlBuffers.Data = malloc(sizeof(char) * 64);
}

static unsigned char OurTxData[64];
int i;
for (i = 0; i < 2; i++) {
TxControlBuffers.Data = OurTxData;
}


I should have written:

   static s32_t OurTxData[2][64];
   int i;

   for (i = 0; i < 2; i++) {
       TxControlBuffers.Data = OurTxData;
     }










where 'Data' is of type 'void * '.
Is this right?
TxControlBuffers.Data=OurTxData[sizeof(char) * 64];

If the elements are 'int', you probably don't wish to assign an 'int' to
the 'void *' here.
TxControlBuffers.Data=(s32_t*)&(OurTxData[i*64]);
OurTxData is an integer array of size 128.

No need to cast because you are assigning to a 'void *'. Also:
&(OurTxData[i * 64])
is the same as:
OurTxData + i * 64
And which leads to a question: Are you trying to jump ahead 64 bytes on
each iteration? If so, OurTxData[] ought to be larger than 64 bytes.
Otherwise, after one iteration, you've already passed all elements.

I got scrambled while reading your post.  Again, sorry about that.

Just to confirm: Are you trying to use a static buffer which contains
two sets of 64, 32-bit, signed integers?


Thanks for the reply...

Yes...I am trying to use a static buffer which contains 2 sets of 64,
32 - bit , signed integers.
 
S

Shao Miller

On 5/19/2011 10:57 PM, Muhammed Shafi wrote:
Hi ,
What is the corresponding static allocation of this statement which
dynamically allocates some memory.
for(int i=0;i<2;i++)
{
TxControlBuffers.Data = malloc(sizeof(char) * 64);
}


static s32_t OurTxData[2][64];
int i;

for (i = 0; i< 2; i++) {
TxControlBuffers.Data = OurTxData;
}
Is this right?

Just to confirm: Are you trying to use a static buffer which contains
two sets of 64, 32-bit, signed integers?


Thanks for the reply...

Yes...I am trying to use a static buffer which contains 2 sets of 64,
32 - bit , signed integers.


Ok. The code above should work for you (assuming you have such an
integer type called 's32_t'). :)

Something else I enjoy is avoiding hard-coded values in loop conditions.
For example:

/*** Constants */
enum {
CvNumberOfBuffers = 2,
CvBufferElementCount = 64,
CvZero = 0
};


/*** Objects */

/* Our buffer area */
static s32_t OurTxData[CvNumberOfBuffers][CvBufferElementCount];


/*** Function definitions */

/* Setup the TxControlBuffers */
int setup_tx_control_buffers(void) {
int rc;
int i;

for (i = 0; i < sizeof OurTxData / sizeof *OurTxData; i++) {
TxControlBuffers.Data = OurTxData;
}
/* ...Other stuff... */
return rc;
}

So now if you ever change your mind and have 3 buffers, you change only
one line of code, in the constants area.

I hope this is useful for you. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top