array question 
December 21st, 2007, 05:55 AM
| | | array question
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 | 
December 21st, 2007, 06:25 AM
| | | 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. Suppose this placed at memory location 0x1000 the content is
initialized with 0 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. | 
December 21st, 2007, 07:35 AM
| | | 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
*/ | 
December 21st, 2007, 01:45 PM
| | | 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:
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:
void func1(Object* arr)
{
arr = new ...
| >> Quote:
But this doesn't seem to work. Can anyone tell me what's wrong with
it?
| >>
Let's interpret the pointers.
>>
Suppose this placed at memory location 0x1000 the content is
initialized with 0
>>
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 | 
December 21st, 2007, 01:45 PM
| | | 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:
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:
void func1(Object* arr)
{
arr = new ...
| >> Quote:
But this doesn't seem to work. Can anyone tell me what's wrong with
it?
| >>
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])". | 
December 21st, 2007, 03:25 PM
| | | 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 | 
December 21st, 2007, 09:35 PM
| | | 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:
>>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:
>>void func1(Object* arr)
>>{
>>arr = new ...
| >>>> Quote:
>>But this doesn't seem to work. Can anyone tell me what's wrong with
>>it?
| >>>>
>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 | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,662 network members.
|