Segmentation Fault ERROR in C


 
Thread Tools Search this Thread
Top Forums Programming Segmentation Fault ERROR in C
Prev   Next
# 1  
Old 10-18-2015
Error Segmentation Fault ERROR in C

Hello guys, i have this code:

Code:
int main(int argc, char *argv[]) {
  int i, j, N, result = 0, **vec;

  if (argc < 2) {
    fprintf(stderr, "Usage: %s N\n", argv[0]);

  }

  /* ... how many numbers to read */
  N = atoi(argv[1]);
  /* check if N is a power of two; exit if not */
  if (! check(N)) {
    fprintf(stderr, "%d is not a power of 2 as required.  Exiting.\n", N);
    exit(1);
  }

  vec = (int**)malloc( (N)*(N)*sizeof(int*) );
  if ( vec == NULL ) {
    fprintf (stderr, "ERROR: not enough memory available!\n");
    exit ( 2 );
  }

  for (i = 0; i < N; i++)
    for (j = 0; j < N; j++)
      scanf("%d", &vec[i][j]);

  printf("\nOriginal array:\n ");
  for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++)
      printf("%d ", vec[i][j]);
    printf("\n");
  }

  /* Now process array; example below is to add numbers */
  for (i = 0; i < N; i++) {
    result = check_property(vec[i], 0, N-1);
    printf("result[line %d]: %d\n", i, result);
  }

  /* free memory */
  free(vec);

  exit(0);

Basically i have to run it has eg. ./program N
my N needs to be a power of 2, like 4 or 8, 16 etc...
when i run it, it goes to the part of scanf and then if i write a number or file name, it gives me a segmentation fault error. Any ideas why?
I dont know if it is because of the memory allocation...

(There are functions there which i have defined but not illustrated, because they are not the problem i think)

Last edited by DinisR; 10-18-2015 at 08:43 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. 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

3. Programming

segmentation fault.

This code is causing a segmentation fault and I can't figure out why. I'm new to UNIX and I need to learn how to avoid this segmentation fault thing. Thank you so much. Thanks also for the great answers to my last post.:):b: int main() { mysqlpp::Connection conn(false); if... (3 Replies)
Discussion started by: sepoto
3 Replies

4. Programming

getting Segmentation Fault (core dumped) error but Program runs fine.

i am executing following program int main() { char str; FILE * fp; int i=0; ... (4 Replies)
Discussion started by: bhavesh.sapra
4 Replies

5. Shell Programming and Scripting

Segmentation Fault(Core Dump) Error

Hi all, I have a folder with some 28 files. I have a script file that will iteratively take one file at a time from the folder and provide an output for the input file. Till the 7th file, there was no problem but from the 8th file onwards, i got this Segmentation Fault(Core Dump) error. A file... (2 Replies)
Discussion started by: mick_000
2 Replies

6. Programming

segmentation fault

Hi, I am having this segmentation fault not in the following program, bt. in my lab program . My lab program is horrible long so cannot post it here bt. I am using the following logic in my program which is giving the segmentation fault. Bt. if I run this sample program as it is it dosen't give... (3 Replies)
Discussion started by: mind@work
3 Replies

7. UNIX for Advanced & Expert Users

capture sqsh segmentation fault error

hi all is there any way to capture the segmentation fault error when i run sqsh on a unix shell script. Ex: #!/bin/ksh sqsh -S "server" -U "user" -P "pwd" << EOF use mydb go exec proc1 go exit EOF retval=$? echo "sqsh return value $retval" if then exit (1 Reply)
Discussion started by: sudheer1984
1 Replies

8. UNIX for Dummies Questions & Answers

What's the difference between Segmentation fault and Bus error and Illegal...?

What's the difference between Segmentation fault and Bus error and Illegal instruction? Sometimes I got the one, and sometimes i got another, what are their differences? Segmentation fault (core dump)? Bus error (core dump)? Illegal instruction (core dump) Thanks Daniel (2 Replies)
Discussion started by: lakeat
2 Replies

9. AIX

Segmentation fault

Hi , During execution a backup binary i get following error "Program error 11 (Segmentation fault), saving core file in '/usr/datatools" Riyaz (2 Replies)
Discussion started by: rshaikh
2 Replies

10. Programming

Hi! segmentation fault

I have written a program which takes a directory as command line arguments and displays all the dir and files in it. I don't know why I have a problem with the /etc directory.It displays all the directories and files untill it reaches a sub directory called peers which is in /etc/ppp/peers.the... (4 Replies)
Discussion started by: vijlak
4 Replies
Login or Register to Ask a Question