I have a template class that works fine when I implement it with <int>,
but when I use <floator <doubleit doesn't work. The class has a
dynamic array of type T that gets instantiated in my constructor. When
type T is int, the array works like I expect. But when I use double or
float, the array points to garbage and any updates to array elements
make no difference.
I'm using Visual Studio 2005 on an XP pro machine. The following is
the code that is giving me problems. If anyone can help explain what
I'm doing wrong and why this doesn't work, I'd really appreciate it.
Thanks in advance.
template <class T>
class Tuple
{
public:
Tuple<T>(void);
Tuple<T>(T x, T y);
~Tuple<T>(void) ;
T getX();
T getY();
void setX(T x);
void setY(T y);
private:
T *_values; //arrray of values
};
template <class T>
Tuple<T>::Tuple (void)
{
_values = new T[2];
_values[0] = 0;
_values[1] = 0;
}
template <class T>
Tuple<T>::Tuple (T x, T y)
{
_values = new T[2];
_values[0] = x;
_values[1] = y;
}
template <class T>
Tuple<T>::~Tupl e(void)
{
}
template <class T>
T Tuple<T>::getX( ) { return _values[0]; }
template <class T>
T Tuple<T>::getY( ) { return _values[1]; }
template <class T>
void Tuple<T>::setX( T x) { _values[0] = x; }
template <class T>
void Tuple<T>::setY( T y) { _values[1] = y; } 11 1628 ho************* *******@gmail.c om wrote:
I have a template class that works fine when I implement it with <int>,
but when I use <floator <doubleit doesn't work. The class has a
dynamic array of type T that gets instantiated in my constructor. When
type T is int, the array works like I expect. But when I use double or
float, the array points to garbage and any updates to array elements
make no difference.
I'm using Visual Studio 2005 on an XP pro machine. The following is
the code that is giving me problems. If anyone can help explain what
I'm doing wrong and why this doesn't work, I'd really appreciate it.
Thanks in advance.
template <class T>
class Tuple
{
public:
Tuple<T>(void);
Tuple<T>(T x, T y);
~Tuple<T>(void) ;
T getX();
T getY();
void setX(T x);
void setY(T y);
private:
T *_values; //arrray of values
};
template <class T>
Tuple<T>::Tuple (void)
{
_values = new T[2];
_values[0] = 0;
_values[1] = 0;
}
template <class T>
Tuple<T>::Tuple (T x, T y)
{
_values = new T[2];
_values[0] = x;
_values[1] = y;
}
template <class T>
Tuple<T>::~Tupl e(void)
{
}
template <class T>
T Tuple<T>::getX( ) { return _values[0]; }
template <class T>
T Tuple<T>::getY( ) { return _values[1]; }
template <class T>
void Tuple<T>::setX( T x) { _values[0] = x; }
template <class T>
void Tuple<T>::setY( T y) { _values[1] = y; }
Hmm.. I used VC++ 2005 Express edition on an XP machine and this code
seemed to work fine for me. ho************* *******@gmail.c om wrote:
I have a template class that works fine when I implement it with <int>,
but when I use <floator <doubleit doesn't work. The class has a
dynamic array of type T that gets instantiated in my constructor. When
type T is int, the array works like I expect. But when I use double or
float, the array points to garbage and any updates to array elements
make no difference.
I'm using Visual Studio 2005 on an XP pro machine. The following is
the code that is giving me problems. If anyone can help explain what
I'm doing wrong and why this doesn't work, I'd really appreciate it.
Thanks in advance.
template <class T>
class Tuple
{
public:
Tuple<T>(void);
Tuple<T>(T x, T y);
~Tuple<T>(void) ;
T getX();
T getY();
void setX(T x);
void setY(T y);
private:
T *_values; //arrray of values
};
template <class T>
Tuple<T>::Tuple (void)
{
_values = new T[2];
_values[0] = 0;
_values[1] = 0;
}
template <class T>
Tuple<T>::Tuple (T x, T y)
{
_values = new T[2];
_values[0] = x;
_values[1] = y;
}
template <class T>
Tuple<T>::~Tupl e(void)
{
}
template <class T>
T Tuple<T>::getX( ) { return _values[0]; }
template <class T>
T Tuple<T>::getY( ) { return _values[1]; }
template <class T>
void Tuple<T>::setX( T x) { _values[0] = x; }
template <class T>
void Tuple<T>::setY( T y) { _values[1] = y; }
I am willing to bet that your test code has undefined behavior. Your
template class has serious issues:
a) The compiler provided copy constructor and assignment operator will copy
pointers instead of the tuples pointed to. Thus your class has reference
semantics instead of value semantics.
b) The memory new[]ed in the constructor is never delete[]d.
Both pitfalls will very likely cause bugs that manifest themselves in UB.
Thus, all bets are off.
Best
Kai-Uwe Bux
Kai-Uwe Bux wrote:
I am willing to bet that your test code has undefined behavior. Your
template class has serious issues:
a) The compiler provided copy constructor and assignment operator will copy
pointers instead of the tuples pointed to. Thus your class has reference
semantics instead of value semantics.
He is using a dynamic array... so if he used the copy constructor, he
would copy the reference to that array... so basically he would re-use
the existing array. This may be an unintentional bug (or exactly what
he wants, I don't know his semantics), but it would not cause a
difference between an array of ints, floats and doubles.
However, use of the copy constructor may indeed provide unextpected
behavior. If the testcode for ints and floats/doubles were somehow
different and the copy constructor was used in the testcode, this could
be a problem area
>
b) The memory new[]ed in the constructor is never delete[]d.
You would leak the memory if the Tuple is deleted, which may
eventually result in UB, but still doesn't explain a difference between
floats/doubles and ints.
bjeremy wrote:
>
Kai-Uwe Bux wrote:
>I am willing to bet that your test code has undefined behavior. Your template class has serious issues:
a) The compiler provided copy constructor and assignment operator will copy pointers instead of the tuples pointed to. Thus your class has reference semantics instead of value semantics.
He is using a dynamic array... so if he used the copy constructor, he
would copy the reference to that array... so basically he would re-use
the existing array. This may be an unintentional bug (or exactly what
he wants, I don't know his semantics), but it would not cause a
difference between an array of ints, floats and doubles.
However, use of the copy constructor may indeed provide unextpected
behavior. If the testcode for ints and floats/doubles were somehow
different and the copy constructor was used in the testcode, this could
be a problem area
>> b) The memory new[]ed in the constructor is never delete[]d.
You would leak the memory if the Tuple is deleted, which may
eventually result in UB, but still doesn't explain a difference between
floats/doubles and ints.
Since there is nothing in the code posted that would explain a difference
between int and float/double, I conjectured that the test code invokes UB.
Thus, I pointed out the "features" in the posted snippet that may cause
test code to be undefined.
Best
Kai-Uwe Bux ho************* *******@gmail.c om wrote:
If you are not learn design of classes, but learn C++ class's
implementation, then
replace:
T getX();
T getY();
to
T getX()const;
T getY()const;
and
void setX(T x);
void setY(T y);
to
void setX(const T& x);
void setY(const T& y);
and
template <class T>
Tuple<T>::Tuple (void)
{
_values = new T[2];
_values[0] = 0;
_values[1] = 0;
}
to
/*Tuple<T>::*/ enum { item_x=0, item_y, total_items };
template <class T>
Tuple<T>::Tuple ():_values(0)
{
//new must not return NULL
_values = new T[total_items];
_values[item_x] = 0;
_values[item_y] = 0;
}
and
template <class T>
Tuple<T>::Tuple (T x, T y)
{
_values = new T[2];
_values[0] = x;
_values[1] = y;
}
to
template <class T>
Tuple<T>::Tuple (const T& x, const T& y):_values(0)
{
//new must not return NULL
_values = new T[total_items];
_values[item_x] = x;
_values[item_y] = y;
}
and
template <class T>
Tuple<T>::~Tupl e(void)
{
}
to
template <class T>
Tuple<T>::~Tupl e(){ delete _values; _values=0; }
and
template <class T>
T Tuple<T>::get?( ) { return _values[?]; }
to
template <class T>
T Tuple<T>::get?( )const { return _values[item_?]; }
and
template <class T>
void Tuple<T>::set?( T ?) { _values[?] = ?; }
to
template <class T>
void Tuple<T>::set?( const T& ?) { _values[item_?] = ?; }
And create at least
template <class T>
class Tuple
{
....
private:
Tuple(const Tuple& ):_values(0){ab ort();}
void Tuple<T>::opera tor = (const Tuple& ){abort();}
};
And if you are using exceptions, define throw(type) for all class's
members.
But when I use double or
float, the array points to garbage and any updates to array elements
make no difference.
Do printing of data for set/get members. ho************* *******@gmail.c om wrote:
If you are not learn design of classes, but learn C++ class's
implementation, then
replace:
T getX();
T getY();
to
T getX()const;
T getY()const;
and
void setX(T x);
void setY(T y);
to
void setX(const T& x);
void setY(const T& y);
and
template <class T>
Tuple<T>::Tuple (void)
{
_values = new T[2];
_values[0] = 0;
_values[1] = 0;
}
to
/*Tuple<T>::*/ enum { item_x=0, item_y, total_items };
template <class T>
Tuple<T>::Tuple ():_values(0)
{
//new must not return NULL
_values = new T[total_items];
_values[item_x] = 0;
_values[item_y] = 0;
}
and
template <class T>
Tuple<T>::Tuple (T x, T y)
{
_values = new T[2];
_values[0] = x;
_values[1] = y;
}
to
template <class T>
Tuple<T>::Tuple (const T& x, const T& y):_values(0)
{
//new must not return NULL
_values = new T[total_items];
_values[item_x] = x;
_values[item_y] = y;
}
and
template <class T>
Tuple<T>::~Tupl e(void)
{
}
to
template <class T>
Tuple<T>::~Tupl e(){ delete _values; _values=0; }
and
template <class T>
T Tuple<T>::get?( ) { return _values[?]; }
to
template <class T>
T Tuple<T>::get?( )const { return _values[item_?]; }
and
template <class T>
void Tuple<T>::set?( T ?) { _values[?] = ?; }
to
template <class T>
void Tuple<T>::set?( const T& ?) { _values[item_?] = ?; }
And create at least
template <class T>
class Tuple
{
....
private:
Tuple(const Tuple& ):_values(0){ab ort();}
void Tuple<T>::opera tor = (const Tuple& ){abort();}
};
And if you are using exceptions, define throw(type) for all class's
members.
But when I use double or
float, the array points to garbage and any updates to array elements
make no difference.
Do printing of data for set/get members.
On 2007-01-04 03:31, ho************* *******@gmail.c om wrote:
I have a template class that works fine when I implement it with <int>,
but when I use <floator <doubleit doesn't work. The class has a
dynamic array of type T that gets instantiated in my constructor. When
type T is int, the array works like I expect. But when I use double or
float, the array points to garbage and any updates to array elements
make no difference.
I'm using Visual Studio 2005 on an XP pro machine. The following is
the code that is giving me problems. If anyone can help explain what
I'm doing wrong and why this doesn't work, I'd really appreciate it.
Thanks in advance.
template <class T>
class Tuple
{
public:
Tuple<T>(void);
Tuple<T>(T x, T y);
~Tuple<T>(void) ;
T getX();
T getY();
void setX(T x);
void setY(T y);
private:
T *_values; //arrray of values
};
Unless you really want the functionality that bjeremy pointed out (when
copying Tuples) I can't see any good reason to use a dynamic array to
store the elements. Alternatives are to use an array which is a member
(non-dynamic) or even better pure members:
T _values[2];
or
T first;
T second;
Either way you don't have to worry about memory leaks.
As for the changes of behavior; use the debugger. VS2005 has excellent
debugging facilities.
--
Erik Wikström
Thanks for the help everyone. Unfortunately, nothing I change seems to
be helping, so perhaps my issue is related to the code I'm using to
debug the class. I tried running the following code on my original
code, then adding the copy constructor and "delete []_values;" to the
deconstructor, and then implenting all the changes that Grizlyk
suggested, and I got the same results each time. I've also tried this
on multiple machines running XP pro and also tried it with Visual
Studio 2005 and Visual Studio C++ Express Edition - no luck. Clearly
I'm doing something wrong. Any thoughts?
#include "Tuple.h"
#include <stdio.h>
int main()
{
Tuple<inttup1(1 , 2);
printf("tup1: %i, %i\n", tup1.getX(), tup1.getY()); //prints as
expected "tup1: 1, 2"
Tuple<floattup2 (2.2f, 2.5f);
printf("tup2: %d, %d\n", tup2.getX(), tup2.getY()); //prints garbage
getchar(); //stop the console window from closing
return 0;
}
Just to clarify, all of my class code is in the Tuple.h file, it is not
split between .h and .cpp.
Again, thanks in advance to any responses. ho************* *******@gmail.c om wrote:
Tuple<floattup2 (2.2f, 2.5f);
printf("tup2: %d, %d\n", tup2.getX(), tup2.getY()); //prints garbage
Because %d is not the formatting character for floating point numbers.
#include <iostream>
#include <cstdio>
int main(void)
{
double x = 5.5;
printf("%d", x);
std::cin.get();
}
I get 0 but it's totally unpredictable what will happen on any given
computer. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: will |
last post by:
All
I am trying to pass a simple xml file using xmlspy :
<?xml version="1.0" standalone="no"?>
<inventory>
<vehicle>
<make>Chevy</make>
|
by: Generic Usenet Account |
last post by:
I am seeing some unexpected behavior while using the STL "includes"
algorithm. I am not sure if this is a bug with the template header
files in our STL distribution, or if this is how it should work.
For your benefit, let me first quote from the STL Standard Reference
(Stepanov & Lee):
template <class InputIterator1, class InputIterator2>
bool includes(InputIterator1 first1, InputIterator1
last1,InputIterator2 first2, InputIterator2...
|
by: Jeff Louie |
last post by:
In C# (and C++/cli) the destructor will be called even if an exception
is thrown in the constructor. IMHO, this is unexpected behavior that can
lead to an invalid system state. So beware!
http://www.geocities.com/jeff_louie/oop30.htm
Regards,
Jeff
*** Sent via Developersdex http://www.developersdex.com ***
|
by: ashu |
last post by:
hi
look at this code
include <stdio.h>
int main(void)
{
int i,j=2;
i=j++ * ++j * j++;
printf("%d %d",i,j);
return 0;
|
by: Dimitri Furman |
last post by:
SQL Server 2000 SP4.
Running the script below prints 'Unexpected':
-----------------------------
DECLARE @String AS varchar(1)
SELECT @String = 'z'
IF @String LIKE ''
| |
by: stephen.diverdi |
last post by:
Can anyone lend a hand on getting this particular template
specialization working? I've been trying to compile with g++ 4.1 and
VS 2005.
//------------------------------------------------------------------
// my regular glass
class A { };
// my templated class
|
by: Kai-Uwe Bux |
last post by:
Juha Nieminen wrote:
I think your code violates the One-Definition-Rule. The template function
foo() is defined in two significantly different ways in the two files. As
far as I know, no diagnostic is required for such errors.
|
by: Unknownmat |
last post by:
Here is the smallest code I could come up with to reproduce the
I was able to reproduce the warning with an even smaller snippet of
code:
#include <vector>
#include <functional>
#include <boost/function.hpp>
int main()
|
by: JRough |
last post by:
I'm trying to use output buffering to cheat so i can print to excel
which is called later than this header().
header("Content-type: application/xmsdownload");
header("Content-Disposition: attachment;
header("Pragma: no-cache");
header("Expires; 0");
print "$header\n$data";
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |