l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
September 2: Social gathering
Next Installfest:
TBD
Latest News:
Aug. 18: Discounts to "Velocity" in NY; come to tonight's "Photography" talk
Page last updated:
2010 Feb 28 21:58

The following is an archive of a post made to our 'vox mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox] things that really suck about C!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox] things that really suck about C!



Hi Brian,

Wishing you all the best on your path! :)

simplearray2.c::load_arrays() seems to expect a GArray ** (pointer to a pointer; or, pointer to an array of pointers).

simplearray4.c::load_arrays() seems to expect a GArray *** (pointer to a pointer to a pointer; or, pointer to a pointer to an array of pointers).

The latter case is handy if you needed to alloc memory for the GArray array of pointers and optionally for each pointer within the array.

The former case is handy if you only need to alloc memory for each pointer within the array. I.e., the array is already alloc'd.

Not sure if you are requesting clarification, but here goes:

simplearray2.c::main():

garrays_ptr -> (beginning of unallocated array of pointers)

...

call to load_arrays()

copy_of_garrays_ptr -> (beginning of unallocated array of pointers)

...

return from load_arrays() -- copy_of_garrays_ptr destroyed

g_arrays_ptr -> (beginning of allocated array of pointers)


simplearray4.c::main():

garrays_ptr_ptr -> garrays_ptr -> (beginning of unallocated array of pointers)

...

call to load_arrays()

copy_of_garrays_ptr_ptr -> garrays_ptr -> (beginning of unallocated array of pointers)

...

return from load_arrays() -- copy_of_garrays_ptr_ptr destroyed

garrays_ptr_ptr -> garrays_ptr -> (beginning of unallocated array of pointers)


Not sure this helps or makes it more obscure. :X


On Sun, Feb 28, 2010 at 9:00 PM, Brian Lavender <brian@brie.com> wrote:
I am ranting because of strange experiences like the following two
programs, simplearray2.c and simplearray4.c. They both do the same
thing! But look at the argument to load_arrays in version 2 vs 4.

You can compile them with the following (you have to have libglib-2.0
installed for Ubuntu at least).

gcc `pkg-config --libs --cflags glib-2.0` simplearray2.c -o simplearray2

Yes, you are correct, I am on a pilgrimage. Right now, it's for my
Master's Project!

brian


=== simplearray2.c ===

#include <glib.h>

#define NUM_ARYS 5

void load_arrays( GArray *garrays[NUM_ARYS] )
{
 gint i,j, storevalue;
 // put the load arrays stuff here.
 for (j=0; j < NUM_ARYS; j++) {
   garrays[j] = g_array_new (FALSE, FALSE, sizeof (gint));
   g_printf("Load Array %d\n", j);
   for (i = 0; i < 10; i++) {
     storevalue = (i + 103) % ( (j +1) * 2 );
     g_array_append_val ( garrays[j], storevalue );
     g_print ("load idx %d value %d\n",
              i, storevalue );
   }
 }

}

int main() {
   GArray *garrays[NUM_ARYS];
 gint i,j, storevalue;

 // Change the following so that I call
 // load_arrays(garrays);
 // instead of the following

 // start
 load_arrays(garrays);

 // end

 for (j=0; j < NUM_ARYS; j++) {
   g_printf("Array %d\n", j);
   for (i = 0; i < 10; i++)
     g_print ("index %d value %d\n",
              i, g_array_index (garrays[j], gint, i));
 }

 for (j=0; j < NUM_ARYS; j++)
   g_array_free (garrays[j], TRUE);

}

=== simplearray4.c ===


#include <glib.h>

#define NUM_ARYS 5

void load_array( GArray *(*garray)[NUM_ARYS] )
{
 gint i,j, storevalue;
 for (j=0; j < NUM_ARYS; j++) {
   (*garray)[j] = g_array_new (FALSE, FALSE, sizeof (gint));
   g_printf("Load Array %d\n", j);
   for (i = 0; i < 10; i++) {
     storevalue = (i + 103) % ( (j +1) * 2 );
     g_array_append_val ( (*garray)[j], storevalue );
     g_print ("load idx %d value %d\n",
              i, storevalue );
   }
 }
}

int main() {
 GArray *garray[NUM_ARYS];
 gint i,j, storevalue;
 /* We create a new array to store gint values.
    We don't want it zero-terminated or cleared to 0's. */
 load_array(&garray);

 for (j=0; j < NUM_ARYS; j++) {
   g_printf("Array %d\n", j);
   for (i = 0; i < 10; i++)
     g_print ("index %d value %d\n",
              i, g_array_index (garray[j], gint, i));
 }

 for (j=0; j < NUM_ARYS; j++)
   g_array_free (garray[j], TRUE);

}

On Sun, Feb 28, 2010 at 08:03:14PM -0800, Kevin Schultz wrote:
>    Hi Brian,
>    Sounds like you are the path to a true religious experience! ;D
>    mod_array() also receives a pass-by-value argument. ;) C copies the
>    pointer. Variable int b[CAP] is really syntactic sugar for int * b. And
>    so is mod_array()'s formal parameter int a[], which is the same as int
>    * a. So the actual and formal parameters match up. And since you are
>    changing what the pointer points to, all is well when the function
>    returns.
>    However, if you pass an array (say, int a[]) into a function, and then
>    attempt to change what variable a points to, it will last only as long
>    as you are in the function, because a is a copy of the original
>    pointer. For example, a = calloc(4, sizeof(char));. When the function
>    ends, variable a is deleted, control returns to main(), and pointer b
>    still points to its original contents. To get around this, you need to
>    pass a pointer to a pointer into the function (int **b). :P
>    I am likely proving your point about C being a detriment to the field!
>    ;D Let us know if/when you have a true Lisp epiphany. :) I hear it is
>    quite a revolution in thinking!
>
>    On Sun, Feb 28, 2010 at 6:35 PM, Brian Lavender <[1]brian@brie.com>
>    wrote:
>
>      I think if anything, C has been a certain detriment to the field of
>      computer science!
>      One calls a function and the arguments are passed by value. Call a
>      function with an array as an argument, and feel free to modify its
>      contents!
>      Certainly, C++ added the idea of reference, but I think Pascal
>      simplifies these concepts much better. Yet, Pascal seems to be
>      relegated
>      to the status as a legacy language!
>      brian
>      #include <stdio.h>
>      #define CAP 10
>      void mod_array(int a[])
>      {
>       a[2] = 5;
>      }
>      void trychange(int a)
>      {
>       a = 2;
>      }
>      void reallychange(int *a)
>      {
>       *a = 2;
>      }
>      int main() {
>       int b[CAP];
>       int c;
>       int i;
>       printf("Load array and change a value\n");
>       for (i=0; i < CAP; i++)
>         b[i] = i + 20;
>       mod_array(b);
>       for (i=0; i < CAP; i++)
>         printf("b[%d] has value of %d\n",i,b[i]);
>       c = 10;
>       printf("c has a value of %d\n",c);
>       trychange(c);
>       printf("c has a value of %d after trychange(c)\n",c);
>       reallychange(&c);
>       printf("c has a value of %d after reallychange(&c)\n",c);
>      }
>      --
>      Brian Lavender
>      [2]http://www.brie.com/brian/
>      "There are two ways of constructing a software design. One way is to
>      make it so simple that there are obviously no deficiencies. And the
>      other
>      way is to make it so complicated that there are no obvious
>      deficiencies."
>      Professor C. A. R. Hoare
>      The 1980 Turing award lecture
>      _______________________________________________
>      vox mailing list
>      [3]vox@lists.lugod.org
>      [4]http://lists.lugod.org/mailman/listinfo/vox
>
> References
>
>    1. mailto:brian@brie.com
>    2. http://www.brie.com/brian/
>    3. mailto:vox@lists.lugod.org
>    4. http://lists.lugod.org/mailman/listinfo/vox

> _______________________________________________
> vox mailing list
> vox@lists.lugod.org
> http://lists.lugod.org/mailman/listinfo/vox


--
"About 3 million computers get sold every year in China, but people don't
pay for the software. Someday they will, though. As long as they are going
to steal it, we want them to steal ours. They'll get sort of addicted, and
then we'll somehow figure out how to collect sometime in the next decade."

-- Bill Gates (Microsoft) 1998
_______________________________________________
vox mailing list
vox@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox

_______________________________________________
vox mailing list
vox@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox


LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
EDGE Tech Corp.
For donating some give-aways for our meetings.