Question about arrays..

Discussion in 'C++' started by kiddler, Jul 19, 2006.

  1. kiddler

    kiddler Guest

    I'm sorta new to the C++ syntax.

    I know that arrays can be delcared like so:
    int a[]={1,2,3,4};

    but what if I want the array to be a global, and then set the length
    and value inside a method?

    like in java, you can do:

    int a[];
    void main()
    {
    a=new int{1,2,3,4};
    }

    How do I do that in C++?
    kiddler, Jul 19, 2006
    #1
    1. Advertising

  2. kiddler

    Jerry Coffin Guest

    In article <>,
    says...
    > I'm sorta new to the C++ syntax.
    >
    > I know that arrays can be delcared like so:
    > int a[]={1,2,3,4};
    >
    > but what if I want the array to be a global, and then set the length
    > and value inside a method?


    You can't do it (not directly anyway). If you want to do that, you're
    probably better off using an std::vector instead.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jul 19, 2006
    #2
    1. Advertising

  3. kiddler

    kiddler Guest

    I see... I'll look that up, thanks alot
    kiddler, Jul 19, 2006
    #3
  4. kiddler

    AB Guest

    > like in java, you can do:
    >
    > int a[];
    > void main()
    > {
    > a=new int{1,2,3,4};
    > }
    >
    > How do I do that in C++?


    No direct method, but you can try this..

    int* a ;//global declaration

    void method()
    {
    a = new int[size] ;
    for(i = 0; i < size; i++)
    a = value ;
    }
    AB, Jul 19, 2006
    #4
  5. kiddler

    chriskr7 Guest

    AB wrote:
    > > like in java, you can do:
    > >
    > > int a[];
    > > void main()
    > > {
    > > a=new int{1,2,3,4};
    > > }
    > >
    > > How do I do that in C++?

    >
    > No direct method, but you can try this..
    >
    > int* a ;//global declaration
    >
    > void method()
    > {
    > a = new int[size] ;
    > for(i = 0; i < size; i++)
    > a = value ;
    > }


    another way -

    int* a;

    void method()
    {
    int b[] = {1, 2, 3, 4};
    a = new int [4];
    memcpy(a, b, 4);
    //think it's better for speed of performance
    }//
    chriskr7, Jul 19, 2006
    #5
  6. AB schrieb:
    >> like in java, you can do:
    >>
    >> int a[];
    >> void main()


    main returns an int:

    int main()

    >> {
    >> a=new int{1,2,3,4};
    >> }
    >>
    >> How do I do that in C++?

    >
    > No direct method, but you can try this..
    >
    > int* a ;//global declaration
    >
    > void method()
    > {
    > a = new int[size] ;
    > for(i = 0; i < size; i++)
    > a = value ;
    > }


    Don't do it the Java way when you program C++. Where would you delete[]
    the pointer?

    Better would be a std::vector as Jerry suggests:

    #include <vector>

    std::vector<int> a;

    int main()
    {
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    a.push_back(4);

    // or:
    a.resize(4);
    for (int i = 0; i < 4; i++)
    a = i+1;
    }

    --
    Thomas
    Thomas J. Gritzan, Jul 19, 2006
    #6
  7. Thomas J. Gritzan wrote:
    > AB schrieb:
    >>> like in java, you can do:
    >>>
    >>> int a[];
    >>> void main()

    >
    > main returns an int:
    >
    > int main()
    >
    >>> {
    >>> a=new int{1,2,3,4};
    >>> }
    >>>
    >>> How do I do that in C++?

    >>
    >> No direct method, but you can try this..
    >>
    >> int* a ;//global declaration
    >>
    >> void method()
    >> {
    >> a = new int[size] ;
    >> for(i = 0; i < size; i++)
    >> a = value ;
    >> }

    >
    > Don't do it the Java way when you program C++. Where would you
    > delete[] the pointer?
    >
    > Better would be a std::vector as Jerry suggests:
    >
    > #include <vector>
    >
    > std::vector<int> a;
    >
    > int main()
    > {
    > a.push_back(1);
    > a.push_back(2);
    > a.push_back(3);
    > a.push_back(4);
    >
    > // or:
    > a.resize(4);
    > for (int i = 0; i < 4; i++)
    > a = i+1;
    > }


    Why is 'a' *global*? Is there really *the need* for it to be global?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 19, 2006
    #7
  8. Victor Bazarov schrieb:
    > Thomas J. Gritzan wrote:
    >> Don't do it the Java way when you program C++. Where would you
    >> delete[] the pointer?
    >>
    >> Better would be a std::vector as Jerry suggests:
    >>
    >> #include <vector>
    >>
    >> std::vector<int> a;
    >>
    >> int main()
    >> {
    >> a.push_back(1);
    >> a.push_back(2);
    >> a.push_back(3);
    >> a.push_back(4);
    >>
    >> // or:
    >> a.resize(4);
    >> for (int i = 0; i < 4; i++)
    >> a = i+1;
    >> }

    >
    > Why is 'a' *global*? Is there really *the need* for it to be global?


    Because the OP wanted it:

    > but what if I want the array to be a global, and then set the length
    > and value inside a method?


    I wonder how he got the global in Java to work...

    --
    Thomas
    Thomas J. Gritzan, Jul 19, 2006
    #8
  9. kiddler posted:

    > I'm sorta new to the C++ syntax.
    >
    > I know that arrays can be delcared like so:
    > int a[]={1,2,3,4};
    >
    > but what if I want the array to be a global, and then set the length
    > and value inside a method?
    >
    > like in java, you can do:
    >
    > int a[];
    > void main()
    > {
    > a=new int{1,2,3,4};
    > }
    >
    > How do I do that in C++?



    std::vector would probably be the way to go.

    Or if you're feeling creative. . .

    template<class T>
    class Array {
    private:

    T *pNC;
    unsigned lenNC;

    public:

    T * const p;

    unsigned const &len;


    Array() : p(pNC), len(lenNC) {}

    void Allocate(unsigned const quantity)
    {
    lenNC = quantity;

    pNC = new T[len];
    }

    void Deallocate()
    {
    delete [] pNC;
    }
    };


    Array<int> a;


    int main()
    {
    a.Allocate(4);

    a.p[0] = 1; a.p[1] = 2; a.p[2] = 3; a.p[3] = 4;


    ...


    a.Deallocate();
    }



    --

    Frederick Gotham
    Frederick Gotham, Jul 19, 2006
    #9
  10. Frederick Gotham wrote:
    > kiddler posted:
    >
    >> I'm sorta new to the C++ syntax.
    >>
    >> I know that arrays can be delcared like so:
    >> int a[]={1,2,3,4};
    >>
    >> but what if I want the array to be a global, and then set the length
    >> and value inside a method?
    >>
    >> like in java, you can do:
    >>
    >> int a[];
    >> void main()
    >> {
    >> a=new int{1,2,3,4};
    >> }
    >>
    >> How do I do that in C++?

    >
    >
    > std::vector would probably be the way to go.
    >
    > Or if you're feeling creative. . .
    >
    > template<class T>
    > class Array {
    > private:
    >
    > T *pNC;
    > unsigned lenNC;
    >
    > public:
    >
    > T * const p;
    >
    > unsigned const &len;
    >
    >
    > Array() : p(pNC), len(lenNC) {}


    Neither 'pNC' nor 'lenNC' are initialised. Initialising other members
    with their values (indeterminate, BTW), is very dangerous.

    >
    > void Allocate(unsigned const quantity)
    > {
    > lenNC = quantity;
    >
    > pNC = new T[len];


    This doesn't seem to do anything with 'p'. What is 'p' for, then?

    > }
    >
    > void Deallocate()
    > {
    > delete [] pNC;
    > }
    > };
    >
    >
    > Array<int> a;
    >
    >
    > int main()
    > {
    > a.Allocate(4);
    >
    > a.p[0] = 1; a.p[1] = 2; a.p[2] = 3; a.p[3] = 4;


    Undefined behaviour. You're dereferencing an uninitialised pointer.

    >
    >
    > ...
    >
    >
    > a.Deallocate();
    > }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 19, 2006
    #10
  11. Victor Bazarov schrieb:
    > Frederick Gotham wrote:
    >> Or if you're feeling creative. . .
    >>
    >> template<class T>
    >> class Array {
    >> private:
    >>
    >> T *pNC;
    >> unsigned lenNC;
    >>
    >> public:
    >>
    >> T * const p;
    >>
    >> unsigned const &len;
    >>
    >>
    >> Array() : p(pNC), len(lenNC) {}

    >
    > Neither 'pNC' nor 'lenNC' are initialised. Initialising other members
    > with their values (indeterminate, BTW), is very dangerous.
    >
    >> void Allocate(unsigned const quantity)
    >> {
    >> lenNC = quantity;
    >>
    >> pNC = new T[len];

    >
    > This doesn't seem to do anything with 'p'. What is 'p' for, then?


    Maybe p is supposed to be a reference like len.
    Doesn't change the fact that it's bad advice, and that there is no
    destructor.

    >> }
    >>
    >> void Deallocate()
    >> {
    >> delete [] pNC;
    >> }
    >> };


    --
    Thomas
    Thomas J. Gritzan, Jul 19, 2006
    #11
  12. Thomas J. Gritzan posted:

    > Maybe p is supposed to be a reference like len.
    > Doesn't change the fact that it's bad advice, and that there is no
    > destructor.



    Yes, the code was unchecked, and I posted in haste.

    "p" was supposed to be a reference.



    --

    Frederick Gotham
    Frederick Gotham, Jul 19, 2006
    #12
    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. Alexandra Stehman
    Replies:
    5
    Views:
    30,604
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,252
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    565
  4. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    385
  5. Philipp
    Replies:
    21
    Views:
    1,126
    Philipp
    Jan 20, 2009
Loading...

Share This Page