Segment Fault


 
Thread Tools Search this Thread
Top Forums Programming Segment Fault
# 1  
Old 02-27-2006
Segment Fault

When run it, segment fault.
What is wrong?


Code:
#include <stdio.h>
#include <stdlib.h>
 
const int max =20;


//****************************************************
//     Input Matrix 
//****************************************************

void inMatrixAA(int *AA, int row, int col)
{     int i,j;
        for( i = 0; i<row; i++)
           {
               for( j = 0; j<col; j++)
                 scanf("%d",AA+i*max+j);
           }   
}

//*****************************************************
//   Print Matrix 
//***************************************************** 

void prMatrix( int *AA, int row, int col) 
{       int i,j;
           for(i =0; i<row; i++)
              {
                 for(j = 0; j<col; j++) 
                  printf("%d\n",*(AA+i*max+j));
              }
}

int main(int argc,char *argv[])
   {
       int m,k;
       int A[max][max];
       int ii,jj;
        for (ii = 0; ii<max;ii++)
         {
           for(jj =0;jj<max;jj++)
            {
              A[ii][jj] = 0;
            }
         }            
       printf("Enter values for m, k");
       scanf("%d",&m);
       scanf("%d",&k);

       
       printf("Input Matrix A");
       inMatrixAA(&A[0][0], m, k);

       printf("Print Matrix A");
       prMatrix(&A[0][0], m, k);
   
   }

# 2  
Old 02-28-2006
i wonder,
first this would not compile,

Quote:
int A[max][max];
you need to mention an integral constant expression

may be you would have thought providing an #define value to max

Code:
#define max 20

# 3  
Old 02-28-2006
I tried to compile this ealrier and found even more issues. I just gave up. If it really compiles for the OP, she/he's got a very interesting compiler. Maybe a C++ compiler being used for C.
# 4  
Old 02-28-2006
Tried to compile this a few hours ago on HP-UX 11.11. Being a non ANSI compliant compiler, the thing puked on so many points, I didn't even try to fix the problems.
# 5  
Old 03-28-2006
I dare to rectify ur code though I agree with others that ur code shouldn't compile.

Code:
#include <stdio.h>
#include <stdlib.h>
#define max 20

//****************************************************
//     Input Matrix
//****************************************************

void inMatrixAA(int AA[max][max], int row, int col)
{     int i,j;
        for( i = 0; i<row; i++)
           {
               for( j = 0; j<col; j++)
                 scanf("%d",&AA[i][j]);
           }
}

//*****************************************************
//   Print Matrix
//*****************************************************

void prMatrix( int AA[max][max], int row, int col)
{       int i,j;
           for(i =0; i<row; i++)
              {
                 for(j = 0; j<col; j++)
                   printf("%d\n",AA[i][j]);
              }
}

int main(int argc,char *argv[])
   {
       int A[max][max];
       int m,k;
       int ii,jj;
        for (ii = 0; ii<max;ii++)
         {
           for(jj =0;jj<max;jj++)
            {
              A[ii][jj] = 0;
            }
         }
         printf("Enter values for m, k\n");
       scanf("%d%d",&m,&k);
       printf("Input Matrix A");
       inMatrixAA(A, m, k);
       printf("Print Matrix A");
       prMatrix(A, m, k);
       return 0;
   }

It shall compile.
I would like u to advise :
1) In C use constants expressions in specifying the size of array
U may use preprocessors like #define though.
2) Take care with pointers (if u r passing pointer to pointer in
invoking the function then keep the same in defintion of the function)

Hope these help u.
# 6  
Old 03-28-2006
Good work, Rakesh Ranjan. I like your code. As for zhshqzyc's code, while I do dislike the techniques being used, I don't find the code to as alien as everyone is indicating. I see two items that violate Ansi C: the // style one line comments and the use of "const int max =20;" instead of "#define max 20". Both of these are legal in C++. But also there is a new C standard out called C99. I don't have access to a C99 compiler, but I strongly suspect that the code is legal C99. And for the record, gcc accepts the code as well. When I run the code, it more or less works for me. I also dislike the output format and will never regard output a 2 dimensional array as a column "correct". The coding techniques being used invite the programmer to confuse rows and columns and that is what happened here. I used square matrices to avoid any problems.

Compiling the code on a compiler that does not understand the one line comment style results in a lot of errors. This is because a comment like:
//******
has the beginning of a old c style comment starting with the second character. The compiler has a very hard time recovering from that error. If your C compilers are freaking out, I think that is the principal reason. Even gcc with an -ansi switch gave up, no -pedantic switch needed.
# 7  
Old 03-29-2006
I have a CC native compiler on solaris10..it works perfectly on it..and i dont think that ..const int max =20;" instead of "#define max 20 is required...C++ ANSI standrads dont prohibits it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Segment fault related to strlen.S

Hello, This function was copied into my code, which was compiled without error/warning, but when executed there is always Segmentation fault at the end after the output (which seems correct!): void get_hashes(unsigned int hash, unsigned char *in) { unsigned char *str = in; int pos =... (7 Replies)
Discussion started by: yifangt
7 Replies

2. Programming

Segment fault for C++ program when return vector

I am trying to reverse complement DNA sequence (string) with a short c++ code using boost library. Code was compiled without any warning/error, but ran into Segmentation fault. My guess is the function to return a vector, but not sure. #include <iostream> #include <fstream> #include <string>... (14 Replies)
Discussion started by: yifangt
14 Replies

3. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

4. Programming

Segment-fault handling for pthreads

Hi I have struggling a week to fix a program , in the begining i got SIGBUS , but after many attempts still the program gets SIGSEGV segment fault , In bellow i post the seg fault log + source codes. would really appreciate if experts help me to fix this segment fault error. any advice is... (2 Replies)
Discussion started by: pooyair
2 Replies

5. Programming

why segment fault,

I always get segment fault, why? can sb help me and modify it, I have spend on much time on #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <string.h> #define MAX 10 pthread_t thread; void *thread1() { int *a; int i, n; ... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

6. Programming

Data segment or Text segment

Hi, Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment? char *a = "Hello"; I am getting two different answers while searching in google :( that's why the confusion is (7 Replies)
Discussion started by: royalibrahim
7 Replies

7. Programming

Segment Violation

Hi to all. I'm reciving a "Segment violation" error from this code and I don't know why. void insertAtEnd(NodeType *pList) { char element; printf("Introduce a element: \n"); setbuf(stdin, NULL); scanf("%c", &element); //Find the end of the list; while... (4 Replies)
Discussion started by: daniel.gbaena
4 Replies

8. Programming

How can I know where the segment of memory is all Zero?

I mean, I malloc a segment of memory, maybe 1k maybe 20bytes.. assume the pointer is pMem How can I know the content pMem refered is all Zero or \0 . I know memcmp but the second parameter should another memory address... thanx (4 Replies)
Discussion started by: macroideal
4 Replies

9. Programming

a strange segment fault about ltp-posix test

Hi all In the ltp-posix test,there is a case in open_posix_testsuite\conformance\interfaces\timer_gettime\speculative/6-1.c I run the above code,it will has a segment fault, if I modify it to below,it works well Anybody can tell me why? (1 Reply)
Discussion started by: yanglei_fage
1 Replies

10. Shell Programming and Scripting

extract segment

Hey all, could someone please direct me on how to extract a segment from a file between two tags? Thanks! (1 Reply)
Discussion started by: mpang_
1 Replies
Login or Register to Ask a Question