Loading ...
Sorry, an error occurred while loading the content.

Re: [midatlanticretro] Stupid C question/Altair32

Expand Messages
  • Richard A. Cini
    Bingo! Did the trick. Thanks for the help! ... Rich -- Rich Cini Collector of Classic Computers Build Master and lead engineer, Altair32 Emulator
    Message 1 of 15 , Dec 5, 2008
    • 0 Attachment
      Re: [midatlanticretro] Stupid C question/Altair32 Bingo! Did the trick. Thanks for the help!


      On 12/5/08 11:53 PM, "M. Frank" <mmfrank2001@...> wrote:


       



      --- On Fri, 12/5/08, Russell Frank <rfrank@...> wrote:
      From: Russell Frank <rfrank@...>
      Subject: Re: Fw: Re: [midatlanticretro] Stupid C question/Altair32
      To: mmfrank2001@...
      Date: Friday, December 5, 2008, 11:50 PM

        
      Yep.

      Why are you taking an array as a parameter? Never, ever take arrays as parameters.  The entire array is copied in memory.  This is generally considered to be a bad thing.

      When you need to take an array as an argument, you take a pointer.  So, define your function as this:

        double foo (int a, int b, double * c)

      Normally, you'd also want to take an int that would be the # of elements in that array, but in this case since you know the length you can omit this.  In your function, you'd access the array exactly the same way as otherwise.  Here's a quick test case I wrote up:

      #include <stdio.h>
      #include <stdlib.h>

      void
      some_func (double * array_pointer)
      {
        int i;
        for (i = 0; i <= 3; i++)
          printf ("Number %i: %f\n", i, array_pointer[i]);
      }

      int
      main (int argc, char ** argv)
      {
        double some_array[4] = {1, 2, 4, 5};
        some_func (some_array);
      }

      Hope this helps!
      Russell Frank
      Burst Development


      M. Frank wrote:
        
       can you figure this out
       
      --- On Fri, 12/5/08, Richard A. Cini <rcini@...> <mailto:rcini@...> wrote:
       
      From: Richard A. Cini <rcini@...> <mailto:rcini@...>
      Subject: Re: [midatlanticretro] Stupid C question/Altair32
      To: "midatlanticretro@yahoogroups.com" <mailto:midatlanticretro@yahoogroups.com> <midatlanticretro@yahoogroups.com> <mailto:midatlanticretro@yahoogroups.com>
      Date: Friday, December 5, 2008, 11:39 PM
       
         So declare the function without the const but call it with a variable that was cast with a const? Tried it...same error.
       
      I also tried it with and without the array dimension (81) and with and without the const modifier on the variable declaration. The MSVC error is C2664. The on-line help is less than helpful.
       
       
      On 12/5/08 11:24 PM, "Mason Taube" <mason_taube@ yahoo.com> wrote:
       
        

      try double foo(blah, blah, double h[]);
       
       
      From: Richard A. Cini <rcini@optonline. net> <mailto:rcini@...>
       To: MARCH-Post <midatlanticretro@ yahoogroups. com>
       Sent: Friday, December 5, 2008 11:17:04 PM
       Subject: [midatlanticretro] Stupid C question/Altair32
       

       All:
       
          Ok, this has been driving me nuts for the last hour. My C is kind of lousy and I’ve seem to have forgotten something that is undoubtedly easy. This is part of the WAV file processing project I’m working on.
       
          I have declared a constant array containing coefficients for a filter (as follows):
       
          const double coeff[81] ={ ...a whole bunch of numbers };
       
          The function that requires this array as a parameter is declared as follows:   double foo(blah, blah, const double h[]);
       
          When I call the function, I should be able to do this: a = foo(blah, blah, coeff);
       
          I get an error that basically the compiler can’t convert from “const double coeff[81]” to “const double coeff”. I tried removing the array dimension but that doesn’t make a difference.
       
          Any help would be appreciated. Thanks!
       
       
       

       
      Rich
       
      --
      Rich Cini
      Collector of Classic Computers
      Build Master and lead engineer, Altair32 Emulator
       http://www.altair32 .com <http://www.altair32.com>
       http://www.classicc mp.org/cini <http://www.classiccmp.org/cini>
        
       
       
          


       
       
          


      Rich

      --
      Rich Cini
      Collector of Classic Computers
      Build Master and lead engineer, Altair32 Emulator
      http://www.altair32.com
      http://www.classiccmp.org/cini
    • david_comley@yahoo.com
      Rich, Try defining foo() as double foo(blah, blah, const double *h) and call as foo(blah,blah,coeff), or even foo(blah,blah,&coeff) which amounts to the same
      Message 2 of 15 , Dec 6, 2008
      • 0 Attachment
        Rich,

        Try defining foo() as double foo(blah, blah, const double *h) and call as foo(blah,blah,coeff), or even foo(blah,blah,&coeff) which amounts to the same thing.

        Regards,
        -Dave

        Sent via BlackBerry from T-Mobile


        From: "Richard A. Cini"
        Date: Fri, 05 Dec 2008 23:17:04 -0500
        To: MARCH-Post<midatlanticretro@yahoogroups.com>
        Subject: [midatlanticretro] Stupid C question/Altair32

        All:

            Ok, this has been driving me nuts for the last hour. My C is kind of lousy and I’ve seem to have forgotten something that is undoubtedly easy. This is part of the WAV file processing project I’m working on.

            I have declared a constant array containing coefficients for a filter (as follows):

            const double coeff[81] ={ ...a whole bunch of numbers };

            The function that requires this array as a parameter is declared as follows:   double foo(blah, blah, const double h[]);

            When I call the function, I should be able to do this: a = foo(blah, blah, coeff);

            I get an error that basically the compiler can’t convert from “const double coeff[81]” to “const double coeff”. I tried removing the array dimension but that doesn’t make a difference.

            Any help would be appreciated. Thanks!

        Rich

        --
        Rich Cini
        Collector of Classic Computers
        Build Master and lead engineer, Altair32 Emulator
        http://www.altair32 .com
        http://www.classicc mp.org/cini

      • Richard A. Cini
        No, I actually didn¹t know that. I¹m trying to use someone else¹s processing algorithm in my code. Now I know. Thanks again to everyone for the help. Now to
        Message 3 of 15 , Dec 6, 2008
        • 0 Attachment
          Re: [midatlanticretro] Stupid C question/Altair32 No, I actually didn’t know that. I’m trying to use someone else’s processing algorithm in my code. Now I know.

          Thanks again to everyone for the help. Now to see if the program actually works :-)




          On 12/6/08 12:40 AM, "M. Frank" <mmfrank2001@...> wrote:


           

          You do realize that when you pass an array that way, using a[] as a param in the definition of the function, that the entire array is copied in memory, right?  It's completely nonsensical to pass an uninitialized array like that, you're essentially making a copy of uninitialized memory, then using it, then freeing it.  Might as well just malloc your own work array in the function, or define the work array right there as it'll be freed as soon as it goes out of scope.

          --- On Fri, 12/5/08, Richard A. Cini <rcini@...> wrote:
          From: Richard A. Cini <rcini@...>
          Subject: Re: [midatlanticretro] Stupid C question/Altair32
          To: "midatlanticretro@yahoogroups.com" <midatlanticretro@yahoogroups.com>, "Bryan Pope" <bryan.pope@...>
          Date: Friday, December 5, 2008, 11:56 PM

            On 12/5/08 11:39 PM, "Bryan Pope" <bryan.pope@comcast. net <mailto:bryan.pope%40comcast.net> > wrote:

          > I am assuming the array coeff is defined outside of your functions so
          > that coeff [] is accessible to all.

          The coeff array is declared within the function that calls the FFT function
          that uses the coeff array ("coeff" is a parameter to the FFT function). The
          FFT function uses the entire 81-element array during one pass of the
          function (it's run inside a loop, processing X-byte sized chunks of a large
          buffer).

          Coeff is actually an initialized array of doubles: const double
          coeff1850[NTAPS] ={...}; (#define NTAPS 81)

          The FFT function is declared as double FFT(int a, int b, const double h[],
          double z[]);

          The second array ("z[]") is an uninitialized work buffer for the FFT
          function.

          When called in the processing loop, FFT is called thusly:
          A = FFT(var1, var2, coeff1850, z);

          Rich

          > items in your array, you would declare your function like this double
          > foo (blah, blah, int coeff_item); Then when you call function foo you
          > would do this: a = foo (blah, blah, coeff_item); where coeff_item would
          > be a int from 0 - 80. In function foo you would then use coeff [] by
          > doing coeff [coeff_item] .
          >
          > Cheers,
          >
          > Bryan
          >
          > P.S. I would also do some sanity checking by:
          > #define MAX_COEFF_ITEMS 81
          > const double coeff [MAX_COEFF_ITEMS] = { ... ... }
          > then in function foo ()
          > if (coeff_item >= MAX_COEFF_ITEMS) {
          > ... output an error message ..
          > exit (-1);
          > }
          >
          >
          > ------------ --------- --------- ------
          >
          > Yahoo! Groups Links
          >
          >
          >

          Rich

          --
          Rich Cini
          Collector of Classic Computers
          Build Master and lead engineer, Altair32 Emulator
          http://www.altair32 .com <http://www.altair32.com>
          http://www.classicc mp.org/cini <http://www.classiccmp.org/cini>

           
             

           
           
              


          Rich

          --
          Rich Cini
          Collector of Classic Computers
          Build Master and lead engineer, Altair32 Emulator
          http://www.altair32.com
          http://www.classiccmp.org/cini
        • Dan Roganti
          ... wrote: Why are you taking an array as a parameter? Never, ever take arrays as parameters. The entire array is copied in memory. This is generally
          Message 4 of 15 , Dec 6, 2008
          • 0 Attachment


            M. Frank wrote:


            --- On Fri, 12/5/08, Russell Frank <rfrank@...> wrote:


            Why are you taking an array as a parameter? Never, ever take arrays as parameters.  The entire array is copied in memory.  This is generally considered to be a bad thing.

            When you need to take an array as an argument, you take a pointer.  So, define your function as this:

              double foo (int a, int b, double * c)



            yep, pointers is the proper way.
            This is something done even in Pascal while back in the school.

            =Dan
            [ Pittsburgh 250th --- http://www2.applegate.org/~ragooman/   ]
            
          • Dan Roganti
            Dan Roganti wrote: yep, pointers is the proper way. This is something done even in Pascal while back in the school. I forgot to include another example. I m no
            Message 5 of 15 , Dec 6, 2008
            • 0 Attachment

              Dan Roganti wrote:

              yep, pointers is the proper way.
              This is something done even in Pascal while back in the school.

              I forgot to include another example.
              I'm no expert on C so I use this website among several others for reference.
              This one page describes pointers and includes and example on passing the pointer array to a function.
              http://lib.daemon.am/Books/C/ch15/ch15.htm#Heading7

              my other links to C references is on my other website.
              http://www.pittsburghroboticssociety.net/roboswarm_avr.html

              =Dan
              -- 
              [ Pittsburgh 250th --- http://www2.applegate.org/~ragooman/   ]

            • Richard A. Cini
              That¹s a great Web site. Thanks for the link. ... Rich -- Rich Cini Collector of Classic Computers Build Master and lead engineer, Altair32 Emulator
              Message 6 of 15 , Dec 6, 2008
              • 0 Attachment
                Re: [midatlanticretro] Stupid C question/Altair32 That’s a great Web site. Thanks for the link.


                On 12/6/08 9:56 AM, "Dan Roganti" <ragooman@...> wrote:


                 


                Dan Roganti wrote:
                 
                yep, pointers is the proper way.
                This is something done even in Pascal while back in the school.

                I forgot to include another example.
                I'm no expert on C so I use this website among several others for reference.
                This one page describes pointers and includes and example on passing the pointer array to a function.
                http://lib.daemon.am/Books/C/ch15/ch15.htm#Heading7

                my other links to C references is on my other website.
                http://www.pittsburghroboticssociety.net/roboswarm_avr.html

                =Dan


                Rich

                --
                Rich Cini
                Collector of Classic Computers
                Build Master and lead engineer, Altair32 Emulator
                http://www.altair32.com
                http://www.classiccmp.org/cini
              • Russell Frank
                My bad, you actually are right. I don t remember exactly who it was who told me that it s copied unless you pass a reference, but that s wrong - a[] and *a
                Message 7 of 15 , Dec 6, 2008
                • 0 Attachment
                  My bad, you actually are right.  I don't remember exactly who it was who told me that it's copied unless you pass a reference, but that's wrong - a[] and *a are technically the same thing, but just because the compiler replaces it for you.

                  However, since by typing a[] or a[8] or any length you're still just passing a reference (a pointer to the first element), it doesn't make any sense to write a[]; you might as well write the correct form *a.  This is evidenced by the fact that my other answer fixed the problem.

                  see http://c-faq.com/aryptr/aryptrparam.html
                  Russell Frank
                  Burst Development


                  Sridhar Ayengar wrote:

                  M. Frank wrote:
                  > You do realize that when you pass an array that way, using a[] as a
                  > param in the definition of the function, that the entire array is copied
                  > in memory, right? It's completely nonsensical to pass an uninitialized
                  > array like that, you're essentially making a copy of uninitialized
                  > memory, then using it, then freeing it. Might as well just malloc your
                  > own work array in the function, or define the work array right there as
                  > it'll be freed as soon as it goes out of scope.

                  Arrays aren't passed by value in C though, right? So you'll just be
                  passing a reference to an uninitialized memory location, not copying an
                  uninitialized memory location.

                  Peace... Sridhar

                Your message has been successfully submitted and would be delivered to recipients shortly.