473,226 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

argument returning

Hi, I'm converting (for learning purpose) )a program from Java to C++
and I've a doubt:

In Java every argument (variable and reference types) is passed and
returned in functions by-value so if I want to create inside that
function a matrix (i.e. int a[][]) and than passing the reference to it
I can simply
returning that value:

in main()
....
int z[][] = pass(); // here 'z' contain the reference to the array
object created in pass() by 'a'
....

the method
....
public static int[][] pass()
{
int a[][] = {{1,2},{3,4}};
return a;
}
....

instead in C++

in main()
....
int **p = pass();
....

the function
....
int ** pass()
{
static int z[] = {1,2,3, -1};
static int o[] = {4,5,6,7, -1};
static int j[] = {8,9,10,11,12, -1};

static int *p[3];

p[0] = z;
p[1] = o;
p[2] = j;

return p;
}
....

in the C++ if I don't use the static when I go out from that function
in the caller 'p' has not
the correct array values. In C++ the argument can be passed by-value
and by-reference so
when in the function I call 'return p' is passed its address but if I
don't use static that address is
not more available bacause 'p' is not more available....

is that the correct conversion ?

Thanks

Oct 4 '06 #1
9 1825
lwz
It works for me without static... :)

josh wrote:
Hi, I'm converting (for learning purpose) )a program from Java to C++
and I've a doubt:

In Java every argument (variable and reference types) is passed and
returned in functions by-value so if I want to create inside that
function a matrix (i.e. int a[][]) and than passing the reference to it
I can simply
returning that value:

in main()
...
int z[][] = pass(); // here 'z' contain the reference to the array
object created in pass() by 'a'
...

the method
...
public static int[][] pass()
{
int a[][] = {{1,2},{3,4}};
return a;
}
...

instead in C++

in main()
...
int **p = pass();
...

the function
...
int ** pass()
{
static int z[] = {1,2,3, -1};
static int o[] = {4,5,6,7, -1};
static int j[] = {8,9,10,11,12, -1};

static int *p[3];

p[0] = z;
p[1] = o;
p[2] = j;

return p;
}
...

in the C++ if I don't use the static when I go out from that function
in the caller 'p' has not
the correct array values. In C++ the argument can be passed by-value
and by-reference so
when in the function I call 'return p' is passed its address but if I
don't use static that address is
not more available bacause 'p' is not more available....

is that the correct conversion ?

Thanks
Oct 4 '06 #2

lwz ha scritto:
It works for me without static... :)
Sorry but it's not possible:

try this, eliminating static

int main()
{
int **p = pass();

for(int ix=0; ix < 3; ix++)
{
int *row = *(p+ix);

for(int iy=0; *(row+iy) != -1; iy++)
printf(" %d ", *(row+iy));
}
printf("\n");
}

if you try to compile the compiler show a warning:
warning: address of local variable 'p' returned cpp_test
and in fact the output shows trashing values......

Oct 4 '06 #3
josh wrote:
Hi, I'm converting (for learning purpose) )a program from Java to C++
and I've a doubt:

In Java every argument (variable and reference types) is passed and
returned in functions by-value so if I want to create inside that
function a matrix (i.e. int a[][]) and than passing the reference to
it I can simply
returning that value:
[...]
in the C++ if I don't use the static when I go out from that function
in the caller 'p' has not
the correct array values. In C++ the argument can be passed by-value
and by-reference so
when in the function I call 'return p' is passed its address but if I
don't use static that address is
not more available bacause 'p' is not more available....

is that the correct conversion ?
Yes, that's true. In Java the garbage collector will not release the
variable as there is a reference passed around. In C++ the variable will be
released if it goes out of scope - which means for local variables when the
function returns. You must use static then (which might cause new problems
though in multithreaded programs).

Boris
Oct 4 '06 #4

Boris ha scritto:
Yes, that's true. In Java the garbage collector will not release the
variable as there is a reference passed around.
not just... the object created on the heap is not released because
there is 'z'
that is pointing to but the variable 'a' when the function return is
not more available :)

Oct 4 '06 #5

josh wrote:
Hi, I'm converting (for learning purpose) )a program from Java to C++
and I've a doubt:

In Java every argument (variable and reference types) is passed and
returned in functions by-value
Well - sort of. Remember that in Java, you can not declare an object -
only a reference:

//JAVA code (sorry!)
Object obj = new Object;

Here obj is not an object but a reference to an object. This
corresponds to the following C++ code:
Object* obj = new Object;

so if I want to create inside that
function a matrix (i.e. int a[][]) and than passing the reference to it
I can simply
returning that value:

in main()
...
int z[][] = pass(); // here 'z' contain the reference to the array
object created in pass() by 'a'
...

the method
...
public static int[][] pass()
{
int a[][] = {{1,2},{3,4}};
return a;
}
...

instead in C++

in main()
...
int **p = pass();
Here you're wrong. There is a difference between a pointer and a
matrix, and in C/C++ you can not return an array (and thus not a
matrix).

What you should do is find out what you want to return and declare that
type, e.g.
typedef std::vector<std::vector<int matrix;

(Depending on what you do return the above might be overkill. If e.g.
you always return a 2*2 integer matrix, the above is overkill).
...

the function
...
int ** pass()
now becomes
matrix pass()
{
matrix result;

....
return result;
}
{
static int z[] = {1,2,3, -1};
static int o[] = {4,5,6,7, -1};
static int j[] = {8,9,10,11,12, -1};

static int *p[3];

p[0] = z;
p[1] = o;
p[2] = j;

return p;
}
...

in the C++ if I don't use the static when I go out from that function
in the caller 'p' has not
the correct array values. In C++ the argument can be passed by-value
and by-reference so
when in the function I call 'return p' is passed its address but if I
don't use static that address is
not more available bacause 'p' is not more available....

is that the correct conversion ?
The real problem is that what you point to has been deallocated at
return - it does not exist anymore. You "resolve" this by using static
memory, but other problems come up - e.g. that you can not use pass()
when multithreading and each call to pass destroys any previously
returned values.

/Peter

Oct 4 '06 #6

peter koch ha scritto:
Here you're wrong. There is a difference between a pointer and a
matrix,
yes there is a difference in fact a matrix is not a pointer but a
pointer to an array
i.e. if I have int a[2][2] I can pass it to a function writing:
1) foo(int a[][2]) or
2) foo(int (*a)[2])
>and in C/C++ you can not return an array (and thus not a
matrix).
no I can return an array:
int * foo()
{
static int a[3] = {1,2,3};
return a;
}
and so I can return a matrix initializating it as a triangular array ad
so like an array of pointers
and so the function:
int ** foo()
{
static int z[] = {1,2,3, -1};
static int o[] = {4,5,6,7, -1};
static int j[] = {8,9,10,11,12, -1};

static int *p[3];

p[0] = z;
p[1] = o;
p[2] = j;

return p;
}
is legal!

Oct 4 '06 #7

josh wrote:
peter koch ha scritto:
Here you're wrong. There is a difference between a pointer and a
matrix,

yes there is a difference in fact a matrix is not a pointer but a
pointer to an array
i.e. if I have int a[2][2] I can pass it to a function writing:
1) foo(int a[][2]) or
2) foo(int (*a)[2])
No you can not. You are not passing the matrix to a function - what you
are passing is a pointer to the first element. I believe it must be
your Java background that confuses you.
>
and in C/C++ you can not return an array (and thus not a
matrix).
no I can return an array:
int * foo()
This is not a array! It is a pointer to an integer - for Christs sake.
{
static int a[3] = {1,2,3};
return a;
}
and so I can return a matrix initializating it as a triangular array ad
You are not returning a matrix. What you are returning is a pointer to
the first element. If you can not understand that, I must strongly
recommend you to stop programming in C (or C++ if thats what you call
it).
so like an array of pointers
and so the function:
int ** foo()
{
static int z[] = {1,2,3, -1};
static int o[] = {4,5,6,7, -1};
static int j[] = {8,9,10,11,12, -1};

static int *p[3];

p[0] = z;
p[1] = o;
p[2] = j;

return p;
}
is legal!
It has nothing to do with legality.

/Peter

Oct 4 '06 #8

peter koch ha scritto:
>
No you can not. You are not passing the matrix to a function - what you
are passing is a pointer to the first element. I believe it must be
your Java background that confuses you.
sorry my first language is not english so for me is difficult to
explain my concepts.
I did not want to say that I can pass an array or a matrix because a
pointer is
different from that objects. I wanted only say that as in C/C++ I can't
pass an
array or a matrix with the sintax i.e. int [] or int [][] I can receive
them using
pointer notation.

Also in Java we don't pass array or matrix directly but we pass a
reference of
the array object created.

Oct 4 '06 #9

josh wrote:
peter koch ha scritto:

No you can not. You are not passing the matrix to a function - what you
are passing is a pointer to the first element. I believe it must be
your Java background that confuses you.

sorry my first language is not english so for me is difficult to
explain my concepts.
I did not want to say that I can pass an array or a matrix because a
pointer is
different from that objects. I wanted only say that as in C/C++ I can't
pass an
array or a matrix with the sintax i.e. int [] or int [][] I can receive
them using
pointer notation.
You can receive their adress by returning the pointer. But this is a
very bad idea so change it to std::vector (unless you want to spend
long hours debugging the situation).
>
Also in Java we don't pass array or matrix directly but we pass a
reference of
the array object created.
Correct, but the situation is not the same here: in Java you create the
initial object with new, so the object has dynamic allocation from the
beginning. Unless you install a garbage-collector with C++ you can not
do the same thing - your program will leak.

/Peter

Oct 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: PerfectDayToChaseTornados | last post by:
Hi All, I am trying to create a BeanHelper class which will set a beans values from the request much the same as it would be form a jsp using 'usebean'. I can get everything to work except for...
5
by: Tobiah | last post by:
What is the purpose of the second argument to super()? What is meant by the returning of an 'unbound' object when the argument is omitted. Also, when would I pass an object as the second...
4
by: Siemel Naran | last post by:
Hi. I have found one advantage of returning values through the argument list. It's that we have to store the return value. But when we return by value, we may forgot to store the return value. ...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
4
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw...
10
by: Immortalist | last post by:
Various aquisition devices that guide learning along particular pathways towards human biases. And as E.O. Wilson might say mental development appears to be genetically constrained. (1) Language...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
8
by: A. Anderson | last post by:
Howdy everyone, I'm experiencing a problem with a program that I'm developing. Take a look at this stack report from GDB - #0 0xb7d782a3 in strlen () from /lib/tls/i686/cmov/libc.so.6 #1 ...
17
by: Klaas Vantournhout | last post by:
Hi all, I was wondering if it is possible if you can check in a function if one of the arguments is temporary. What I mean is the following. A is a class, foo is a function returning a class...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.