Connecting Tech Pros Worldwide Help | Site Map

array question

  #1  
Old December 21st, 2007, 06:55 AM
bahoo
Guest
 
Posts: n/a
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
  #2  
Old December 21st, 2007, 07:25 AM
Sarath
Guest
 
Posts: n/a

re: array question


On Dec 21, 3:47*pm, bahoo <b83503...@yahoo.comwrote:
Quote:
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.
Quote:
Quote:
>Object* arr = NULL;
Suppose this placed at memory location 0x1000 the content is
initialized with 0
Quote:
Quote:
>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.
  #3  
Old December 21st, 2007, 08:35 AM
Salt_Peter
Guest
 
Posts: n/a

re: array question


On Dec 21, 1:47 am, bahoo <b83503...@yahoo.comwrote:
Quote:
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[i] << std::endl;
}
}

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

/*
array[0] 11
array[1] 22
*/
  #4  
Old December 21st, 2007, 02:45 PM
bahoo
Guest
 
Posts: n/a

re: array question


On Dec 21, 2:16 am, Sarath <CSar...@gmail.comwrote:
Quote:
On Dec 21, 3:47 pm, bahoo <b83503...@yahoo.comwrote:
>
>
>
Quote:
Hi,
>
Quote:
I have an array that I need to define inside a function, and later use
it in another function.
>
Quote:
main()
{
Object* arr = NULL;
>
Quote:
func1( arr );
func2 (arr );
>
Quote:
}
>
Quote:
where
>
Quote:
void func1(Object* arr)
{
arr = new ...
>
Quote:
}
>
Quote:
But this doesn't seem to work. Can anyone tell me what's wrong with
it?
>
Quote:
Thanks
bahoo
>
Let's interpret the pointers.
>
Quote:
Quote:
Object* arr = NULL;
>
Suppose this placed at memory location 0x1000 the content is
initialized with 0
>
Quote:
Quote:
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
  #5  
Old December 21st, 2007, 02:45 PM
bahoo
Guest
 
Posts: n/a

re: array question


On Dec 21, 3:28 am, Salt_Peter <pj_h...@yahoo.comwrote:
Quote:
On Dec 21, 1:47 am, bahoo <b83503...@yahoo.comwrote:
>
>
>
Quote:
Hi,
>
Quote:
I have an array that I need to define inside a function, and later use
it in another function.
>
Quote:
main()
{
Object* arr = NULL;
>
Quote:
func1( arr );
func2 (arr );
>
Quote:
}
>
Quote:
where
>
Quote:
void func1(Object* arr)
{
arr = new ...
>
Quote:
}
>
Quote:
But this doesn't seem to work. Can anyone tell me what's wrong with
it?
>
Quote:
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[i] << 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])".
  #6  
Old December 21st, 2007, 04:25 PM
Puppet_Sock
Guest
 
Posts: n/a

re: array question


On Dec 21, 9:41*am, bahoo <b83503...@yahoo.comwrote:
[snip]
Quote:
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
  #7  
Old December 21st, 2007, 10:35 PM
Jim Langston
Guest
 
Posts: n/a

re: array question


bahoo wrote:
Quote:
On Dec 21, 3:28 am, Salt_Peter <pj_h...@yahoo.comwrote:
Quote:
>On Dec 21, 1:47 am, bahoo <b83503...@yahoo.comwrote:
>>
>>
>>
Quote:
>>Hi,
>>
Quote:
>>I have an array that I need to define inside a function, and later
>>use it in another function.
>>
Quote:
>>main()
>>{
>>Object* arr = NULL;
>>
Quote:
>>func1( arr );
>>func2 (arr );
>>
Quote:
>>}
>>
Quote:
>>where
>>
Quote:
>>void func1(Object* arr)
>>{
>>arr = new ...
>>
Quote:
>>}
>>
Quote:
>>But this doesn't seem to work. Can anyone tell me what's wrong with
>>it?
>>
Quote:
>>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[i] << 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
tazmaster@rocketmail.com


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple array question Michael answers 24 September 10th, 2006 09:45 PM
Sort array question yeti349@yahoo.com answers 21 November 23rd, 2005 03:31 AM
array question WStoreyII answers 14 November 20th, 2005 05:25 PM
array question WStoreyII answers 14 November 20th, 2005 04:56 PM
Dynamic drop-down list with array - question jonnytansey2@hotmail.com answers 1 July 17th, 2005 01:35 PM