hello,
i've just encountered following piece of code:
struct Vector
{
float x, y, z;
inline float & operator[] (size_t i)
{
assert(i<3);
return *(&x + i);
}
};
i think it can work due to placement in memory, but i'd like to ask
you
whether it is a good and safe programming practice or not.
many thanks in advance,
mojmir 10 1356
mojmir wrote:
hello,
i've just encountered following piece of code:
struct Vector
{
float x, y, z;
inline float & operator[] (size_t i)
{
assert(i<3);
return *(&x + i);
}
};
i think it can work due to placement in memory, but i'd like to ask
you
whether it is a good and safe programming practice or not.
many thanks in advance,
mojmir
the only fact that you don't know if it's a good practice, it means that
is not a good practice (i.e., adds complexity where it can be avoided).
Anyway, afaik is not safe due to the fact that there cen be alignement
requirements for x, y, z.
Regards,
Zeppe
On 2007-10-25 14:23, mojmir wrote:
hello,
i've just encountered following piece of code:
struct Vector
{
float x, y, z;
inline float & operator[] (size_t i)
{
assert(i<3);
return *(&x + i);
}
};
i think it can work due to placement in memory, but i'd like to ask
you whether it is a good and safe programming practice or not.
The important word is *can*. The above code can work due to placement in
memory, but it can also not work due to placement in memory. A safer
version could look like this:
struct vector
{
float x, y, z;
float& operator[](size_t i)
{
assert(0 <= i && i <3);
return i < 2 ? i == 0 ? x : y : z;
}
};
The body of the [] operator can be replaced with if-statements if you
like that better.
--
Erik Wikström
On Oct 25, 4:24 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-10-25 14:23, mojmir wrote:
hello,
i've just encountered following piece of code:
struct Vector
{
float x, y, z;
inline float & operator[] (size_t i)
{
assert(i<3);
return *(&x + i);
}
};
i think it can work due to placement in memory, but i'd like to ask
you whether it is a good and safe programming practice or not.
The important word is *can*. The above code can work due to placement in
memory, but it can also not work due to placement in memory. A safer
version could look like this:
struct vector
{
float x, y, z;
float& operator[](size_t i)
{
assert(0 <= i && i <3);
return i < 2 ? i == 0 ? x : y : z;
}
};
The body of the [] operator can be replaced with if-statements if you
like that better.
--
Erik Wikström- Hide quoted text -
- Show quoted text -
Wait a sec!!! why not just a simple array?
enum{x=0,y=1,z= 2};
typedef float vec3d[z+1];
vec3d v={1,2,3};
{//in some function
v[x]=....
v[y]=...
v[z]=...
}
thanks,
FM.
On Oct 25, 7:19 pm, terminator <farid.mehr...@ gmail.comwrote:
On Oct 25, 4:24 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-10-25 14:23, mojmir wrote:
hello,
i've just encountered following piece of code:
struct Vector
{
float x, y, z;
inline float & operator[] (size_t i)
{
assert(i<3);
return *(&x + i);
}
};
i think it can work due to placement in memory, but i'd like to ask
you whether it is a good and safe programming practice or not.
The important word is *can*. The above code can work due to placement in
memory, but it can also not work due to placement in memory. A safer
version could look like this:
struct vector
{
float x, y, z;
float& operator[](size_t i)
{
assert(0 <= i && i <3);
return i < 2 ? i == 0 ? x : y : z;
}
};
The body of the [] operator can be replaced with if-statements if you
like that better.
--
Erik Wikström- Hide quoted text -
- Show quoted text -
Wait a sec!!! why not just a simple array?
enum{x=0,y=1,z= 2};
typedef float vec3d[z+1];
vec3d v={1,2,3};
{//in some function
v[x]=....
v[y]=...
v[z]=...
}
thanks,
FM.- Hide quoted text -
because it is unsecure on array bounds.
On Oct 25, 5:26 pm, terminator <farid.mehr...@ gmail.comwrote:
On Oct 25, 7:19 pm, terminator <farid.mehr...@ gmail.comwrote: On Oct 25, 4:24 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-10-25 14:23, mojmir wrote:
hello,
i've just encountered following piece of code:
struct Vector
{
float x, y, z;
inline float & operator[] (size_t i)
{
assert(i<3);
return *(&x + i);
}
};
i think it can work due to placement in memory, but i'd like to ask
you whether it is a good and safe programming practice or not.
The important word is *can*. The above code can work due to placementin
memory, but it can also not work due to placement in memory. A safer
version could look like this:
struct vector
{
float x, y, z;
float& operator[](size_t i)
{
assert(0 <= i && i <3);
return i < 2 ? i == 0 ? x : y : z;
}
};
The body of the [] operator can be replaced with if-statements if you
like that better.
--
Erik Wikström- Hide quoted text -
- Show quoted text -
Wait a sec!!! why not just a simple array?
enum{x=0,y=1,z= 2};
typedef float vec3d[z+1];
vec3d v={1,2,3};
{//in some function
v[x]=....
v[y]=...
v[z]=...
}
thanks,
FM.- Hide quoted text -
because it is unsecure on array bounds.
and "return *(&x + i);" isn't ???
On 2007-10-25 18:56, ho******@gmail. com wrote:
On Oct 25, 5:26 pm, terminator <farid.mehr...@ gmail.comwrote:
>On Oct 25, 7:19 pm, terminator <farid.mehr...@ gmail.comwrote: On Oct 25, 4:24 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-10-25 14:23, mojmir wrote:
hello,
i've just encountered following piece of code:
struct Vector
{
float x, y, z;
inline float & operator[] (size_t i)
{
assert(i<3);
return *(&x + i);
}
};
i think it can work due to placement in memory, but i'd like to ask
you whether it is a good and safe programming practice or not.
The important word is *can*. The above code can work due to placement in
memory, but it can also not work due to placement in memory. A safer
version could look like this:
struct vector
{
float x, y, z;
float& operator[](size_t i)
{
assert(0 <= i && i <3);
return i < 2 ? i == 0 ? x : y : z;
}
};
The body of the [] operator can be replaced with if-statements if you
like that better.
--
Erik Wikström- Hide quoted text -
- Show quoted text -
Wait a sec!!! why not just a simple array?
enum{x=0,y=1,z= 2};
typedef float vec3d[z+1];
vec3d v={1,2,3};
{//in some function
v[x]=....
v[y]=...
v[z]=...
}
thanks,
FM.- Hide quoted text -
because it is unsecure on array bounds.
and "return *(&x + i);" isn't ???
Not with a proper assert on the value of i.
--
Erik Wikström
because it is unsecure on array bounds.
>
and "return *(&x + i);" isn't ???
Not with a proper assert on the value of i.
yes, the assert is there.
so in my case it's working because sizeof(float)=4 , sizeof(Vector)= 12
and the
machine word is 32b. but if i move to 64bit platform for example, the
&x+1 will
eventually point to 4 padding bytes inserted by compiler, right?
regards,
mojmir
On Oct 26, 10:33 am, mojmir <svobod...@gmai l.comwrote:
>because it is unsecure on array bounds.
and "return *(&x + i);" isn't ???
Not with a proper assert on the value of i.
yes, the assert is there.
so in my case it's working because sizeof(float)=4 , sizeof(Vector)= 12
and the
machine word is 32b. but if i move to 64bit platform for example, the
&x+1 will
eventually point to 4 padding bytes inserted by compiler, right?
consider:
class A a;
size_t n;
then following is always true:
&A+n == static_cast<A*> (static_cast<si ze_t>(&A)+n*siz eof(A))
So your code works fine provided that overflow does not happen (that
is you do not index the vector with n 2).But Erik`s code never
overflows even if the assertion is omitted.
regards,
FM.
On Oct 25, 2:24 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-10-25 14:23, mojmir wrote:
i've just encountered following piece of code:
struct Vector
{
float x, y, z;
inline float & operator[] (size_t i)
{
assert(i<3);
return *(&x + i);
}
};
i think it can work due to placement in memory, but i'd like to ask
you whether it is a good and safe programming practice or not.
The important word is *can*. The above code can work due to placement in
memory, but it can also not work due to placement in memory. A safer
version could look like this:
struct vector
{
float x, y, z;
float& operator[](size_t i)
{
assert(0 <= i && i <3);
return i < 2 ? i == 0 ? x : y : z;
}
};
The body of the [] operator can be replaced with if-statements
if you like that better.
Or a switch. Alternatively, you could declare an array, and
provide functions "x() { return values[ 0 ] ; }", etc.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Scott J. McCaughrin |
last post by:
The following program compiles fine but elicits this message from the
linker: "undefined reference to VarArray::funct" and thus fails.
It seems to behave as if the static data-member: VarArray::funct were
an extern, but it is declared in the same file (q.v.). What is the
remedy for this?
=================
|
by: blueblueblue2005 |
last post by:
here is a friend function of Class Array, which has two private data
member: int size, int *ptr
// Array's public member function to return size
int getSize() const { return size; }
friend istream &operator>>(istream &in, Array &a)
{
for(int i=0; i<a.size; i++)
// do something
|
by: Kaz Kylheku |
last post by:
Given some class C with array T x, is it possible to get a
pointer-to-data-member to one of the elements?
&C::x gives us a pointer-to-member-array: T (C::*).
But I just want to get a T C::* pointing to a selected array element,
so that I can later use an instance c of that class to pick out that
array element: c->*ptr.
This syntax, for instance, doesn't work: &C::x. My compiler thinks
|
by: Steve Rencontre |
last post by:
I can't for the life of me see how to do pointer-to-member when the
member is actually part of an embedded structure.
That is, if I have:
struct S1 { int a; };
struct S2 { S1 s; int b; };
how can I get a pointer to the a in an S2?
|
by: partha.p.das |
last post by:
Here is my class:
class MyClass {
public:
MyClass();
~MyClass();
private:
| |
by: sherifffruitfly |
last post by:
Hi,
I'm just learning cpp, and the exercise I'm working on is basically as
follows:
1) Create a struct type with 4 members (char, char, char, int).
2) Create an array of, say 3 instances of the struct, and populate them
with data.
3) cin 1, 2, 3, or 4 from the user
4) If the user selected, say, 2, display the contents of the 2nd data
|
by: Bala L |
last post by:
I have a class with a private array data member 'm_array'. I have
written a public function, called 'fileRead', to read values into the
array from a file. I just noticed that I have declared this function to
be const.
I dont understand how the code compiled and executed without returning
an error or a warning. I am assigning values from a file to m_array
inside this function. I thought that a member function can be made
const only if...
|
by: anonymous |
last post by:
I'm having some trouble understanding the meaning of visibility.
<?php
class foobar {
private $key1 = "cat";
private $key2 = "apple";
protected $key3 = "book";
public $key4 = 42;
}
|
by: huili80 |
last post by:
Say I have two classes:
class A
{
public:
int x;
};
class B
{
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |