array question

Discussion in 'C++' started by bahoo, Dec 21, 2007.

  1. bahoo

    bahoo Guest

    Hi,

    I have an array that I need to define inside a function, and later use
    it in another function.

    main()
    {
    Object* arr = NULL;

    func1( arr );
    func2 (arr );
    }

    where

    void func1(Object* arr)
    {
    arr = new ...
    }

    But this doesn't seem to work. Can anyone tell me what's wrong with
    it?

    Thanks
    bahoo
    bahoo, Dec 21, 2007
    #1
    1. Advertising

  2. bahoo

    Sarath Guest

    On Dec 21, 3:47 pm, bahoo <> wrote:
    > Hi,
    >
    > I have an array that I need to define inside a function, and later use
    > it in another function.
    >
    > main()
    > {
    > Object* arr = NULL;
    >
    > func1( arr );
    > func2 (arr );
    >
    > }
    >
    > where
    >
    > void func1(Object* arr)
    > {
    > arr = new ...
    >
    > }
    >
    > But this doesn't seem to work. Can anyone tell me what's wrong with
    > it?
    >
    > Thanks
    > bahoo


    Let's interpret the pointers.

    >> Object* arr = NULL;


    Suppose this placed at memory location 0x1000 the content is
    initialized with 0

    >> void func1(Object* arr)


    Object* arr will take another location say 0x2000 and it's content
    initialized with the value passed (here 0).

    and you have allocated memory and put the resultant pointer in 0x2000
    where the location 0x1000 (pointer in main function) still unchanged
    (here 0)

    To resolve this issue.. You can approach the following method

    void main()
    {
    Object* arr = NULL;
    func1( &arr );
    func2 (arr );
    }


    void func1(Object** arr)
    {
    (*arr) = new ...
    }

    HTH.
    Sarath, Dec 21, 2007
    #2
    1. Advertising

  3. bahoo

    Salt_Peter Guest

    On Dec 21, 1:47 am, bahoo <> wrote:
    > Hi,
    >
    > I have an array that I need to define inside a function, and later use
    > it in another function.
    >
    > main()
    > {
    > Object* arr = NULL;
    >
    > func1( arr );
    > func2 (arr );
    >
    > }
    >
    > where
    >
    > void func1(Object* arr)
    > {
    > arr = new ...
    >
    > }
    >
    > But this doesn't seem to work. Can anyone tell me what's wrong with
    > it?
    >
    > Thanks
    > bahoo


    pass the array by reference instead:

    #include <iostream>

    template < typename T, const size_t SIZE >
    void func( T(& array)[SIZE])
    {
    for(size_t i = 0; i < SIZE; ++i)
    {
    std::cout << "array[" << i << "] ";
    std::cout << array << std::endl;
    }
    }

    int main()
    {
    int myarray[] = { 11, 22 };
    func(myarray);
    }

    /*
    array[0] 11
    array[1] 22
    */
    Salt_Peter, Dec 21, 2007
    #3
  4. bahoo

    bahoo Guest

    On Dec 21, 2:16 am, Sarath <> wrote:
    > On Dec 21, 3:47 pm, bahoo <> wrote:
    >
    >
    >
    > > Hi,

    >
    > > I have an array that I need to define inside a function, and later use
    > > it in another function.

    >
    > > main()
    > > {
    > > Object* arr = NULL;

    >
    > > func1( arr );
    > > func2 (arr );

    >
    > > }

    >
    > > where

    >
    > > void func1(Object* arr)
    > > {
    > > arr = new ...

    >
    > > }

    >
    > > But this doesn't seem to work. Can anyone tell me what's wrong with
    > > it?

    >
    > > Thanks
    > > bahoo

    >
    > Let's interpret the pointers.
    >
    > >> Object* arr = NULL;

    >
    > Suppose this placed at memory location 0x1000 the content is
    > initialized with 0
    >
    > >> void func1(Object* arr)

    >
    > Object* arr will take another location say 0x2000 and it's content
    > initialized with the value passed (here 0).
    >
    > and you have allocated memory and put the resultant pointer in 0x2000
    > where the location 0x1000 (pointer in main function) still unchanged
    > (here 0)
    >
    > To resolve this issue.. You can approach the following method
    >
    > void main()
    > {
    > Object* arr = NULL;
    > func1( &arr );
    > func2 (arr );
    >
    > }
    >
    > void func1(Object** arr)
    > {
    > (*arr) = new ...
    >
    > }
    >
    > HTH.


    Thanks for the tips. But there is some problem.
    The object that I'm passing around is a 2D image.
    Notice that it doesn't get created until after func1 is executed.
    After func1 is executed, its size is 30x30 pixels, which is correct.
    However, once it enters func2, I noticed it's size changes to 256x256
    even though I haven't touched this object inside func2 yet.

    Could it be that this kind of passing around is not safe? How does C++
    know that its size is 30x30 if I'm only passing a pointer to func2? If
    it's not safe, what's the best way to do it?

    Thanks again!
    bahoo
    bahoo, Dec 21, 2007
    #4
  5. bahoo

    bahoo Guest

    On Dec 21, 3:28 am, Salt_Peter <> wrote:
    > On Dec 21, 1:47 am, bahoo <> wrote:
    >
    >
    >
    > > Hi,

    >
    > > I have an array that I need to define inside a function, and later use
    > > it in another function.

    >
    > > main()
    > > {
    > > Object* arr = NULL;

    >
    > > func1( arr );
    > > func2 (arr );

    >
    > > }

    >
    > > where

    >
    > > void func1(Object* arr)
    > > {
    > > arr = new ...

    >
    > > }

    >
    > > But this doesn't seem to work. Can anyone tell me what's wrong with
    > > it?

    >
    > > Thanks
    > > bahoo

    >
    > pass the array by reference instead:
    >
    > #include <iostream>
    >
    > template < typename T, const size_t SIZE >
    > void func( T(& array)[SIZE])
    > {
    > for(size_t i = 0; i < SIZE; ++i)
    > {
    > std::cout << "array[" << i << "] ";
    > std::cout << array << std::endl;
    > }
    >
    > }
    >
    > int main()
    > {
    > int myarray[] = { 11, 22 };
    > func(myarray);
    >
    > }
    >
    > /*
    > array[0] 11
    > array[1] 22
    > */


    Sorry but this doesn't directly address my original question.
    I have three functions, main + func1 + func2, and the object is NOT
    created in the main function.
    Also, I don't know the "SIZE", so I cannot make it a function argument
    as you suggested by " void func( T(& array)[SIZE])".
    bahoo, Dec 21, 2007
    #5
  6. bahoo

    Puppet_Sock Guest

    On Dec 21, 9:41 am, bahoo <> wrote:
    [snip]
    > Thanks for the tips. But there is some problem.
    > The object that I'm passing around is a 2D image.
    > Notice that it doesn't get created until after func1 is executed.
    > After func1 is executed, its size is 30x30 pixels, which is correct.
    > However, once it enters func2, I noticed it's size changes to 256x256
    > even though I haven't touched this object inside func2 yet.
    >
    > Could it be that this kind of passing around is not safe? How does C++
    > know that its size is 30x30 if I'm only passing a pointer to func2? If
    > it's not safe, what's the best way to do it?


    Allocated data changing between function calls is often
    caused by one of a few problems:
    - dangling or wild pointers
    - treating a variable as a pointer or pointer as a variable
    - using a pointer before it is initialized
    - stomping on pointers
    - object slicing
    - bad casts

    And a few others I'm not thinking of just off.

    But, since we can't see your code, it's very difficult to answer.

    Do this: Make the smallest sample code that can be compiled
    and still show the problem. While you are doing that it is
    quite likely you will see the problem for yourself and how
    to solve it. If not, post the sample code and ask for more
    help. You should be aiming for only posting 20 or 30 lines
    of code at most.
    Socks
    Puppet_Sock, Dec 21, 2007
    #6
  7. bahoo

    Jim Langston Guest

    bahoo wrote:
    > On Dec 21, 3:28 am, Salt_Peter <> wrote:
    >> On Dec 21, 1:47 am, bahoo <> wrote:
    >>
    >>
    >>
    >>> Hi,

    >>
    >>> I have an array that I need to define inside a function, and later
    >>> use it in another function.

    >>
    >>> main()
    >>> {
    >>> Object* arr = NULL;

    >>
    >>> func1( arr );
    >>> func2 (arr );

    >>
    >>> }

    >>
    >>> where

    >>
    >>> void func1(Object* arr)
    >>> {
    >>> arr = new ...

    >>
    >>> }

    >>
    >>> But this doesn't seem to work. Can anyone tell me what's wrong with
    >>> it?

    >>
    >>> Thanks
    >>> bahoo

    >>
    >> pass the array by reference instead:
    >>
    >> #include <iostream>
    >>
    >> template < typename T, const size_t SIZE >
    >> void func( T(& array)[SIZE])
    >> {
    >> for(size_t i = 0; i < SIZE; ++i)
    >> {
    >> std::cout << "array[" << i << "] ";
    >> std::cout << array << std::endl;
    >> }
    >>
    >> }
    >>
    >> int main()
    >> {
    >> int myarray[] = { 11, 22 };
    >> func(myarray);
    >>
    >> }
    >>
    >> /*
    >> array[0] 11
    >> array[1] 22
    >> */

    >
    > Sorry but this doesn't directly address my original question.
    > I have three functions, main + func1 + func2, and the object is NOT
    > created in the main function.
    > Also, I don't know the "SIZE", so I cannot make it a function argument
    > as you suggested by " void func( T(& array)[SIZE])".


    Are you storing the size of the image in the object? First off if you are
    using pointers in your class, you should make your copy constructor and
    assignment operators private so that they can't be called for right now.
    When you find you need to actually copy construct or assign your object
    you'll need to write correct copy constructor and assignment operators (rule
    of three).

    Now, you are using a pointer to create this object using new (which may or
    may not be a good thing.). As long as you make sure to work on that pointer
    itself, and not it's value, then you should be looking at the object that
    was created with new. Passing a reference to the pointer is one of the best
    ways to do this, although you may also want to look at usages of smart
    pointers.

    You say that the size seems to change from one call to another, it sounds to
    me like you may not be looking at the same instance in one of your calls
    then. At this point to determine what is going wrong, we need to see some
    code. Otherwise we can just guess that the error is on line 42 of your
    program.


    --
    Jim Langston
    Jim Langston, Dec 21, 2007
    #7
    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. Joseph Turian
    Replies:
    11
    Views:
    517
    Joseph Turian
    Jan 19, 2005
  2. Fabio
    Replies:
    0
    Views:
    310
    Fabio
    Nov 5, 2008
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    486
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    207
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    470
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page