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
vec(2rheolef)                                                       rheolef-6.1                                                      vec(2rheolef)

NAME
vec - vector in distributed environment (rheolef-6.1) SYNOPSYS
STL-like vector container for a sequential or distributed memory machine model. Additional operation fom classical algebra. EXAMPLE
A sample usage of the class is: int main(int argc, char**argv) { environment distributed(argc, argv); vec<double> x(100, 3.14); dout << x << endl; } IMPLEMENTATION NOTE
Implementation use array<T,M>. IMPLEMENTATION
template <class T, class M = rheo_default_memory_model> class vec : public array<T, M> { public: // typedef: typedef array<T, M> base; typedef typename base::size_type size_type; typedef std::ptrdiff_t difference_type; #ifdef TODO // pb compile avec boost sur foehn: typedef typename base::difference_type difference_type; #endif // TODO typedef basic_range<size_type, difference_type> range_type; typedef typename base::reference reference; typedef typename base::const_reference const_reference; typedef typename base::iterator iterator; typedef typename base::const_iterator const_iterator; // allocator/deallocator: vec (const distributor& ownership, const T& init_val = std::numeric_limits<T>::max()); vec(size_type dis_size = 0, const T& init_val = std::numeric_limits<T>::max()); void resize ( const distributor& ownership, const T& init_val = std::numeric_limits<T>::max()); void resize ( size_type size = 0, const T& init_val = std::numeric_limits<T>::max()); // accessors: const_reference operator[] (size_type i) const; reference operator[] (size_type i); T max_abs () const; // range: vec(const vec_range<T,M>& vr); vec(const vec_range_const<T,M>& vr); vec<T,M>& operator= (const vec_range<T,M>& vr); vec<T,M>& operator= (const vec_range_const<T,M>& vr); vec_range_const<T,M> operator[] (const range_type& r) const; vec_range<T,M> operator[] (const range_type& r); // assignment to a constant: vec<T,M>& operator= (const int& expr); vec<T,M>& operator= (const T& expr); // expression template: template<typename Expr> vec (const Expr& expr); template<typename Expr> vec<T,M>& operator= (const vec_expr<Expr>& expr); template<typename Expr> vec<T,M>& operator+= (const Expr& expr); template<typename Expr> vec<T,M>& operator-= (const Expr& expr); // initializer list (c++ 2011): #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST vec (const std::initializer_list<vec_concat_value<T,M> >& init_list); vec<T,M>& operator= (const std::initializer_list<vec_concat_value<T,M> >& init_list); #endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST }; rheolef-6.1 rheolef-6.1 vec(2rheolef)
All times are GMT -4. The time now is 11:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy