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 9 1886
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
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......
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
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 :)
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
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!
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 methods which take arrays of
primitives. If my bean method takes an array of primitves I get a
"java.lang.IllegalArgumentException: argument type mismatch" when
method.invoke(bean, args) is called ,where 'bean' is my bean to set & 'args'
is an...
|
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 argument,
and when would I pass a type?
Thanks,
|
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.
Consider,
void f(int x, int& i);
int i, j;
f(1, i);
|
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 (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);
then everything works. But if instead I code qsort as:
|
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 exceptions?
| |
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 Aquisition Device
(2) Color Aqusition Device
(3) Sound Aquistion Device
(4) Smell Aquisition Device
(5) Touch Aquisition Device
(6) Art Aquisition Device
|
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 by value temporaries are
created?(I'm not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two...
|
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 0xb7d4c2f7 in vfprintf () from /lib/tls/i686/cmov/libc.so.6
#2 0xb7d6441b in vsprintf () from /lib/tls/i686/cmov/libc.so.6
#3 0x08049ba0 in character_data::printf (this=0x800, argument=0x0)
at character.c:198
|
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 and bar is a function using A is an argument returning something
else
class A;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |