<Vector> layout in memory
CASTalk.com Forum Index CASTalk.com
Discussion of DSP, FPGA, storage and embedded system.
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web castalk.com
layout in memory

 
Post new topic   Reply to topic    CASTalk.com Forum Index -> DSP
Author Message
Isaac Gerg
Guest





Posted: Wed Dec 15, 2004 10:37 pm    Post subject: layout in memory Reply with quote

How do vectors 'look' in memory? Are they the same as arrays?

Specifically, I have a complex vector container and I wish to pass it
to a complex8_t array. The complex vector contains the same struct
element as the complex8_t. Can i substitue (cast) pointers from the
vector type to complex8_t and get the same, correct results?

Isaac
Back to top
Tim Wescott
Guest





Posted: Wed Dec 15, 2004 11:01 pm    Post subject: Re: layout in memory Reply with quote

Isaac Gerg wrote:

Quote:
How do vectors 'look' in memory? Are they the same as arrays?

Specifically, I have a complex vector container and I wish to pass it
to a complex8_t array. The complex vector contains the same struct
element as the complex8_t. Can i substitue (cast) pointers from the
vector type to complex8_t and get the same, correct results?

Isaac

What language?

If you mean vectors in the C++ container class then you have absolutely
no guarantees of underlying storage. If you're talking C and you mean
something like

complex8_t array[16];

then you can make a pointer and set it equal to "array":

complex8_t * pointer = array;

As long as the value is retained "pointer[n]" will have the same logical
behavior as "array[n]".

If neither of my guesses are correct you need to tell us more about what
you're asking.

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com
Back to top
Isaac Gerg
Guest





Posted: Thu Dec 16, 2004 12:30 am    Post subject: Re: layout in memory Reply with quote

I want to swap a vector poitner with an array pointer in c++. If i do a
pfVec[i] will i get the same value as a pfArray[i]? Are vector data and
arrays stored teh same in memory with vectors having some other misc
storage to take care of sizing?

Isaac


Quote:

What language?

If you mean vectors in the C++ container class then you have absolutely
no guarantees of underlying storage. If you're talking C and you mean
something like

complex8_t array[16];

then you can make a pointer and set it equal to "array":

complex8_t * pointer = array;

As long as the value is retained "pointer[n]" will have the same logical
behavior as "array[n]".

If neither of my guesses are correct you need to tell us more about what
you're asking.
Back to top
Jerry Avins
Guest





Posted: Thu Dec 16, 2004 12:43 am    Post subject: Re: layout in memory Reply with quote

Isaac Gerg wrote:
Quote:
How do vectors 'look' in memory? Are they the same as arrays?

Specifically, I have a complex vector container and I wish to pass it
to a complex8_t array. The complex vector contains the same struct
element as the complex8_t. Can i substitue (cast) pointers from the
vector type to complex8_t and get the same, correct results?

Isaac

This is the second time around. In the final analysis, you are fetching
and storing bits from/in memory locations defined by addresses. While a
high-level language wraps those operations in certain useful
abstractions which depend on the particular language, there are times
when you need to distinguish between the abstractions and the operations
they model. This seems to be one of those times.

A fellow who was taking his master's degree in Computer Science once
asked me (in my capacity as a one-time designer of computer hardware)
how computer hardware distinguished ASCII from integers. When I told him
that software, not hardware, makes that distinction, he decided that I
was an antiquated ignoramus who hadn't kept up with the times. Such is
the power of abstraction to cloud the mind when accepted as reality.

Jerry
--
Engineering is the art of making what you want from things you can get.
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
Back to top
Tim Wescott
Guest





Posted: Thu Dec 16, 2004 1:00 am    Post subject: Re: layout in memory Reply with quote

Isaac Gerg wrote:

(top-posting fixed)
Quote:

What language?

If you mean vectors in the C++ container class then you have
absolutely no guarantees of underlying storage. If you're talking C
and you mean something like

complex8_t array[16];

then you can make a pointer and set it equal to "array":

complex8_t * pointer = array;

As long as the value is retained "pointer[n]" will have the same
logical behavior as "array[n]".

If neither of my guesses are correct you need to tell us more about
what you're asking.

I want to swap a vector poitner with an array pointer in c++. If i do
a pfVec[i] will i get the same value as a pfArray[i]? Are vector data
and arrays stored teh same in memory with vectors having some other
misc storage to take care of sizing?


Please clarify what you mean when you say "vector", then maybe I can
answer your question. C and C++ do not have "vectors" -- they have
pointers to memory and they have arrays. You can use a pointer to
memory to reference an array if you feel like it. But you can't use an
intrinsic "vector" type because there isn't one.

The C++ container class, which is _not_ part of the C++ language itself,
has various containers with "Vector" in the name, and which overload the
'[]' operator. Thus you can extract the contents of such a class with
exactly the same semantics as you can with a simple C array, but with
entirely different code.

So without knowing what you mean when you say "vector" you may as well
replace the word with "gblorg-snerg" everywhere it appears in your post:
it won't contribute any less to my understanding of your problem and it
won't distract me by it's implied functionality.

Give me a code snippet that shows what you mean by "vector" or
"gblorg-snerg" or whatever, and another one to show what you mean by
"array" so we know if it matches the one that shows up in all the C and
C++ books.

Then I, or nearly anyone else in the group, can answer your question.

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com
Back to top
Isaac Gerg
Guest





Posted: Thu Dec 16, 2004 2:09 am    Post subject: Re: layout in memory Reply with quote

Another newsgroup answered my question.

Arrays and the C++ vector contains store memory contiguious and
identical, thus, pointers to each are identical. I verfied this with
code on serveral platforms using different C++ compiliers.

Thanks

Jerry Avins wrote:
Quote:
Isaac Gerg wrote:

How do vectors 'look' in memory? Are they the same as arrays?

Specifically, I have a complex vector container and I wish to pass it
to a complex8_t array. The complex vector contains the same struct
element as the complex8_t. Can i substitue (cast) pointers from the
vector type to complex8_t and get the same, correct results?

Isaac


This is the second time around. In the final analysis, you are fetching
and storing bits from/in memory locations defined by addresses. While a
high-level language wraps those operations in certain useful
abstractions which depend on the particular language, there are times
when you need to distinguish between the abstractions and the operations
they model. This seems to be one of those times.

A fellow who was taking his master's degree in Computer Science once
asked me (in my capacity as a one-time designer of computer hardware)
how computer hardware distinguished ASCII from integers. When I told him
that software, not hardware, makes that distinction, he decided that I
was an antiquated ignoramus who hadn't kept up with the times. Such is
the power of abstraction to cloud the mind when accepted as reality.

Jerry
Back to top
Clay S. Turner
Guest





Posted: Thu Dec 16, 2004 4:45 am    Post subject: Re: layout in memory Reply with quote

----- Original Message -----
From: "Isaac Gerg" <isaac.gergNOSPAM@adelphia.net>
Newsgroups: comp.dsp
Sent: Wednesday, December 15, 2004 4:09 PM
Subject: Re: <Vector> layout in memory


Quote:
Another newsgroup answered my question.

Arrays and the C++ vector contains store memory contiguious and identical,
thus, pointers to each are identical. I verfied this with code on
serveral platforms using different C++ compiliers.


Hello Isaac,

If you are using the STL <vector> class, then the memory allocator for it
grabs ram in chunks from the heap. So your first x number of values will be
contiguous in ram, but as you add more items to the the container, it will
grow and thus periodically grabs more ram. These chunks will not necessarily
be contiguous. If you need for the ram to be this way, then overload the
container's allocator to grab a big enough chunk at one time to hold
everything you wish to hold.

For your experiment, try creating the vector and add several items. Then
dynamically create something else and then add more stuff to your vector.
You should be able to find discontinuities in the ram addressing. You can
use the size() and capacity() member functions to watch the memory
requirements of the container. IIRC, windows assigns ram in 4096 bytes
chunks.

Clay
Back to top
U-CDK_CHARLES\\Charles
Guest





Posted: Fri Dec 17, 2004 12:33 am    Post subject: Re: layout in memory Reply with quote

On Wed, 15 Dec 2004 18:50:01 -0500, Clay S. Turner
<Physics@Bellsouth.net> wrote:
Quote:

----- Original Message -----
From: "Isaac Gerg" <isaac.gergNOSPAM@adelphia.net
Newsgroups: comp.dsp
Sent: Wednesday, December 15, 2004 4:09 PM
Subject: Re: <Vector> layout in memory


Another newsgroup answered my question.

Arrays and the C++ vector contains store memory contiguious and identical,
thus, pointers to each are identical. I verfied this with code on
serveral platforms using different C++ compiliers.


Hello Isaac,

If you are using the STL <vector> class, then the memory allocator for it
grabs ram in chunks from the heap. So your first x number of values will be
contiguous in ram, but as you add more items to the the container, it will
grow and thus periodically grabs more ram. These chunks will not necessarily
be contiguous. If you need for the ram to be this way, then overload the
container's allocator to grab a big enough chunk at one time to hold
everything you wish to hold.


I recall reading references that stated the <vector T> was guaranteed to
be contiguous, and every implementation I've seen simply allocates and
reallocates an array via new.

But I've not read that this is required. The only "hint" that this
might be so is that any iterators pointing to a <vector T> are
invalidated after a resize.
Back to top
 
Post new topic   Reply to topic    CASTalk.com Forum Index -> DSP All times are GMT
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




VoIP Electronics Powered by phpBB