Segment Fault


 
Thread Tools Search this Thread
Top Forums Programming Segment Fault
# 8  
Old 03-29-2006
Quote:
I dare to rectify ur code though I agree with others that ur code shouldn't compile.
your code can be further optimised to have a common function as below instead of having two seperate functions

Code:
#include <stdio.h>
#define max 20
int glb=0;

void comMatrixAA(int AA[max][max], int row, int col)
{     int i,j;
       for( i = 0; i<row; i++)
          {
              for( j = 0; j<col; j++)
              {
                !glb && scanf("%d",&AA[i][j]);
                glb && printf("<-:-> %d\n", AA[i][j]);
              }
          }
        glb=!glb;
}

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");

      comMatrixAA(A, m, k);
      printf("Print Matrix A");
      comMatrixAA(A, m, k);
      return 0;
  }

besides that what is the need to include stdlib.h
# 9  
Old 04-01-2006
Elegant piece of code!! I really appreciate the way u join the two functions. And yes stdlib really didn't mean to be there. I just skipped the non-essential include part and concentrated on the part where zhshqzyc might be erring. But on the combining of the two functions I would like to make a small comment though ur code does what is expected of the code but this might not be the real intension of the programmer, may be he was presenting it in most simple way. Although here the calls to get input and display results are sequential but in real problem he might be facing he may be calling input once and printing the elements twice or thrice so it doesn't make sense to combine two functions with two different functionality.
Further I quote
Quote:
your code can be further optimised to have a common function as below instead of having two seperate functions
I really couldn't figure it out how this tweak of urs ensured code optimization.
The calls are two separate calls and overhead of switching from main to ur function is also incurred twice (as it might have been, had there been two functions as before) rather I feel that ur induction of a global variable 'glb' makes the code more inefficient for most modern C compilers alias local variables to CPUs register's or SRAM but can't do so with global variables. Furthermore, if all variables in a given scope are local, then an optimizing compiler, can forgo maintaining the variable outside the scope, and therefore has more simplification optimization opportunities than with global.
Add to it the time in evaluation of logical operators.
The only way I feel ur code can be made really efficient might be by declaring the function static making the call a fixed function call.

Please rectify me if I've gone wrong somewhere.

Last edited by Rakesh Ranjan; 04-01-2006 at 07:13 AM..
# 10  
Old 04-07-2006
Yes,

It was just a tweak or work around with the legacy of code.
No way its related with the performance of the code.

Its well apted for code with serialized functions, when they are common in call-sequence, generalized functionality- this would serve as an approacher.
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