Compiler error with a push_back()

well, i was programing a neural net for some ants in c++, and then when i compiled, the program got stuck in this part of the xmemory code:

1
2
3
4
void construct(pointer _Ptr, _Ty&& _Val)
		{	// construct object at _Ptr with value _Val
		::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_Ty>(_Val));
		}


the "::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_Ty>(_Val));" is where i get the mark.
with a log function, i found out that the problem lies in this part of the code:
1
2
3
4
for( int i = 0; i < 2; i++)
		{
			ANeurons[2].dinput.push_back(dRand(0,50,4));
		}


ANeurons being a vector that contains objects of the class CNeuron, dinputs being a vector of doubles and dRand a fuction that generates random doubles.

i know that there are not syntax error, and that the dRand function works perfectly; however, i can't understand the compiler code, so i have no idea what the problem is, just where it is generated.

Can anyone help me? Thanks beforehand.
It might help if you posted the compiler error verbatum.
this is the whole code in xmemory:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// xmemory internal header (from <memory>)
#pragma once
#ifndef _XMEMORY_
#define _XMEMORY_
#ifndef RC_INVOKED
#include <cstdlib>
#include <new>
#include <xutility>

 #pragma pack(push,_CRT_PACKING)
 #pragma warning(push,3)

 #define _ALLOCATOR	allocator

 #pragma push_macro("new")
 #undef new

 #pragma warning(disable: 4100)

#ifndef _FARQ	/* specify standard memory model */
 #define _FARQ
 #define _PDFT	ptrdiff_t
 #define _SIZT	size_t
#endif /* _FARQ */

_STD_BEGIN
		// TEMPLATE FUNCTION _Allocate
template<class _Ty> inline
	_Ty _FARQ *_Allocate(_SIZT _Count, _Ty _FARQ *)
	{	// allocate storage for _Count elements of type _Ty
	void *_Ptr = 0;

	if (_Count <= 0)
		_Count = 0;
	else if (((_SIZT)(-1) / sizeof (_Ty) < _Count)
		|| (_Ptr = ::operator new(_Count * sizeof (_Ty))) == 0)
		_THROW_NCEE(bad_alloc, 0);

	return ((_Ty _FARQ *)_Ptr);
	}

		// TEMPLATE FUNCTION _Construct
template<class _Ty1,
	class _Ty2> inline
	void _Construct(_Ty1 _FARQ *_Ptr, _Ty2&& _Val)
	{	// construct object at _Ptr with value _Val
	void _FARQ *_Vptr = _Ptr;
	::new (_Vptr) _Ty1(_STD forward<_Ty2>(_Val));
	}

template<class _Ty1> inline
	void _Construct(_Ty1 _FARQ *_Ptr)
	{	// construct object at _Ptr with default value
	void _FARQ *_Vptr = _Ptr;

	::new (_Vptr) _Ty1();
	}

		// TEMPLATE FUNCTION _Destroy
template<class _Ty> inline
	void _Destroy(_Ty _FARQ *_Ptr)
	{	// destroy object at _Ptr
	_Ptr->~_Ty();
	}

template<> inline
	void _Destroy(char _FARQ *)
	{	// destroy a char (do nothing)
	}

template<> inline
	void _Destroy(wchar_t _FARQ *)
	{	// destroy a wchar_t (do nothing)
	}

 #ifdef _NATIVE_WCHAR_T_DEFINED
template<> inline
	void _Destroy(unsigned short _FARQ *)
	{	// destroy a unsigned short (do nothing)
	}
 #endif /* _NATIVE_WCHAR_T_DEFINED */

		// TEMPLATE FUNCTION _Destroy_range
template<class _Alloc> inline
	void _Destroy_range(typename _Alloc::pointer _First,
		typename _Alloc::pointer _Last, _Alloc& _Al)
	{	// destroy [_First, _Last)
	_Destroy_range(_First, _Last, _Al, _Ptr_cat(_First, _Last));
	}

template<class _Alloc> inline
	void _Destroy_range(typename _Alloc::pointer _First,
		typename _Alloc::pointer _Last, _Alloc& _Al,
		_Nonscalar_ptr_iterator_tag)
	{	// destroy [_First, _Last), arbitrary type
	for (; _First != _Last; ++_First)
		_Dest_val(_Al, _First);
	}

template<class _Alloc> inline
	void _Destroy_range(typename _Alloc::pointer _First,
		typename _Alloc::pointer _Last, _Alloc& _Al,
		_Scalar_ptr_iterator_tag)
	{	// destroy [_First, _Last), scalar type (do nothing)
	}

		// TEMPLATE FUNCTION addressof
template<class _Ty> inline
	_Ty * addressof(_Ty& _Val)
	{	// return address of _Val
	return ((_Ty *) &(char&)_Val);
	}

		// TEMPLATE CLASS _Allocator_base
template<class _Ty>
	struct _Allocator_base
	{	// base class for generic allocators
	typedef _Ty value_type;
	};

		// TEMPLATE CLASS _Allocator_base<const _Ty>
template<class _Ty>
	struct _Allocator_base<const _Ty>
	{	// base class for generic allocators for const _Ty
	typedef _Ty value_type;
	};

		// TEMPLATE CLASS _ALLOCATOR
template<class _Ty>
	class _ALLOCATOR
		: public _Allocator_base<_Ty>
	{	// generic allocator for objects of class _Ty
public:
	typedef _Allocator_base<_Ty> _Mybase;
	typedef typename _Mybase::value_type value_type;

	typedef value_type _FARQ *pointer;
	typedef value_type _FARQ& reference;
	typedef const value_type _FARQ *const_pointer;
	typedef const value_type _FARQ& const_reference;

	typedef _SIZT size_type;
	typedef _PDFT difference_type;

	template<class _Other>
		struct rebind
		{	// convert this type to _ALLOCATOR<_Other>
		typedef _ALLOCATOR<_Other> other;
		};

	pointer address(reference _Val) const
		{	// return address of mutable _Val
		return ((pointer) &(char&)_Val);
		}

	const_pointer address(const_reference _Val) const
		{	// return address of nonmutable _Val
		return ((const_pointer) &(char&)_Val);
		}

	_ALLOCATOR() _THROW0()
		{	// construct default allocator (do nothing)
		}

	_ALLOCATOR(const _ALLOCATOR<_Ty>&) _THROW0()
		{	// construct by copying (do nothing)
		}

	template<class _Other>
		_ALLOCATOR(const _ALLOCATOR<_Other>&) _THROW0()
		{	// construct from a related allocator (do nothing)
		}

	template<class _Other>
		_ALLOCATOR<_Ty>& operator=(const _ALLOCATOR<_Other>&)
		{	// assign from a related allocator (do nothing)
		return (*this);
		}

	void deallocate(pointer _Ptr, size_type)
		{	// deallocate object at _Ptr, ignore size
		::operator delete(_Ptr);
		}

	pointer allocate(size_type _Count)
		{	// allocate array of _Count elements
		return (_Allocate(_Count, (pointer)0));
		}

	pointer allocate(size_type _Count, const void _FARQ *)
		{	// allocate array of _Count elements, ignore hint
		return (allocate(_Count));
		}

	void construct(pointer _Ptr, const _Ty& _Val)
		{	// construct object at _Ptr with value _Val
		_Construct(_Ptr, _Val);
		}

	void construct(pointer _Ptr, _Ty&& _Val)
		{	// construct object at _Ptr with value _Val
		::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_Ty>(_Val));
		}

	template<class _Other>
		void construct(pointer _Ptr, _Other&& _Val)
		{	// construct object at _Ptr with value _Val
		::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_Other>(_Val));
		}

	void destroy(pointer _Ptr)
		{	// destroy object at _Ptr
		_Destroy(_Ptr);
		}

	_SIZT max_size() const _THROW0()
		{	// estimate maximum array size
		_SIZT _Count = (_SIZT)(-1) / sizeof (_Ty);
		return (0 < _Count ? _Count : 1);
		}
	};

		// CLASS _ALLOCATOR<void>
template<> class _ALLOCATOR<void>
	{	// generic _ALLOCATOR for type void
public:
	typedef void _Ty;
	typedef _Ty _FARQ *pointer;
	typedef const _Ty _FARQ *const_pointer;
	typedef _Ty value_type;

	template<class _Other>
		struct rebind
		{	// convert this type to an _ALLOCATOR<_Other>
		typedef _ALLOCATOR<_Other> other;
		};

	_ALLOCATOR() _THROW0()
		{	// construct default allocator (do nothing)
		}

	_ALLOCATOR(const _ALLOCATOR<_Ty>&) _THROW0()
		{	// construct by copying (do nothing)
		}

	template<class _Other>
		_ALLOCATOR(const _ALLOCATOR<_Other>&) _THROW0()
		{	// construct from related allocator (do nothing)
		}

	template<class _Other>
		_ALLOCATOR<_Ty>& operator=(const _ALLOCATOR<_Other>&)
		{	// assign from a related allocator (do nothing)
		return (*this);
		}
	};

template<class _Ty,
	class _Other> inline
	bool operator==(const allocator<_Ty>&,
		const allocator<_Other>&) _THROW0()
	{	// test for allocator equality
	return (true);
	}

template<class _Ty,
	class _Other> inline
	bool operator!=(const allocator<_Ty>& _Left,
		const allocator<_Other>& _Right) _THROW0()
	{	// test for allocator inequality
	return (!(_Left == _Right));
	}

		// TEMPLATE FUNCTIONS _Cons_val AND _Dest_val
template<class _Alloc,
	class _Ty1,
	class _Ty2>
	void _Cons_val(_Alloc& _Alval, _Ty1 *_Pdest, _Ty2&& _Src)
	{	// construct using allocator
	_Alval.construct(_Pdest, _STD forward<_Ty2>(_Src));
	}

template<class _Alloc,
	class _Ty1>
	void _Dest_val(_Alloc& _Alval, _Ty1 *_Pdest)
	{	// destroy using allocator
	_Alval.destroy(_Pdest);
	}
_STD_END

 #pragma pop_macro("new")

 #pragma warning(pop)
 #pragma pack(pop)

#endif /* RC_INVOKED */
#endif /* _XMEMORY_ */ 

I was hoping to see the compiler error that you received. There's no point in the parts you think are relevant, we can't assist you otherwise.
sorry for my ignorance i assume.
what i actually refereed as mi compiler error was to the stopping of the program at that specific part of the xmemory code that i mentioned in mi first post. as i don't really understand the nature of my problem, the only real thing i can do is to give as much information of my problem as posible, sorry if you thought of that as misguiding, however,I don't really know what kind of information is actually of use and believe that more information can only be helpful.

but getting to the actual thing, this is the error message that i get:

"Unhandled exception at 0x00b5bcfb in Thinking_Dots.exe: 0xC0000005: Access violation writing location 0x18002e11."

and the "Autos" box says that "_Ptr" with value 0xbaadf00d cannot be evaluated (CXX0030: Error: expression cannot be evaluated)

Lastly, the debug outputs this:
"First-chance exception at 0x002fb56a in Thinking_Dots.exe: 0xC0000005: Access violation writing location 0xbaadf00d.
Unhandled exception at 0x002fb56a in Thinking_Dots.exe: 0xC0000005: Access violation writing location 0xbaadf00d."

I see. It sounds like the program complies/links, but you get the error at runtime.

Can you make one change for me, change this:
 
ANeurons[2].dinput.push_back(dRand(0,50,4));

to this:
 
ANeurons.at(2).dinput.push_back(dRand(0,50,4));


Build then run the program and let me know what happens.
i changed it at got the next results:

first, i get this message; "Unhandled exception at 0x7720b9bc in Thinking_Dots.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0029f460.."

then, i get this pile of code called malloc.c:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/***
*malloc.c - Get a block of memory from the heap
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       Defines the malloc() function.
*
*******************************************************************************/

#include <cruntime.h>
#include <malloc.h>
#include <internal.h>
#include <mtdll.h>
#include <dbgint.h>
#include <rterr.h>

#include <windows.h>
#include <winheap.h>
#include <rtcsup.h>

extern int _newmode;    /* malloc new() handler mode */

#ifdef _DEBUG
#define _heap_alloc _heap_alloc_base
#endif  /* _DEBUG */

/***
*void *_heap_alloc_base(size_t size) - does actual allocation
*
*Purpose:
*       Same as malloc() except the new handler is not called.
*
*Entry:
*       See malloc
*
*Exit:
*       See malloc
*
*Exceptions:
*
*******************************************************************************/

__forceinline void * __cdecl _heap_alloc (size_t size)

{

    if (_crtheap == 0) {
        _FF_MSGBANNER();    /* write run-time error banner */
        _NMSG_WRITE(_RT_CRT_NOTINIT);  /* write message */
        __crtExitProcess(255);  /* normally _exit(255) */
    }

    return HeapAlloc(_crtheap, 0, size ? size : 1);
}


/***
*void *malloc(size_t size) - Get a block of memory from the heap
*
*Purpose:
*       Allocate of block of memory of at least size bytes from the heap and
*       return a pointer to it.
*
*       Calls the new appropriate new handler (if installed).
*
*Entry:
*       size_t size - size of block requested
*
*Exit:
*       Success:  Pointer to memory block
*       Failure:  NULL (or some error value)
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/

void * __cdecl _malloc_base (size_t size)
{
    void *res = NULL;

    //  validate size
    if (size <= _HEAP_MAXREQ) {
        for (;;) {

            //  allocate memory block
            res = _heap_alloc(size);////<--------Heres Is where i get the mark---///

            //  if successful allocation, return pointer to memory
            //  if new handling turned off altogether, return NULL

            if (res != NULL)
            {
                break;
            }
            if (_newmode == 0)
            {
                errno = ENOMEM;
                break;
            }

            //  call installed new handler
            if (!_callnewh(size))
                break;

            //  new handler was successful -- try to allocate again
        }
    } else {
        _callnewh(size);
        errno = ENOMEM;
        return NULL;
    }

    RTCCALLBACK(_RTC_Allocate_hook, (res, size, 0));
    if (res == NULL)
    {
        errno = ENOMEM;
    }
    return res;
}


and the debug outputs this: "First-chance exception at 0x7720b9bc in Thinking_Dots.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0029f460..
Unhandled exception at 0x7720b9bc in Thinking_Dots.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0029f460.."

And That is what i get.
closed account (DSLq5Di1)
As kbw had guessed, you are accessing an element out of range with ANeurons[2].

1
2
3
4
for (vector<CNeurons>::size_type i = 0; i < ANeurons.size(); ++i)
{
    ANeurons[i].dinput.push_back(dRand(0,50,4));
}

However, you should prefer iterators [1],

1
2
3
4
for (vector<CNeurons>::iterator it = ANeurons.begin(); it != ANeurons.end(); ++it)
{
    it->dinput.push_back(dRand(0,50,4));
}

[1] http://stackoverflow.com/questions/131241/why-use-iterators-instead-of-array-indices
You don't need to keep posting from the compiler implementation. Clearly the fault is in your code.

ANeurons doesn't have an index 2. Clearly, its size is 0 or 1 and you're accessing index 2.

It might be useful if you posted your code in the context of the error, the whole function perhaps.
you are right, i just found the error... it was simply a wrong defined if (i had writen if(i = 2) and not if(i==2))

i'm really sorry for having been wasting your time. but im'm really grateful for the help anyway.

ans sorry again.
A good argument for the use of Yoda syntax!

if(2==i)...
Last edited on
Yoda syntax; that's funny!
closed account (D80DSL3A)
Good point though. if(2 = i) won't compile since 2 is not an lvalue.
Topic archived. No new replies allowed.