prob with int**

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • the consiglieri

    prob with int**

    hello there ..
    im not much an expert in C....im trying to use
    int** for reperesentation of matrices(as double subscripted
    arrays .)but at run time an error occurs ..saying ...general exception
    error ...processor fault ....im using TURBO C v4.5 so plzif any body
    could help me
  • Nick Keighley

    #2
    Re: prob with int**

    On Sep 21, 12:16 pm, the consiglieri <graciahell...@ gmail.comwrote:
                          im not much an expert in C.....im trying to use
    int** for reperesentation of matrices(as double subscripted
    arrays .)but at run time an error occurs ..saying ...general exception
    error ...processor fault ....im using TURBO C v4.5  so plzif any body
    could help me
    Post your code, we can't mind read.
    Post in standard english. Nether "...." not "plzif" are standard
    english.


    --
    Nick Keighley

    Comment

    • Flash Gordon

      #3
      Re: prob with int**

      the consiglieri wrote, On 21/09/08 12:16:
      hello there ..
      im not much an expert in C....im trying to use
      int** for reperesentation of matrices(as double subscripted
      arrays .)but at run time an error occurs ..saying ...general exception
      error ...processor fault ....im using TURBO C v4.5 so plzif any body
      could help me
      There is an error on line 42. If I'm wrong try reading the comp.lang.c
      FAQ at http://c-faq.com/ probably starting with section 6, but also 4
      and 5. If you still can't fix it and the problem is not on line 42 post
      a small compilable example that demonstrates the problem. After all,
      without seeing your code all we can do is guess!
      --
      Flash Gordon
      If spamming me sent it to smap@spam.cause way.com
      If emailing me use my reply-to address
      See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

      Comment

      • Bartc

        #4
        Re: prob with int**


        "the consiglieri" <graciahellboy@ gmail.comwrote in message
        news:8acdc55c-4f44-4bff-8a2a-c66c48f3033b@d7 7g2000hsb.googl egroups.com...
        hello there ..
        im not much an expert in C....im trying to use
        int** for reperesentation of matrices(as double subscripted
        arrays .)but at run time an error occurs ..saying ...general exception
        error ...processor fault ....im using TURBO C v4.5 so plzif any body
        could help me
        int** requires that you manually allocate memory, which for a 2D array has
        to be done in a structured form; have you done so?

        --
        Bartc

        Comment

        • Malcolm McLean

          #5
          Re: prob with int**


          "the consiglieri" <graciahellboy@ gmail.comwrote in message
          hello there ..
          im not much an expert in C....im trying to use
          int** for reperesentation of matrices(as double subscripted
          arrays .)but at run time an error occurs ..saying ...general exception
          error ...processor fault ....im using TURBO C v4.5 so plzif any body
          could help me
          >
          Hetre's how to do it.

          remember to #include stdlib.h

          int **mtx;

          mtx = malloc(height * sizeof(int *));
          if(!mtx)
          /* out of memory error, handle here */
          for(i=0;i<heigh t;i++)
          {
          mtx[i] = malloc(width * sizeof(int));
          if(!mtx[i])
          /* out of memory, handle here */
          }

          /* ( initialise to zero) */
          for(i=0;i,heigh t;i++)
          for(ii=0;ii<wid th;ii++)
          mxt[i][ii] = 0;


          --
          Free games and programming goodies.


          Comment

          • Barry Schwarz

            #6
            Re: prob with int**

            On Sun, 21 Sep 2008 16:54:48 +0100, "Malcolm McLean"
            <regniztar@btin ternet.comwrote :
            >
            >"the consiglieri" <graciahellboy@ gmail.comwrote in message
            >hello there ..
            > im not much an expert in C....im trying to use
            >int** for reperesentation of matrices(as double subscripted
            >arrays .)but at run time an error occurs ..saying ...general exception
            >error ...processor fault ....im using TURBO C v4.5 so plzif any body
            >could help me
            >>
            >
            >Hetre's how to do it.
            >
            >remember to #include stdlib.h
            >
            >int **mtx;
            >
            >mtx = malloc(height * sizeof(int *));
            >if(!mtx)
            /* out of memory error, handle here */
            >for(i=0;i<heig ht;i++)
            >{
            mtx[i] = malloc(width * sizeof(int));
            if(!mtx[i])
            /* out of memory, handle here */
            >}
            >
            >/* ( initialise to zero) */
            >for(i=0;i,heig ht;i++)
            Obvious finger check. s/i,/i<
            for(ii=0;ii<wid th;ii++)
            mxt[i][ii] = 0;
            --
            Remove del for email

            Comment

            • jameskuyper@verizon.net

              #7
              Re: prob with int**

              Bartc wrote:
              "the consiglieri" <graciahellboy@ gmail.comwrote in message
              news:8acdc55c-4f44-4bff-8a2a-c66c48f3033b@d7 7g2000hsb.googl egroups.com...
              hello there ..
              im not much an expert in C....im trying to use
              int** for reperesentation of matrices(as double subscripted
              arrays .)but at run time an error occurs ..saying ...general exception
              error ...processor fault ....im using TURBO C v4.5 so plzif any body
              could help me
              >
              int** requires that you manually allocate memory, which for a 2D array has
              to be done in a structured form; have you done so?
              I'm not sure what you mean by "manually allocate" and "structured ".
              Does the following code qualify?:

              int row1[] = {1,2,3};
              int row2[] = {-6,-5,-4};
              int *rows[] = {row1, row2};
              int **array = rows;

              Comment

              • Bartc

                #8
                Re: prob with int**


                "Malcolm McLean" <regniztar@btin ternet.comwrote in message
                news:N-ydnZm37YzV80vVR VnyhAA@bt.com.. .
                >
                "the consiglieri" <graciahellboy@ gmail.comwrote in message
                >hello there ..
                > im not much an expert in C....im trying to use
                >int** for reperesentation of matrices(as double subscripted
                >arrays .)but at run time an error occurs ..saying ...general exception
                >error ...processor fault ....im using TURBO C v4.5 so plzif any body
                >could help me
                int **mtx;
                >
                mtx = malloc(height * sizeof(int *));
                if(!mtx)
                /* out of memory error, handle here */
                for(i=0;i<heigh t;i++)
                {
                mtx[i] = malloc(width * sizeof(int));
                if(!mtx[i])
                /* out of memory, handle here */
                }
                >
                /* ( initialise to zero) */
                for(i=0;i,heigh t;i++)
                for(ii=0;ii<wid th;ii++)
                mxt[i][ii] = 0;
                This is fiddly enough to be put into it's own function. Here's my code in
                bad C. As written it requires the matrix to be indexed as [y][x] rather than
                than [x][y] (but it's so long since I've had to subscript a matrix that I
                can't remember the form).

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

                typedef int T; /* matrix element type */

                T** allocmatrix(int rows, int columns) {
                T **p;
                int i;

                if (rows<=0 || columns <=0) return NULL;

                p=malloc(sizeof (T*)*rows);
                if (p==NULL) return NULL;

                for (i=0; i<rows; ++i) {
                p[i]=malloc(sizeof( T)*columns);
                if (p[i]==NULL) {
                /* possibly free already allocated rows here, and p */
                return NULL;
                }
                memset(p[i],0,sizeof(T)*co lumns);
                }

                return p;
                }

                void freematrix(T** p, int rows){
                int i;

                for (i=0; i<rows; ++i)
                free(p[i]);

                free(p);
                }

                int main(void){
                #define rows 8
                #define columns 12
                T **mat;
                int x,y;

                mat=allocmatrix (rows,columns);

                if (mat==NULL) exit(0);

                for (y=0; y<rows; ++y)
                for (x=0; x<columns; ++x)
                mat[y][x]=y*1000+x;

                for (y=0; y<rows; ++y) {
                for (x=0; x<columns; ++x)
                printf("%04d ",mat[y][x]);
                puts("");
                }

                freematrix(mat, rows);

                }

                --
                Bartc

                Comment

                • Bartc

                  #9
                  Re: prob with int**


                  <jameskuyper@ve rizon.netwrote in message
                  news:3b3914fb-4114-49f2-8f7f-ae6828442741@p2 5g2000hsf.googl egroups.com...
                  Bartc wrote:
                  >"the consiglieri" <graciahellboy@ gmail.comwrote in message
                  >news:8acdc55 c-4f44-4bff-8a2a-c66c48f3033b@d7 7g2000hsb.googl egroups.com...
                  hello there ..
                  im not much an expert in C....im trying to use
                  int** for reperesentation of matrices(as double subscripted
                  arrays .)but at run time an error occurs ..saying ...general exception
                  error ...processor fault ....im using TURBO C v4.5 so plzif any body
                  could help me
                  >>
                  >int** requires that you manually allocate memory, which for a 2D array
                  >has
                  >to be done in a structured form; have you done so?
                  >
                  I'm not sure what you mean by "manually allocate"
                  Use malloc explicitly.
                  >and "structured ".
                  Make use of a row of pointers. See my other post for example.
                  Does the following code qualify?:
                  >
                  int row1[] = {1,2,3};
                  int row2[] = {-6,-5,-4};
                  int *rows[] = {row1, row2};
                  int **array = rows;
                  This is structured. But not manually allocated using malloc. I'm assuming
                  the matrix size is a variable (or is large) otherwise you can just declare a
                  regular matrix:

                  int matrix[6][9];

                  --
                  bartc

                  Comment

                  • Malcolm McLean

                    #10
                    Re: prob with int**


                    "Bartc" <bc@freeuk.comw rote in message
                    I'm assuming
                    the matrix size is a variable (or is large) otherwise you can just declare
                    a
                    regular matrix:
                    >
                    int matrix[6][9];
                    >
                    2D arrays have all sorts of problems. Firstly they cannot be resized.
                    Secondly the syntax for passing them to other functions is so complex as to
                    be effectively unusable. Thirdly the subroutines have to be written to take
                    fixed-width matrices.
                    Arrays of pointer, whilst not ideal, get around many of these problems.

                    My favoured solution is to just allocate a flat buffer and calculate the
                    offset

                    matrix[y*height+x] = val;


                    --
                    Free games and programming goodies.


                    Comment

                    • Flash Gordon

                      #11
                      Re: prob with int**

                      Malcolm McLean wrote, On 21/09/08 16:54:
                      >
                      "the consiglieri" <graciahellboy@ gmail.comwrote in message
                      >hello there ..
                      > im not much an expert in C....im trying to use
                      >int** for reperesentation of matrices(as double subscripted
                      >arrays .)but at run time an error occurs ..saying ...general exception
                      >error ...processor fault ....im using TURBO C v4.5 so plzif any body
                      >could help me
                      >>
                      >
                      Hetre's how to do it.
                      Well, it is *one* way to do it, but not the only way. The comp.lang.c
                      FAQ has more methods at hrrp://c-faq.com/
                      remember to #include stdlib.h
                      >
                      int **mtx;
                      >
                      mtx = malloc(height * sizeof(int *));
                      Better would be
                      mtx = malloc(height * sizeof *mtx);
                      if(!mtx)
                      /* out of memory error, handle here */
                      Note that the out of memory handler needs to stop it from executing the
                      code below.
                      for(i=0;i<heigh t;i++)
                      {
                      mtx[i] = malloc(width * sizeof(int));
                      <snip>

                      Here it would be better to have
                      mtx[i] = malloc(width * sizeof *mtx[1]);

                      Note the how many times Malcolm had to get the type right compared to
                      how many times I need to.

                      Also depending on what you are trying to do it could be the wrong way.
                      --
                      Flash Gordon
                      If spamming me sent it to smap@spam.cause way.com
                      If emailing me use my reply-to address
                      See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

                      Comment

                      • Bartc

                        #12
                        Re: prob with int**


                        "Malcolm McLean" <regniztar@btin ternet.comwrote in message
                        news:a7udnVznct _pFEvVnZ2dnUVZ8 t7inZ2d@bt.com. ..
                        >
                        "Bartc" <bc@freeuk.comw rote in message
                        >I'm assuming
                        >the matrix size is a variable (or is large) otherwise you can just
                        >declare a
                        >regular matrix:
                        >>
                        >int matrix[6][9];
                        >>
                        2D arrays have all sorts of problems. Firstly they cannot be resized.
                        Secondly the syntax for passing them to other functions is so complex as
                        to be effectively unusable. Thirdly the subroutines have to be written to
                        take fixed-width matrices.
                        Arrays of pointer, whilst not ideal, get around many of these problems.
                        >
                        My favoured solution is to just allocate a flat buffer and calculate the
                        offset
                        >
                        matrix[y*height+x] = val;
                        Don't you mean, y*width?

                        And to pass this to a function still requires this extra parameter, and
                        possibly height too, depending on what the function does.

                        (You also have the nuisance of having this extra value: height or width,
                        associated with each matrix access, and the overhead of an extra multiply.)

                        --
                        Bartc


                        Comment

                        • Malcolm McLean

                          #13
                          Re: prob with int**


                          "Bartc" <bc@freeuk.comw rote in message news
                          >
                          "Malcolm McLean" <regniztar@btin ternet.comwrote in message
                          news:a7udnVznct _pFEvVnZ2dnUVZ8 t7inZ2d@bt.com. ..
                          >>
                          >"Bartc" <bc@freeuk.comw rote in message
                          >>I'm assuming
                          >>the matrix size is a variable (or is large) otherwise you can just
                          >>declare a
                          >>regular matrix:
                          >>>
                          >>int matrix[6][9];
                          >>>
                          >2D arrays have all sorts of problems. Firstly they cannot be resized.
                          >Secondly the syntax for passing them to other functions is so complex as
                          >to be effectively unusable. Thirdly the subroutines have to be written to
                          >take fixed-width matrices.
                          >Arrays of pointer, whilst not ideal, get around many of these problems.
                          >>
                          >My favoured solution is to just allocate a flat buffer and calculate the
                          >offset
                          >>
                          >matrix[y*height+x] = val;
                          >
                          Don't you mean, y*width?
                          >
                          And to pass this to a function still requires this extra parameter, and
                          possibly height too, depending on what the function does.
                          >
                          (You also have the nuisance of having this extra value: height or width,
                          associated with each matrix access, and the overhead of an extra
                          multiply.)
                          >
                          Yes I do, which is the disadvantage of the manaul method. It's too easy to
                          make that type of slip.
                          The multiplication overhead is a type of micro-optimisation. Whilst there
                          are circumstances where it can make or break a routine, generally that's
                          what you worry about after you have a nice functioning program. Sometimes
                          there is a hidden multiply in a 2D array access, though I admit it is easier
                          for the compiler to optiise out the constant.

                          --
                          Free games and programming goodies.


                          Comment

                          • Ben Bacarisse

                            #14
                            Re: prob with int**

                            "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                            "Bartc" <bc@freeuk.comw rote in message
                            >I'm assuming
                            >the matrix size is a variable (or is large) otherwise you can just
                            >declare a
                            >regular matrix:
                            >>
                            >int matrix[6][9];
                            >>
                            2D arrays have all sorts of problems. Firstly they cannot be
                            resized.
                            I think this is a red-herring. Neither can 1D arrays. It is always
                            going to be a bit fiddly to resize 2D arrays in C no matter what
                            method you use to implement them and your suggestion does not make it
                            any easier than the "natural" way (see below).
                            Secondly the syntax for passing them to other functions is so
                            complex as to be effectively unusable.
                            No so in C99, and a temporary typedef can be used to make even the
                            allocation be neat and simple:

                            int main(int argc, char **argv)
                            {
                            size_t n = argc 1 ? atoi(argv[1]) : 4;
                            size_t m = argc 2 ? atoi(argv[2]) : 5;
                            typedef double Array[n][m];
                            Array *ap = malloc(sizeof *ap);
                            fill(n, m, *ap);
                            print(n, m, *ap);
                            return 0;
                            }

                            How hard is that? It looks logical and consistent. The function
                            prototypes are not obscure either[1]:

                            void fill(size_t n, size_t m, double array[n][m]);
                            void print(size_t n, size_t m, double array[n][m]);
                            Thirdly the subroutines have to
                            be written to take fixed-width matrices.
                            Again, not in C99. Now I know that C99 is not as portable as C90 but I
                            think it deserves a fair hearing. This is one thing it does
                            reasonable well. The fill function might just be:

                            void fill(size_t n, size_t m, double array[n][m])
                            {
                            for (size_t r = 0; r < n; r++)
                            for (size_t c = 0; c < n; c++)
                            array[r][c] = (r + 1) * (c + 1);
                            }

                            When doing numerical work with arrays in C, C99's features deserve
                            serious consideration.

                            To the OP: do consider using C99 if it is available to you.

                            [1] The form using * is rather less obvious until you think of it is
                            simply a way to say "some parameter -- the name does not matter":

                            void fill(size_t, size_t, double array[*][*]);

                            --
                            Ben.

                            Comment

                            • Barry Schwarz

                              #15
                              Re: prob with int**

                              On Sun, 21 Sep 2008 18:50:44 +0100, "Malcolm McLean"
                              <regniztar@btin ternet.comwrote :
                              >
                              >"Bartc" <bc@freeuk.comw rote in message
                              >I'm assuming
                              >the matrix size is a variable (or is large) otherwise you can just declare
                              >a
                              >regular matrix:
                              >>
                              >int matrix[6][9];
                              >>
                              >2D arrays have all sorts of problems. Firstly they cannot be resized.
                              Isn't the same true for 1D arrays?
                              >Secondly the syntax for passing them to other functions is so complex as to
                              You really consider
                              int func(int x[][9]){...}
                              to be too complex to use?
                              >be effectively unusable. Thirdly the subroutines have to be written to take
                              >fixed-width matrices.
                              >Arrays of pointer, whilst not ideal, get around many of these problems.
                              >
                              >My favoured solution is to just allocate a flat buffer and calculate the
                              >offset
                              >
                              >matrix[y*height+x] = val;
                              --
                              Remove del for email

                              Comment

                              Working...