array question

B

bahoo

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
 
S

Sarath

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.
 
S

Salt_Peter

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
*/
 
B

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.

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
 
B

bahoo

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 );


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])".
 
P

Puppet_Sock

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
 
J

Jim Langston

bahoo said:
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 );


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top