Archived

This topic is now archived and is closed to further replies.

xe_sphinx

array manipultaion via unios

Recommended Posts

Hi everybody. I have heard of a technique that could solve all my problems. Instead of using a linkedliist I could use an array of classes. The array is defined as follows: const int number; class List[number]; I have heard that I can manipulate ''number'' via unions. If I am right than a union is something like: struct { union { const int number; int number2; } }num; Then my declaration of list would be; class List[num.number]; If I change the value of number2, then the value of the constant is changed automatically, because it uses the same memory as number2. Please tell me If I am right and if yes, whether this can be effeciently used instead of a linked list or whether there can occur memory errors. (I AM SORRY IF THE SYNTAX IS WRONG, BECAUSE I JUST HAVE HEARD VERY ROUGH INFOS ABOUR UNIONS). Windows (N): A 32 Bit patch to a 16 bit graphical interface based on a 8 bit operating system originaly encoded for a 4 bit processor written by a 2 bit company that can''''t stand 1 bit of competition.

Share this post


Link to post
Share on other sites
Well you can change the value of number that way although there are easier ways.

However doing this will not change the length of your array. It doesn''t do anything to help you make a linked list.

-Mike

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
here''s what I do when I want a list:

#include
using namespace std;
list listOfInts;//declares a list

or if I want something more like an array, but it resizes to hold more stuff, I use a vector (silly name but a good class).

#include
using namespace std;
vector vectorOfInts;//declares a vector


the standard libary is part of the standard for a reason, use it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
oops, used the wrong tags. After the includes you want list and vector (without .h) in angle brackets. In the declarations you want the type following the data structure name, also in angle brackets. Maybe this will work:

  
#include <list>
#include <vector>
list<int> listOfInts;
vector<int> vectorOfInts;

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
...vector... silly name but a good class...

Actually, it's a very good name (if you can get away from the mathematical/physical definition of vectors).

Anyway, xe_sphinx, use STL vectors:

#include <vector>
typedef vector<MyClass> MyClassVector; // for convenience
typedef vector<MyClass *> MyClassPtrVector;
MyClassVector vec;
MyClassPtrVector vec_ptr;

//
// to add to the vector:
vec.push_back(obj); // this actually makes a copy of the object
vec_ptr.push_back(&obj); // this copies the address of the object


[EDIT] Standards compliance...



I wanna work for Microsoft!

Edited by - Oluseyi on November 7, 2001 5:31:50 PM

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
By fully standard-compliant browsers I suppose you mean "anything other than IE" I shall now go back where I can and make the necessary adjustments. Thanks.


Well I don''t want to single out IE, since I''m not sure how many browsers have that problem. I always assumed it to be a long running typo on your part . Then I finally figured that I should open IE and check to make sure it didn''t work in it either, but, to my surprise, it did work, heh.

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
Using ANSI-C, you could theoretically do this...

union A {
unsigned short x;
struct y {
unsigned char lo;
unsigned char hi;
};
}

Which makes A act as a 16 bit accumulator containing 2 8bit accumulators. Or, in other words, gives you better access to the bytes contained in the word. The only problem with this is that it''s very machine specific. Pentium PCs have no problem with this, since they have Low Endian-ness and an 8bit word data bus. But if you have a machine with high endianess and a 16bit word data bus, A.y.lo would give the high 8bits and the low 8bits would be unaccessible through this.

By the way, this is just a way to get around A<<8 or A&255 ... Why bother you say? I don''t know, its just a nice trick. If you use this on a machine that doesn''t have 8bit registers, then the C compiler would put in the shifts and ands anyways, so you wouldn''t save on code size there.

:: Inmate2993
:: William C. Bubel
"Please refrain from bothering Booster."

Share this post


Link to post
Share on other sites
You probably want to use the STL vector, but for future reference:

  
template<typename T>
class CArray
{
CArray(int iSize) : m_iSize(iSize)
{
m_pData = new T[m_iSize];
};
~CArray()
{
delete[] m_pData;
}
T& operator[](int i)
{
_ASSERT(i>=0 && i<m_iSize);
return m_pData[i];
}
const int m_iSize;
T* m_pData;
};

If you want the size to be constant, do that. If it''s not constant, don''t fudge it with a union.

Magmai Kai Holmlor
- Not For Rent

Share this post


Link to post
Share on other sites