Sponsored Content
Top Forums Programming Segmentation Fault ERROR in C Post 302958137 by DinisR on Monday 19th of October 2015 07:52:49 PM
Old 10-19-2015
Thank you for your answer!

Here are my functions which weren't illustrated.

check_property

Code:
int check_property(int *vec, int iL, int iR) {
  int k, res, resl, resr;
  int iM = (iR - iL) / 2;

  if (iM == 0) {
    if (vec[iL] != vec[iR]) return(0);
    else return(1);
  }
  for (res = 1, k = 0; k < (iR-iL); k++) {
    if (vec[iL + k] != vec[iL + k + 1]) {
      res = 0;
      break;
    }
  }
  resl = check_property(vec, iL, iL + iM);
  resr = check_property(vec, iL + iM+1, iR);
  return(res+min(resl,resr));
}

check

Code:
int check(int N) {
  int K = 1;

  while (K < N) {
    K = K * 2;
    if (K == N)
      return(1);
  }
  return(0);
}

min

Code:
int min(int a, int b) {
  if (a < b) return (a);
  else return(b);
}

But i agree with your code, and it works, the s() functions works fine on
&vec[s(N, i, j)]

But i still cant understand why it doesnt works with just
&vec[i][j]

You say:
Quote:
the compiler has never been told the dimensions of that array. So, that can't work.
By inc the i an j, I'm giving the dimension of the array until it reaches its max which is N^2 or am I incorrect?

Is there a way to do it without using the s() function?


POST EDIT:

I have done a new solution, i came up with this on the memory allocation:

Code:
/* allocate memory, read in the array and print it */
  vec = (int**)malloc(N*sizeof(int*));
  if ( vec == NULL ) {
    fprintf (stderr, "ERROR: not enough memory available!\n");
    exit ( 2 );
  }
/*New allocation*/
  for (j=0; j<N; j++){
    vec[j]= (int*)malloc(N*sizeof(int));
      if ( vec[j] == NULL ) {
    fprintf (stderr, "ERROR: not enough memory available!\n");
    exit ( 4 );
    }
  }

Now i understand what you were telling me about the dimensions.
I have debated this with a friend of mine and he told me there were 2 solutions to my problem; this one of the memory allocation, or yours, creating a s() function like you did

Thank you for your time!

Last edited by DinisR; 10-19-2015 at 10:43 PM..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
sigaction(3)						     Library Functions Manual						      sigaction(3)

Name
       sigaction - software signal facilities (POSIX)

Syntax
       #include <signal.h>

       struct sigaction {
       void  (*sa_handler)();
       sigset_t sa_mask;
       int   sa_flags;
       };

       int sigaction(sig, vec, ovec)
       int sig;
       struct sigaction *vec, *ovec;

Description
       The sigaction call is the POSIX equivalent to the system call. This call behaves as described on the reference page with the following mod-
       ifications:

       o    The signal mask is manipulated using the functions.

       o    A process can suppress the generation of the SIGCHLD when a child stops by setting the SA_NOCLDSTOP bit in sa_flags.

       o    The SV_INTERRUPT flag is always set by the system when using in POSIX mode. The flag is set so that interrupted system calls will fail
	    with the EINTR error instead of getting restarted.

Return Values
       A  0  return value indicated that the call succeeded.  A -1 return value indicates an error occurred and errno is set to indicated the rea-
       son.

Diagnostics
       The system call fails and a new signal handler is not installed if one of the following occurs:

       [EFAULT]       Either vec or ovec points to memory which is not a valid part of the process address space.

       [EINVAL]       Sig is not a valid signal number.

       [EINVAL]       An attempt is made to ignore or supply a handler for SIGKILL or SIGSTOP.

See Also
       sigvec(2), sigsetops(3), sigprocmask(3), sigsuspend(3), sigpending(2), setjmp(3), siginterrupt(3), tty(4)

																      sigaction(3)
All times are GMT -4. The time now is 08:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy