Board index » cppbuilder » strucs and memory

strucs and memory


2008-04-08 06:06:04 AM
cppbuilder27
This question follows some previous struc questions.
I read data into a byte array and convert it to the following structure (which references data in the byte array)
struct _SAMP_BLK {
INT8U channelID; // Channel (band) ID for this blocks I/Q data
INT16S sampleI; // In Phase Sample
INT16S sampleQ; // Quadrature Phase Sample
};
Now, what if I want to convert the data to a structure identical to the one above, but with one more data member, not
part of the byte array itself, i.e. a structure like this
struct MY_SAMP_BLK {
INT8U channelID; // Channel (band) ID for this blocks I/Q data
INT16S sampleI; // In Phase Sample
INT16S sampleQ; // Quadrature Phase Sample
INT32S recordCount;
};
where the first 3 records, channelID, sampleI, sampleQ references memory in a byte array data[]. and record count is a "new" int.
Is that possible? Without any copying.
so 40 bits in data[i] correspond to the first 40 bits in MY_SAMP_BLK and the rest to some "new" memory.
Maybe such a new struc should be
struct MY_SAMP_BLK
{
_SAMP_BLK* aBlkPointer;
INT32S recordCount;
};
instead. which would only create one int and one _samplBLK pointer.
As you can understand, I'm not that used to dealing with memory on this level. Thanks for your patience!
-totte
 
 

Re:strucs and memory

Totte Karlsson wrote:
Quote
Is that possible? Without any copying.
so 40 bits in data[i] correspond to the first 40 bits in
MY_SAMP_BLK and the rest to some "new" memory.
You copy contiguous memory.
If the recordCount values are not contiguous with the rest of the
struct they must be assigned separately.
--
Liz
Please check the newsgroup guidelines and general netiquette
info.borland.com/newsgroups/guide.html
info.borland.com/newsgroups/guide.html
info.borland.com/newsgroups/netiquette.html
 

Re:strucs and memory

Liz Albin wrote:
Quote
Totte Karlsson wrote:

>Is that possible? Without any copying.
>so 40 bits in data[i] correspond to the first 40 bits in
>MY_SAMP_BLK and the rest to some "new" memory.

You copy contiguous memory.
Nope, I don't want to copy the memory. I want part of a structure referencing memory in another array. If possible.
I guess a pointer is the easiest?
 

{smallsort}

Re:strucs and memory

Totte Karlsson wrote:
Quote
Nope, I don't want to copy the memory. I want part of a structure
referencing memory in another array. If possible. I guess a
pointer is the easiest?
Not necessarily.
If the struct has a pointer
struct Sthing
{
T * pT;
int value;
};
what do you intend pointing your pointer at?
--
Liz
Please check the newsgroup guidelines and general netiquette
info.borland.com/newsgroups/guide.html
info.borland.com/newsgroups/guide.html
info.borland.com/newsgroups/netiquette.html
 

Re:strucs and memory

Quote

struct Sthing
{
T * pT;
int value;
};



what do you intend pointing your pointer at?

to memory in another array. that was my thought. Then I can assing anumber to value seperatly.
 

Re:strucs and memory

Totte Karlsson wrote:
Quote
>
>struct Sthing
>{
>T * pT;
>int value;
>};
>
>
>
>what do you intend pointing your pointer at?
>

to memory in another array. that was my thought. Then I can assing
anumber to value seperatly.
Well yes
you can have
Sthing temp;
T arrayofT[3];
temp.pT = arrayofT;
temp.value = 4;
(or whatever)
if that's what you meant
--
Liz
Please check the newsgroup guidelines and general netiquette
info.borland.com/newsgroups/guide.html
info.borland.com/newsgroups/guide.html
info.borland.com/newsgroups/netiquette.html
 

Re:strucs and memory

Quote
Well yes

you can have

Sthing temp;
T arrayofT[3];

temp.pT = arrayofT;
temp.value = 4;

(or whatever)

if that's what you meant
exactly. However, I thought maybe there is another way, where instead of having a pointer to the array,
actually pointing to a structure, I could have a new structure, where part of the memory is a reference to the
array and 32 bit is "new" memory for an additional struc member. But I guess that don't work?
struc new_struc
{
some
members
referencing ==>memory in data[]
memory in another array
int aNew_member;
}
-totte
 

Re:strucs and memory

"Totte Karlsson" < XXXX@XXXXX.COM >wrote in message
Quote
I guess a pointer is the easiest?
Yes, ie:
struct MY_SAMP_BLK
{
struct _SAMP_BLK *sample;
INT32S recordCount;
};
Then you can point the sample pointer to your byte array, and use the
recordCount separately.
Gambit
 

Re:strucs and memory

"Totte Karlsson" < XXXX@XXXXX.COM >wrote in message
Quote
I thought maybe there is another way, where instead of having
a pointer to the array, actually pointing to a structure, I could
have a new structure, where part of the memory is a reference
to the array
... aka, a pointer to the array...
Quote
struc new_struc
{
some
members
referencing ==>memory in data[]
memory in another array

int aNew_member;

}
The only way for that to work is to declare actual reference variables, ie:
struct MY_SAMP_BLK
{
INT8U &channelID;
INT16S &sampleI;
INT16S &sampleQ;
INT32S recordCount;
};
SAMP_BLK *sample = ...;
MY_SAMP_BLK my_sample = {sample->channelID, sample->sampleI,
sample->sampleQ, 0};
That is essentially the same as using explicit pointers instead:
struct MY_SAMP_BLK
{
INT8U *channelID;
INT16S *sampleI;
INT16S *sampleQ;
INT32S recordCount;
};
SAMP_BLK *sample = ...;
MY_SAMP_BLK my_sample;
my_sample.channelID = &(sample->channelID);
my_sample.sampleI = &(sample->sampleI);
my_sample.sampleQ = &(sample->sampleQ);
Since you are using pointers/references anyway, it is best to just have a
single pointer to the entire SAMP_BLK structure, not pointers to its
individual fields.
Gambit
 

Re:strucs and memory

Remy Lebeau (TeamB) wrote:
Quote
struct MY_SAMP_BLK
{
struct _SAMP_BLK *sample;
INT32S recordCount;
};

Then you can point the sample pointer to your byte array, and use the
recordCount separately.
SAMP_BLK *sample = reinterpret_cast<SAMP_BLK*>(aChannelSample->data);
MY_SAMP_BLK mysample[100];
for( int i=0; i<100; ++i )
{ mysample[i].sample = &sample[i];
mysample[i].recordCount = 0;
};
 

Re:strucs and memory

On Apr 8, 5:54 am, Bob Gonder < XXXX@XXXXX.COM >wrote:
Quote
Remy Lebeau (TeamB) wrote:
>struct MY_SAMP_BLK
>{
>struct _SAMP_BLK *sample;
>INT32S recordCount;
>};

>Then you can point the sample pointer to your byte array, and use the
>recordCount separately.

SAMP_BLK *sample = reinterpret_cast<SAMP_BLK*>(aChannelSample->data);
MY_SAMP_BLK mysample[100];

for( int i=0; i<100; ++i )
{ mysample[i].sample = &sample[i];
mysample[i].recordCount = 0;
};
or try and use unions n structures, use em just like structs
struct MY_SAMP_BLK
{
INT8U channelID;
INT16S sampleI;
INT16S sampleQ;
};
UNION{
SAMP_BLK *sample;
INT32S recordcount;
}
but the effect is one and the same like using 2 structures as Bob says.