Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

initiation string not doing as I expected

21 views
Skip to first unread message

Popping mad

unread,
Dec 21, 2016, 6:17:02 AM12/21/16
to
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), ru...@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;
}

Paavo Helde

unread,
Dec 21, 2016, 6:50:50 AM12/21/16
to
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.



Fred.Zwarts

unread,
Dec 21, 2016, 7:47:39 AM12/21/16
to
"Paavo Helde" schreef in bericht
news:O5mdnY-xfJ0S8cfF...@giganews.com...
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.

Fred.Zwarts

unread,
Dec 21, 2016, 7:51:32 AM12/21/16
to
"Fred.Zwarts" schreef in bericht news:o3dtkv$1goc$1...@gioia.aioe.org...
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.

ruben safir

unread,
Dec 21, 2016, 2:24:51 PM12/21/16
to
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.

Öö Tiib

unread,
Dec 21, 2016, 4:40:30 PM12/21/16
to
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 new messages