f



initiation string not doing as I expected

Why is canvas_index not being assigned properly in the initiation list 
unless I do an assignment inside the backets 


/*
 * 
=====================================================================================
 *
 *       Filename:  test.cpp
 *
 *    Description:  Threading Experiment
 *
 *        Version:  1.0
 *        Created:  12/18/2016 12:46:51 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ruben Safir (mn), ruben@mrbrklyn.com
 *        Company:  NYLXS Inc
 *
 * 
=====================================================================================
 */

#include <iostream>
#include <thread>

namespace testing{
	std::thread t[10];
	class PIC
	{
		public:
		PIC():beg{&source[0]}, end{&source[99]} , canvas_index
{canvas}
		{
			
			canvas_index = canvas;
			for(int i = 0; i < 10;i++)
			{
			t[i] = std::thread([this]{ readin(beg); }
);		
			std::cout << i << ": Making a thread"  << 
std::endl; 
			beg += 10;
			}
		};
		
		~PIC()
		{
			std::cout << "In the destructor"  << std::endl;
			for(int i=0; i<10; i++)
			{
				t[i].join();
			std::cout << i << ": Joining a thread"  << 
std::endl; 
			}

		};
		void readin(char * start)
		{
			for( int i = 0; i<10; i++ )
			{
				*canvas_index = *start;
				std::cout << i << ": Copy " << 
reinterpret_cast<char>(*start) << std::endl;
				std::cout << i << ": Copied to 
canvas_index " << reinterpret_cast<char>(*canvas_index) << std::endl;
				canvas_index++;
				start++;
			}

		};

		char * get_canvas()
		{
			return canvas;
		}

		private:
		char * beg;
		char * end;
		char * canvas_index;
		char * canvas = new char[100];
		char source[100] = {
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'
		};
		//int index;
	};
}//end namespace

int main(int argc, char** argv)
{
	testing::PIC fido;
	for(int i = 0; i<100;i++)
	{
		std::cout << i << " Canvas Position " << fido.get_canvas()
[i] << std::endl;
	}

	return 0;
}
0
Popping
12/21/2016 11:16:44 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

5 Replies
766 Views

Similar Articles

[PageSpeed] 57

On 21.12.2016 13:16, Popping mad wrote:
> Why is canvas_index not being assigned properly in the initiation list
> unless I do an assignment inside the backets
>
> 	class PIC
> 	{
> 		public:
> 		PIC():beg{&source[0]}, end{&source[99]} , canvas_index
> {canvas}
> 		{
[...]
> 		};
[...]
> 		private:
[...]
> 		char * canvas_index;
> 		char * canvas = new char[100];
[...]

canvas_index is initialized before canvas, because it is declared before 
canvas in the class definition. Thus, it is initialized with garbage.



0
Paavo
12/21/2016 11:50:41 AM
"Paavo Helde"  schreef in bericht 
news:O5mdnY-xfJ0S8cfFnZ2dnUU78IvNnZ2d@giganews.com...
>
>On 21.12.2016 13:16, Popping mad wrote:
>> Why is canvas_index not being assigned properly in the initiation list
>> unless I do an assignment inside the backets
>>
>> class PIC
>> {
>> public:
>> PIC():beg{&source[0]}, end{&source[99]} , canvas_index
>> {canvas}
>> {
>[...]
>> };
>[...]
>> private:
>[...]
>> char * canvas_index;
>> char * canvas = new char[100];
>[...]
>
>canvas_index is initialized before canvas, because it is declared before 
>canvas in the class definition. Thus, it is initialized with garbage.

The compilers I use, issue a warning if the order of the initializations is 
different from the order of the declarations. I wonder which compiler was 
used, that did not warn. 

0
Fred
12/21/2016 12:47:28 PM
"Fred.Zwarts"  schreef in bericht news:o3dtkv$1goc$1@gioia.aioe.org...
>
>"Paavo Helde"  schreef in bericht 
>news:O5mdnY-xfJ0S8cfFnZ2dnUU78IvNnZ2d@giganews.com...
>>
>>On 21.12.2016 13:16, Popping mad wrote:
>>> Why is canvas_index not being assigned properly in the initiation list
>>> unless I do an assignment inside the backets
>>>
>>> class PIC
>>> {
>>> public:
>>> PIC():beg{&source[0]}, end{&source[99]} , canvas_index
>>> {canvas}
>>> {
>>[...]
>>> };
>>[...]
>>> private:
>>[...]
>>> char * canvas_index;
>>> char * canvas = new char[100];
>>[...]
>>
>>canvas_index is initialized before canvas, because it is declared before 
>>canvas in the class definition. Thus, it is initialized with garbage.
>
>The compilers I use, issue a warning if the order of the initializations is 
>different from the order of the declarations. I wonder which compiler was 
>used, that did not warn.

Probably, my reaction was too fast. I now see that canvas is not initialized 
in the initialization list, so that the compiler cannot detect the 
non-matching order easily. 

0
Fred
12/21/2016 12:51:23 PM
On 12/21/2016 06:50 AM, Paavo Helde wrote:
> canvas_index is initialized before canvas, because it is declared before
> canvas in the class definition. Thus, it is initialized with garbage.


thanks.
0
ruben
12/21/2016 7:24:43 PM
On Wednesday, 21 December 2016 14:51:32 UTC+2, F.Zwarts  wrote:
> "Fred.Zwarts"  schreef in bericht news:o3dtkv$1goc$1@gioia.aioe.org...
> >
> >
> >The compilers I use, issue a warning if the order of the initializations is 
> >different from the order of the declarations. I wonder which compiler was 
> >used, that did not warn.
> 
> Probably, my reaction was too fast. I now see that canvas is not initialized 
> in the initialization list, so that the compiler cannot detect the 
> non-matching order easily.

clang still warns there about uninitialized canvas used to
initialize canvas_index.
0
UTF
12/21/2016 9:40:21 PM
Reply: