Declaring a valarray, then initialising it later

Discussion in 'C++' started by Jim, Mar 4, 2007.

  1. Jim

    Jim Guest

    Hi,
    I want to declare that that a valarray of a certain name exist at the
    beginning of some code, but I can't instatiate it until I've read in
    some parameters later on in a for loop i.e.

    int main(int argc, char * argv[])
    {
    valarray<float> data;
    float init=0;
    for(int i=0; i<argc; i++)
    {
    //read in parameters, ax1,ax2
    valarray<float> d(init,ax1*ax2); //***
    data=d;
    }

    }

    Problem is when the code leaves the for loop data is of size 0, as
    what it's pointed to has been deleted by passing out of scope. I've
    tried all sorts of combinations data as extern, defining it outside
    the main block, replacing *** with data(init,ax1*ax2); but no success.
    If anyone's got any ideas I would be very grateful!
    Thanks
     
    Jim, Mar 4, 2007
    #1
    1. Advertising

  2. Jim wrote:
    > Hi,
    > I want to declare that that a valarray of a certain name exist at the
    > beginning of some code, but I can't instatiate it until I've read in
    > some parameters later on in a for loop i.e.
    >
    > int main(int argc, char * argv[])
    > {
    > valarray<float> data;
    > float init=0;
    > for(int i=0; i<argc; i++)
    > {
    > //read in parameters, ax1,ax2
    > valarray<float> d(init,ax1*ax2); //***
    > data=d;
    > }
    >
    > }
    >
    > Problem is when the code leaves the for loop data is of size 0, as
    > what it's pointed to has been deleted by passing out of scope. I've
    > tried all sorts of combinations data as extern, defining it outside
    > the main block, replacing *** with data(init,ax1*ax2); but no success.
    > If anyone's got any ideas I would be very grateful!
    > Thanks
    >


    Well I don't quite follow your code (the loop seems misplaced to me) but
    why can't you just do this?

    int main()
    {
    float init = 0;
    int ax1, ax2;
    for (int i = 0; i < argc; ++i)
    {
    // read in parameters
    }
    valarray<float> data(init, ax*ay);
    ...
    }

    john
     
    John Harrison, Mar 4, 2007
    #2
    1. Advertising

  3. Jim

    Jim Guest

    On Mar 4, 11:08 pm, John Harrison <> wrote:
    > Jim wrote:
    > > Hi,
    > > I want to declare that that a valarray of a certain name exist at the
    > > beginning of some code, but I can't instatiate it until I've read in
    > > some parameters later on in a for loop i.e.

    >
    > > int main(int argc, char * argv[])
    > > {
    > > valarray<float> data;
    > > float init=0;
    > > for(int i=0; i<argc; i++)
    > > {
    > > //read in parameters, ax1,ax2
    > > valarray<float> d(init,ax1*ax2); //***
    > > data=d;
    > > }

    >
    > > }

    >
    > > Problem is when the code leaves the for loop data is of size 0, as
    > > what it's pointed to has been deleted by passing out of scope. I've
    > > tried all sorts of combinations data as extern, defining it outside
    > > the main block, replacing *** with data(init,ax1*ax2); but no success.
    > > If anyone's got any ideas I would be very grateful!
    > > Thanks

    >
    > Well I don't quite follow your code (the loop seems misplaced to me) but
    > why can't you just do this?
    >
    > int main()
    > {
    > float init = 0;
    > int ax1, ax2;
    > for (int i = 0; i < argc; ++i)
    > {
    > // read in parameters
    > }
    > valarray<float> data(init, ax*ay);
    > ...
    >
    > }
    >
    > john


    This is just an example of one kind of problem I've been having with C+
    +. If you know you want a complex variable (class or an array for
    instance), you declare it at the start, but then initialise it when
    you've got the information to do it. How do you then tie these two
    together?
     
    Jim, Mar 5, 2007
    #3
  4. "Jim" ,comp.lang.c++:
    > I want to declare that that a valarray of a certain name exist at the
    > beginning of some code, but I can't instatiate it until I've read in
    > some parameters later on in a for loop i.e.


    > valarray<float> data;

    <snip>
    > valarray<float> d(init,ax1*ax2); //***
    > data=d;


    It is undefined behaviou to use in an assignment a valarray of a
    different size (here, data is a valarray of size 0, while d has size
    ax1*ax2). Try use the resize method instead:

    data.resize(ax1*ax2,init)
     
    Pierre Senellart, Mar 5, 2007
    #4
  5. "Jim" ,comp.lang.c++:
    > I want to declare that that a valarray of a certain name exist at the
    > beginning of some code, but I can't instatiate it until I've read in
    > some parameters later on in a for loop i.e.


    > valarray<float> data;

    <snip>
    > valarray<float> d(init,ax1*ax2); //***
    > data=d;


    It is undefined behaviour to use in an assignment a valarray of a
    different size (here, data is a valarray of size 0, while d has size
    ax1*ax2). Try using the resize method instead:

    data.resize(ax1*ax2,init)
     
    Pierre Senellart, Mar 5, 2007
    #5
  6. Jim wrote:
    > On Mar 4, 11:08 pm, John Harrison <> wrote:
    >
    >>Jim wrote:
    >>
    >>>Hi,
    >>>I want to declare that that a valarray of a certain name exist at the
    >>>beginning of some code, but I can't instatiate it until I've read in
    >>>some parameters later on in a for loop i.e.

    >>
    >>>int main(int argc, char * argv[])
    >>>{
    >>> valarray<float> data;
    >>> float init=0;
    >>> for(int i=0; i<argc; i++)
    >>> {
    >>> //read in parameters, ax1,ax2
    >>> valarray<float> d(init,ax1*ax2); //***
    >>> data=d;
    >>> }

    >>
    >>>}

    >>
    >>>Problem is when the code leaves the for loop data is of size 0, as
    >>>what it's pointed to has been deleted by passing out of scope. I've
    >>>tried all sorts of combinations data as extern, defining it outside
    >>>the main block, replacing *** with data(init,ax1*ax2); but no success.
    >>>If anyone's got any ideas I would be very grateful!
    >>>Thanks

    >>
    >>Well I don't quite follow your code (the loop seems misplaced to me) but
    >>why can't you just do this?
    >>
    >>int main()
    >>{
    >> float init = 0;
    >> int ax1, ax2;
    >> for (int i = 0; i < argc; ++i)
    >> {
    >> // read in parameters
    >> }
    >> valarray<float> data(init, ax*ay);
    >> ...
    >>
    >>}
    >>
    >>john

    >
    >
    > This is just an example of one kind of problem I've been having with C+
    > +. If you know you want a complex variable (class or an array for
    > instance), you declare it at the start, but then initialise it when
    > you've got the information to do it. How do you then tie these two
    > together?
    >


    Don't declare it at the start, declare it only when you have the
    necessary information to initialise it. I feel I must be missing something.

    john
     
    John Harrison, Mar 5, 2007
    #6
  7. Jim

    Jim Guest

    On Mar 5, 5:59 pm, John Harrison <> wrote:
    > Jim wrote:
    > > On Mar 4, 11:08 pm, John Harrison <> wrote:

    >
    > >>Jim wrote:

    >
    > >>>Hi,
    > >>>I want to declare that that a valarray of a certain name exist at the
    > >>>beginning of some code, but I can't instatiate it until I've read in
    > >>>some parameters later on in a for loop i.e.

    >
    > >>>int main(int argc, char * argv[])
    > >>>{
    > >>> valarray<float> data;
    > >>> float init=0;
    > >>> for(int i=0; i<argc; i++)
    > >>> {
    > >>> //read in parameters, ax1,ax2
    > >>> valarray<float> d(init,ax1*ax2); //***
    > >>> data=d;
    > >>> }

    >
    > >>>}

    >
    > >>>Problem is when the code leaves the for loop data is of size 0, as
    > >>>what it's pointed to has been deleted by passing out of scope. I've
    > >>>tried all sorts of combinations data as extern, defining it outside
    > >>>the main block, replacing *** with data(init,ax1*ax2); but no success.
    > >>>If anyone's got any ideas I would be very grateful!
    > >>>Thanks

    >
    > >>Well I don't quite follow your code (the loop seems misplaced to me) but
    > >>why can't you just do this?

    >
    > >>int main()
    > >>{
    > >> float init = 0;
    > >> int ax1, ax2;
    > >> for (int i = 0; i < argc; ++i)
    > >> {
    > >> // read in parameters
    > >> }
    > >> valarray<float> data(init, ax*ay);
    > >> ...

    >
    > >>}

    >
    > >>john

    >
    > > This is just an example of one kind of problem I've been having with C+
    > > +. If you know you want a complex variable (class or an array for
    > > instance), you declare it at the start, but then initialise it when
    > > you've got the information to do it. How do you then tie these two
    > > together?

    >
    > Don't declare it at the start, declare it only when you have the
    > necessary information to initialise it. I feel I must be missing something.
    >
    > john



    Ok, if you have a class, all of the member variables are declared in
    the class definition, and are then available to all of the methods in
    that class. I want to have an object with a valarray in it, it's got
    to be a valarray as unfortunately the code written by someone else
    supplies a valarray. Copying it to a different type would be
    inefficient. In the constructor I read in the file using a supplied
    method which when given an uninitialised valarray (i.e.
    valarray<float> d;) gets given a set of values and a confirmed length
    etc. Even if I declare a valarray in the method, or use the one in the
    class definition the size of the valarray is what it should be whilst
    in the method, but as soon as you leave the scope of that method the
    array reverts to zero, meaning it's useless to the rest of the class.
    I can't find any way of initialising a declared valarray.
     
    Jim, Mar 5, 2007
    #7
  8. Jim

    Jim Guest

    On Mar 5, 3:47 pm, Pierre Senellart <> wrote:
    > "Jim" ,comp.lang.c++:
    >
    >
    >
    > > I want to declare that that a valarray of a certain name exist at the
    > > beginning of some code, but I can't instatiate it until I've read in
    > > some parameters later on in a for loop i.e.
    > > valarray<float> data;

    > <snip>
    > > valarray<float> d(init,ax1*ax2); //***
    > > data=d;

    >
    > It is undefined behaviour to use in an assignment a valarray of a
    > different size (here, data is a valarray of size 0, while d has size
    > ax1*ax2). Try using the resize method instead:
    >
    > data.resize(ax1*ax2,init)


    I've just tried that and unfortunately it failed, data went back to
    it's original size.
     
    Jim, Mar 5, 2007
    #8
  9. Jim wrote:
    > On Mar 5, 5:59 pm, John Harrison <> wrote:
    >
    >>Jim wrote:
    >>
    >>>On Mar 4, 11:08 pm, John Harrison <> wrote:

    >>
    >>>>Jim wrote:

    >>
    >>>>>Hi,
    >>>>>I want to declare that that a valarray of a certain name exist at the
    >>>>>beginning of some code, but I can't instatiate it until I've read in
    >>>>>some parameters later on in a for loop i.e.

    >>
    >>>>>int main(int argc, char * argv[])
    >>>>>{
    >>>>> valarray<float> data;
    >>>>> float init=0;
    >>>>> for(int i=0; i<argc; i++)
    >>>>> {
    >>>>> //read in parameters, ax1,ax2
    >>>>> valarray<float> d(init,ax1*ax2); //***
    >>>>> data=d;
    >>>>> }

    >>
    >>>>>}

    >>
    >>>>>Problem is when the code leaves the for loop data is of size 0, as
    >>>>>what it's pointed to has been deleted by passing out of scope. I've
    >>>>>tried all sorts of combinations data as extern, defining it outside
    >>>>>the main block, replacing *** with data(init,ax1*ax2); but no success.
    >>>>>If anyone's got any ideas I would be very grateful!
    >>>>>Thanks

    >>
    >>>>Well I don't quite follow your code (the loop seems misplaced to me) but
    >>>>why can't you just do this?

    >>
    >>>>int main()
    >>>>{
    >>>> float init = 0;
    >>>> int ax1, ax2;
    >>>> for (int i = 0; i < argc; ++i)
    >>>> {
    >>>> // read in parameters
    >>>> }
    >>>> valarray<float> data(init, ax*ay);
    >>>> ...

    >>
    >>>>}

    >>
    >>>>john

    >>
    >>>This is just an example of one kind of problem I've been having with C+
    >>>+. If you know you want a complex variable (class or an array for
    >>>instance), you declare it at the start, but then initialise it when
    >>>you've got the information to do it. How do you then tie these two
    >>>together?

    >>
    >>Don't declare it at the start, declare it only when you have the
    >>necessary information to initialise it. I feel I must be missing something.
    >>
    >>john

    >
    >
    >
    > Ok, if you have a class, all of the member variables are declared in
    > the class definition, and are then available to all of the methods in
    > that class. I want to have an object with a valarray in it, it's got
    > to be a valarray as unfortunately the code written by someone else
    > supplies a valarray. Copying it to a different type would be
    > inefficient. In the constructor I read in the file using a supplied
    > method which when given an uninitialised valarray (i.e.
    > valarray<float> d;) gets given a set of values and a confirmed length
    > etc. Even if I declare a valarray in the method, or use the one in the
    > class definition the size of the valarray is what it should be whilst
    > in the method, but as soon as you leave the scope of that method the
    > array reverts to zero, meaning it's useless to the rest of the class.
    > I can't find any way of initialising a declared valarray.
    >


    Well I feel you must be making a mistake somewhere in your code. This
    shouldn't difficult. Why not post the actual code?

    john
     
    John Harrison, Mar 5, 2007
    #9
  10. >>
    >>
    >> Ok, if you have a class, all of the member variables are declared in
    >> the class definition, and are then available to all of the methods in
    >> that class. I want to have an object with a valarray in it, it's got
    >> to be a valarray as unfortunately the code written by someone else
    >> supplies a valarray. Copying it to a different type would be
    >> inefficient. In the constructor I read in the file using a supplied
    >> method which when given an uninitialised valarray (i.e.
    >> valarray<float> d;) gets given a set of values and a confirmed length
    >> etc. Even if I declare a valarray in the method, or use the one in the
    >> class definition the size of the valarray is what it should be whilst
    >> in the method, but as soon as you leave the scope of that method the
    >> array reverts to zero, meaning it's useless to the rest of the class.
    >> I can't find any way of initialising a declared valarray.
    >>

    >
    > Well I feel you must be making a mistake somewhere in your code. This
    > shouldn't difficult. Why not post the actual code?
    >
    > john


    For instance this program print 0, 100, 100. The size of data has been
    sucessfully changed.

    #include <iostream>
    #include <valarray>

    class A
    {
    public:
    A();
    std::valarray<float> data;
    };

    A::A()
    {
    std::cout << data.size() << '\n';
    std::valarray<float> d(1.0, 100);
    data.resize(100);
    data = d;
    std::cout << data.size() << '\n';
    }

    int main()
    {
    A a;
    std::cout << a.data.size() << '\n';
    }
     
    John Harrison, Mar 5, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim West
    Replies:
    2
    Views:
    521
    Jim West
    Dec 23, 2003
  2. Markus Pitha
    Replies:
    5
    Views:
    290
    Dave Rahardja
    Jun 23, 2007
  3. Replies:
    0
    Views:
    337
  4. Jake Barnes
    Replies:
    1
    Views:
    91
  5. user
    Replies:
    41
    Views:
    377
    Dr J R Stockton
    Dec 2, 2006
Loading...

Share This Page