Sun Nov 24 15:28:11 2024
EVENTS
 FREE
SOFTWARE
INSTITUTE

POLITICS
JOBS
MEMBERS'
CORNER

MAILING
LIST

NYLXS Mailing Lists and Archives
NYLXS Members have a lot to say and share but we don't keep many secrets. Join the Hangout Mailing List and say your peice.

DATE 2010-05-01

HANGOUT

2024-11-24 | 2024-10-24 | 2024-09-24 | 2024-08-24 | 2024-07-24 | 2024-06-24 | 2024-05-24 | 2024-04-24 | 2024-03-24 | 2024-02-24 | 2024-01-24 | 2023-12-24 | 2023-11-24 | 2023-10-24 | 2023-09-24 | 2023-08-24 | 2023-07-24 | 2023-06-24 | 2023-05-24 | 2023-04-24 | 2023-03-24 | 2023-02-24 | 2023-01-24 | 2022-12-24 | 2022-11-24 | 2022-10-24 | 2022-09-24 | 2022-08-24 | 2022-07-24 | 2022-06-24 | 2022-05-24 | 2022-04-24 | 2022-03-24 | 2022-02-24 | 2022-01-24 | 2021-12-24 | 2021-11-24 | 2021-10-24 | 2021-09-24 | 2021-08-24 | 2021-07-24 | 2021-06-24 | 2021-05-24 | 2021-04-24 | 2021-03-24 | 2021-02-24 | 2021-01-24 | 2020-12-24 | 2020-11-24 | 2020-10-24 | 2020-09-24 | 2020-08-24 | 2020-07-24 | 2020-06-24 | 2020-05-24 | 2020-04-24 | 2020-03-24 | 2020-02-24 | 2020-01-24 | 2019-12-24 | 2019-11-24 | 2019-10-24 | 2019-09-24 | 2019-08-24 | 2019-07-24 | 2019-06-24 | 2019-05-24 | 2019-04-24 | 2019-03-24 | 2019-02-24 | 2019-01-24 | 2018-12-24 | 2018-11-24 | 2018-10-24 | 2018-09-24 | 2018-08-24 | 2018-07-24 | 2018-06-24 | 2018-05-24 | 2018-04-24 | 2018-03-24 | 2018-02-24 | 2018-01-24 | 2017-12-24 | 2017-11-24 | 2017-10-24 | 2017-09-24 | 2017-08-24 | 2017-07-24 | 2017-06-24 | 2017-05-24 | 2017-04-24 | 2017-03-24 | 2017-02-24 | 2017-01-24 | 2016-12-24 | 2016-11-24 | 2016-10-24 | 2016-09-24 | 2016-08-24 | 2016-07-24 | 2016-06-24 | 2016-05-24 | 2016-04-24 | 2016-03-24 | 2016-02-24 | 2016-01-24 | 2015-12-24 | 2015-11-24 | 2015-10-24 | 2015-09-24 | 2015-08-24 | 2015-07-24 | 2015-06-24 | 2015-05-24 | 2015-04-24 | 2015-03-24 | 2015-02-24 | 2015-01-24 | 2014-12-24 | 2014-11-24 | 2014-10-24 | 2014-09-24 | 2014-08-24 | 2014-07-24 | 2014-06-24 | 2014-05-24 | 2014-04-24 | 2014-03-24 | 2014-02-24 | 2014-01-24 | 2013-12-24 | 2013-11-24 | 2013-10-24 | 2013-09-24 | 2013-08-24 | 2013-07-24 | 2013-06-24 | 2013-05-24 | 2013-04-24 | 2013-03-24 | 2013-02-24 | 2013-01-24 | 2012-12-24 | 2012-11-24 | 2012-10-24 | 2012-09-24 | 2012-08-24 | 2012-07-24 | 2012-06-24 | 2012-05-24 | 2012-04-24 | 2012-03-24 | 2012-02-24 | 2012-01-24 | 2011-12-24 | 2011-11-24 | 2011-10-24 | 2011-09-24 | 2011-08-24 | 2011-07-24 | 2011-06-24 | 2011-05-24 | 2011-04-24 | 2011-03-24 | 2011-02-24 | 2011-01-24 | 2010-12-24 | 2010-11-24 | 2010-10-24 | 2010-09-24 | 2010-08-24 | 2010-07-24 | 2010-06-24 | 2010-05-24 | 2010-04-24 | 2010-03-24 | 2010-02-24 | 2010-01-24 | 2009-12-24 | 2009-11-24 | 2009-10-24 | 2009-09-24 | 2009-08-24 | 2009-07-24 | 2009-06-24 | 2009-05-24 | 2009-04-24 | 2009-03-24 | 2009-02-24 | 2009-01-24 | 2008-12-24 | 2008-11-24 | 2008-10-24 | 2008-09-24 | 2008-08-24 | 2008-07-24 | 2008-06-24 | 2008-05-24 | 2008-04-24 | 2008-03-24 | 2008-02-24 | 2008-01-24 | 2007-12-24 | 2007-11-24 | 2007-10-24 | 2007-09-24 | 2007-08-24 | 2007-07-24 | 2007-06-24 | 2007-05-24 | 2007-04-24 | 2007-03-24 | 2007-02-24 | 2007-01-24 | 2006-12-24 | 2006-11-24 | 2006-10-24 | 2006-09-24 | 2006-08-24 | 2006-07-24 | 2006-06-24 | 2006-05-24 | 2006-04-24 | 2006-03-24 | 2006-02-24 | 2006-01-24 | 2005-12-24 | 2005-11-24 | 2005-10-24 | 2005-09-24 | 2005-08-24 | 2005-07-24 | 2005-06-24 | 2005-05-24 | 2005-04-24 | 2005-03-24 | 2005-02-24 | 2005-01-24 | 2004-12-24 | 2004-11-24 | 2004-10-24 | 2004-09-24 | 2004-08-24 | 2004-07-24 | 2004-06-24 | 2004-05-24 | 2004-04-24 | 2004-03-24 | 2004-02-24 | 2004-01-24 | 2003-12-24 | 2003-11-24 | 2003-10-24 | 2003-09-24 | 2003-08-24 | 2003-07-24 | 2003-06-24 | 2003-05-24 | 2003-04-24 | 2003-03-24 | 2003-02-24 | 2003-01-24 | 2002-12-24 | 2002-11-24 | 2002-10-24 | 2002-09-24 | 2002-08-24 | 2002-07-24 | 2002-06-24 | 2002-05-24 | 2002-04-24 | 2002-03-24 | 2002-02-24 | 2002-01-24 | 2001-12-24 | 2001-11-24 | 2001-10-24 | 2001-09-24 | 2001-08-24 | 2001-07-24 | 2001-06-24 | 2001-05-24 | 2001-04-24 | 2001-03-24 | 2001-02-24 | 2001-01-24 | 2000-12-24 | 2000-11-24 | 2000-10-24 | 2000-09-24 | 2000-08-24 | 2000-07-24 | 2000-06-24 | 2000-05-24 | 2000-04-24 | 2000-03-24 | 2000-02-24 | 2000-01-24 | 1999-12-24

Key: Value:

Key: Value:

MESSAGE
DATE 2010-05-05
FROM Ruben Safir
SUBJECT Re: [NYLXS - HANGOUT] C++ Workshop - Syntax Basics -- functions

Function Arguments:

We have already noted that the general rule for the input of functions
is the argument, or parameter list within the call operator.
furthermore, the default rule is that data in variables is copied into
the function body, creating a new set of data that is local to the scope
of the function. And we also stated that there is important exceptions
to this rule.

If you want to affect data that exists outside of your function, there
are several important methods to do so. The most basic method is the
use of pointers. When a pointer is defined in a scope global to the
your function, and when there is a value attached, memory is allocated
outside your function for the data and that can be manipulated within
the scope of your function. Data that is created within your function,
even if assigned to a pointer, still remains within your function only,
and disappears when the return value is only the pointer and when your
function instance ends. Let us look at this behavior.


_______________________________________________________________________

#include
using namespace std;

int global_a, * ptr_a;
int scope_data(int);
int * scope_pointer(int *);

int scope_data(int in){
in = 1000;
int out = 25;
return out;
}


int * scope_pointer(int *in){
*in = 1000;
int * out;
int buf = 25;
out = &buf;
cout << "The value of 'out' within function scope_pointer ==> "
<< *out << endl;
return out;
}

int main(int argc, char * argv[]){

int func_return, *func_ptr_return;
global_a = 5;
ptr_a = &global_a;

func_return = scope_data(global_a);
cout << "global_a ==>" << global_a << endl;
cout << "ptr_a value==>" << *ptr_a << endl;
cout << "function return ==>" << func_return << endl;

func_ptr_return = scope_pointer(ptr_a);
cout << "\n\nPointer Behavior\n";
cout << "ptr_a value: Since ptr_a points to global_a, the memory
\
\nsegment is retained with the new value ==> " << *ptr_a
<< endl;
cout << "\n\nFunction ptr return value:meaningless garbage \
\nsince buf is out of scope ==> " << *func_ptr_return << endl
<



return 0;
}


_____________________________________________________________________________

ruben-at-www2:~/cplus/functions> g++ -Wall function_scope.cpp -o scope.bin
ruben-at-www2:~/cplus/functions> ./scope.bin
global_a ==>5
ptr_a value==>5
function return ==>25
The value of 'out' within function scope_pointer ==> 25


Pointer Behavior
ptr_a value: Since ptr_a points to global_a, the memory
segment is retained with the new value ==> 1000


Function ptr return value:meaningless garbage
since buf is out of scope ==> 108






Function arguments and arrays:

The relationship of implicit conversion of arrays to pointers takes an
interesting and important twist when arrays are used as arguments within
functions. Arrays are an exception to the rule of the pass by value
rule for functions. Arrays, as they are in other situations, are passed
by reference with the array symbol being implicitly converted as a
pointer to the first element of the array. This is a source of a lot of
confusion, and needs to be studied thoroughly.

Let us start out first with the simplest examples, the passing of a
common integer array.


func_array.cpp

#include
#include "func_array.h"

using namespace std;

int test_array[10] = {0,1,2,3,4,5,6,7,8,9};

void single_array(int my_array[], int size)
{
//reverse array contents
for(int i = 0; i< 10; i++, size--){
my_array[size - 1] = i;
}

}


int main(int argc, char * argv[]){

single_array(test_array, 10);

for(int i = 0; i<10; i++)
cout << test_array[i] <
return 0;
}


func_array.h

#ifndef FUNC_ARRAY_H
#define FUNC_ARRAY_H
#endif /* FUNC_ARRAY_H */

void single_array(int[]);

ruben-at-www2:~/cplus/functions/func_array> ./func_array.bin
9
8
7
6
5
4
3
2
1
0


What you notice with this use of an array as an argument to the function
single_array, that there is no return of the array back to main. This
is because the array is passed by reference, and it doesn't need to be
returned. Changes to the array change the array outside of the scope of
the function. In fact, the array is passed as a pointer to the first
integer in int test_array[10], and therefor the second argument, the
array size, to know the size of the array. Without this information
being sent into your function, the function has no way of knowing the
size of the array and you can walk right past the end of the array.

Likewise, you must be certain to assign memory to your array, in this
case by initializing the array, otherwise when you pass an empty array
to your function, the point to the first element, an element which
doesn't yet exist, is a random garbage location.

For example if we rewrite our program as such:

#include
#include "func_array.h"

using namespace std;

int test_array[];

void single_array(int my_array[], int size)
{
//reverse array contents
for(int i = 0; i< 10; i++, size--){
my_array[size - 1] = i;
}

}


int main(int argc, char * argv[]){

single_array(test_array, 10);

for(int i = 0; i<10; i++)
cout << test_array[i] <
return 0;
}



and try to compile we get a compiler error:

ruben-at-www2:~/cplus/functions/func_array> g++ func_array.cpp -o
func_array.bin
func_array.cpp:6: error: storage size of ‘test_array’ isn't
known
ruben-at-www2:~/cplus/functions/func_array>

  1. 2010-05-03 Paul Robert Marino <prmarino1-at-gmail.com> Subject: [NYLXS - HANGOUT] do we have an NNTO server?
  2. 2010-05-04 Ron Guerin <ron-at-vnetworx.net> Subject: [NYLXS - HANGOUT] NYLUG Presents: 5/19 -at- 6:30PM Wietse Venema on Postfix: past, present and future
  3. 2010-05-04 Ruben Safir <mrbrklyn-at-panix.com> Subject: [NYLXS - HANGOUT] [ruben-at-mrbrklyn.com: [raj-at-brainlink.com: Re: [nylug-talk]
  4. 2010-05-04 Ruben Safir <mrbrklyn-at-panix.com> Re: [NYLXS - HANGOUT] do we have an NNTO server?
  5. 2010-05-04 From: "Paul Robert Marino" <prmarino1-at-gmail.com> Re: [NYLXS - HANGOUT] do we have an NNTO server?
  6. 2010-05-04 Kevin Mark <kevin.mark-at-verizon.net> Re: [NYLXS - HANGOUT] [ruben-at-mrbrklyn.com: [raj-at-brainlink.com: Re:
  7. 2010-05-04 From: "Paul Robert Marino" <prmarino1-at-gmail.com> Re: [NYLXS - HANGOUT] [ruben-at-mrbrklyn.com: [raj-at-brainlink.com: Re: [nylug-talk]pivot_Root problem]]
  8. 2010-05-04 Ruben Safir <mrbrklyn-at-panix.com> Re: [NYLXS - HANGOUT] do we have an NNTO server?
  9. 2010-05-04 Ruben Safir <mrbrklyn-at-panix.com> Re: [NYLXS - HANGOUT] [ruben-at-mrbrklyn.com: [raj-at-brainlink.com: Re:
  10. 2010-05-05 Ruben Safir <mrbrklyn-at-panix.com> Re: [NYLXS - HANGOUT] C++ Workshop - Syntax Basics -- functions
  11. 2010-05-06 Ron Guerin <ron-at-vnetworx.net> Subject: [NYLXS - HANGOUT] NYLUG Presents: 5/19 -at- 6:30PM Wietse Venema on Postfix: past, present and future
  12. 2010-05-08 Ruben Safir <mrbrklyn-at-panix.com> Re: [NYLXS - HANGOUT] NYLUG Presents: 5/19 -at- 6:30PM Wietse Venema
  13. 2010-05-08 Ruben Safir <mrbrklyn-at-panix.com> Re: [NYLXS - HANGOUT] NYLUG Presents: 5/19 -at- 6:30PM Wietse Venema
  14. 2010-05-11 Ron Guerin <ron-at-vnetworx.net> Subject: [NYLXS - HANGOUT] NYLUG Workshop / Hacking Society (Smalltalk, C++, Python) Tuesday May 11 6:00PM-8:00PM
  15. 2010-05-12 Ruben Safir <mrbrklyn-at-panix.com> Subject: [NYLXS - HANGOUT] Next NYLXS Meeting
  16. 2010-05-19 swd <sderrick-at-optonline.net> RE: [NYLXS - HANGOUT] Next NYLXS Meeting
  17. 2010-05-21 Ruben Safir <mrbrklyn-at-panix.com> Re: [NYLXS - HANGOUT] Next NYLXS Meeting
  18. 2010-05-25 Ron Guerin <ron-at-vnetworx.net> Subject: [NYLXS - HANGOUT] NYLUG Workshop / Hacking Society (Smalltalk, C++, Python) Tuesday May 25 6:00PM-8:00PM
  19. 2010-05-29 Ron Guerin <ron-at-vnetworx.net> Subject: [NYLXS - HANGOUT] Free Software Round Table TONIGHT at 10PM (live streaming and radio)

NYLXS are Do'ers and the first step of Doing is Joining! Join NYLXS and make a difference in your community today!