This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/18534] New: cout operator<< not in right order


Running on a SuSe 9.1 pro x86_64 machine.

Reading specs from /usr/lib64/gcc/x86_64-suse-linux/3.4.3/specs
Configured with: ./configure --enable-threads=posix --prefix=/usr
--with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man
--enable-languages=c,c++,--disable-checking --libdir=/usr/lib64 --enable-libgcj
--with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib64 --with-system-zlib
--enable-shared --enable-__cxa_atexit x86_64-suse-linux : (reconfigured)
./configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local
--infodir=/usr/share/info --mandir=/usr/share/man --enable-languages=c,c++
--disable-checking --libdir=/usr/lib64 --with-gxx-include-dir=/usr/include/g++
--with-slibdir=/lib64 --with-system-zlib --enable-shared --enable-__cxa_atexit
x86_64-suse-linux
Thread model: posix
gcc version 3.4.3

I dont know if this really is a bug, but i thought you might want to know

I made a class that acts like a buffer, or actually is one. It has a get()
function that increments a pointer to the current item, making it point to the
next item in the buffer. When i do "cout << bufferclass.get() <<
bufferclass.get() << endl", it wont print it in the right order! (from left to
right).

For instance, i put 2 char's in it ('a', 'b'). Next i do the cout <<
bufferclass.get() << bufferclass.get() << endl", and it will print "ba".
When i do it in 2 steps, it will print it correct, so its not that my buffer is
screwed. Here is the code for the main:

int main()
{
	
	Chunk<char> bla(128);

	bla.add('a');
	bla.add('b');

	cout << bla.get() << bla.get() << endl; // Here is the error

	return 0;
}

The output:
eric@linux:~/Documents> ./bla
ba



Here is the Chunk class. As you can see i also put in a operator[], and it does
work. So bla[0] will return 'a' and bla[1] will return 'b'.

#include <cstdlib>
#include <memory>

template<class T>
class Chunk
{
public:
	Chunk() { data = current = avail = limit = 0; }
	~Chunk();
	
	explicit Chunk(const std::size_t&);
	
	bool add(const T&);
	T get();
	
	T& operator[](const std::size_t& i) { return data[i]; }
private:
	
	void alloc(const std::size_t&);
	void dealloc();
	
	std::allocator<T> mem;
	T* data;
	T* current;
	T* avail;
	T* limit;
};
template<class T>
Chunk<T>::~Chunk()
{
	dealloc();
}
template<class T>
Chunk<T>::Chunk(const std::size_t& s)
{
	alloc(s);
}
template<class T>
void Chunk<T>::alloc(const std::size_t& s)
{
	data = mem.allocate(s);
	current = avail = data;
	limit = data + s;
};
template<class T>
void Chunk<T>::dealloc()
{
	if (data)
	{
		T* iter = avail;
		while (iter != avail)
		{
			mem.destroy(--iter);
		}
		mem.deallocate(data, limit - data);
	}
	data = current = avail = limit = 0;
}
template<class T>
bool Chunk<T>::add(const T& t)
{
	if (avail == limit)
		return false;
	
	mem.construct(avail++, t);
	
	return true;
}
template<class T>
T Chunk<T>::get()
{
	T ret = *current;
	current++;
	return ret;
}

-- 
           Summary: cout operator<< not in right order
           Product: gcc
           Version: 3.4.3
            Status: UNCONFIRMED
          Severity: minor
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: truegrit at gmail dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18534


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]