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
Bit::Vector::Minimal(3pm)				User Contributed Perl Documentation				 Bit::Vector::Minimal(3pm)

NAME
Bit::Vector::Minimal - Object-oriented wrapper around vec() SYNOPSIS
use Bit::Vector::Minimal; my $vec = Bit::Vector->new(size => 8, width => 1, endianness => "little"); # These are the defaults $vec->set(1); # $vec's internal vector now looks like "00000010" $vec->get(3); # 0 DESCRIPTION
This is a much simplified, lightweight version of Bit::Vector, and wraps Perl's (sometimes confusing) "vec" function in an object-oriented abstraction. METHODS
new Creates a new bit vector. By default, this creates a one-byte vector with 8 one-bit "slots", with bit zero on the right of the bit pattern. These settings can be changed by passing parameters to the constructor: "size" will alter the size in bits of the vector; "width" will alter the width of the slots. The module will die if "width" is not an integer divisor of "size". "endianness" controls whether the zeroth place is on the right or the left of the bit vector. set(POS[, VALUE]) Sets the bit or slot at position "POS" to value "VALUE" or "all bits on" if "VALUE" is not given. get(POS) Returns the bit or slot at position "POS". display Display the vector. For debugging purposes. AUTHOR
Current maintainer: Tony Bowden Original author: Simon Cozens BUGS and QUERIES Please direct all correspondence regarding this module to: bug-Bit-Vector-Minimal@rt.cpan.org SEE ALSO
Bit::Vector COPYRIGHT AND LICENSE
Copyright 2003, 2004 by Kasei This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.12.3 2005-10-04 Bit::Vector::Minimal(3pm)
All times are GMT -4. The time now is 01:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy