Sponsored Content
Top Forums Programming Unexplained segmentation fault Post 302508093 by drl on Friday 25th of March 2011 05:50:55 PM
Old 03-25-2011
Hi.

Sometimes lint-like tools can catch these errors. For example, on Debian GNU/Linux, there is a utility splint. The output from running it on your code is:
Code:
% splint t1.c
Splint 3.1.2 --- 23 Aug 2008

t1.c:4:1: Function main declared to return void, should return int
  The function main does not match the expected type. (Use -maintype to inhibit
  warning)
t1.c: (in function main)
t1.c:13:16: Unallocated storage buf passed as out parameter to fgets: buf
  An rvalue is used that may not be initialized to a value on some execution
  path. (Use -usedef to inhibit warning)
t1.c:13:25: Possibly null storage fp passed as non-null param: fgets (..., fp)
  A possibly null pointer is passed as a parameter corresponding to a formal
  parameter with no /*@null@*/ annotation.  If NULL may be used for this
  parameter, add a /*@null@*/ annotation to the function parameter declaration.
  (Use -nullpass to inhibit warning)
   t1.c:11:8: Storage fp may become null
t1.c:16:23: Possibly null storage fp2 passed as non-null param:
               fputs (..., fp2)
   t1.c:12:9: Storage fp2 may become null
t1.c:7:8: Variable c declared but not used
  A variable is declared but never used. Use /*@unused@*/ in front of
  declaration to suppress message. (Use -varuse to inhibit warning)

Finished checking --- 5 code warnings

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Segmentation Fault

hello all, I tried a program on an array to intialise array elements from the standard input device.it is an integer array of 5 elements.but after entering the 4th element it throws a message called "Segmentation Fault" and returns to the command prompt without asking for the 5th element. ... (3 Replies)
Discussion started by: compbug
3 Replies

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

3. Programming

segmentation fault

ive written my code in C for implementation of a simple lexical analyser using singly linked list hence am making use of dynamic allocation,but when run in linux it gives a segmentation fault is it cause of the malloc function that ive made use of????any suggestions as to what i could do??? thank... (8 Replies)
Discussion started by: rockgal
8 Replies

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

5. Programming

Why not a segmentation fault??

Hi, Why I don't receive a segmentation fault in the following sample. int main(void) { char buff; sprintf(buff,"Hello world"); printf("%s\n",buff); } If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)
Discussion started by: lagigliaivan
22 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. 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

8. Homework & Coursework Questions

Segmentation Fault

this is a network programming code to run a rock paper scissors in a client and server. I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault. -the segmentation fault is fixed Current problem -Also... (3 Replies)
Discussion started by: femchi
3 Replies

9. Solaris

Segmentation fault

Hi Guys, I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error: "Segmentation fault" Can someone please help me resolve this? Thanks alot (2 Replies)
Discussion started by: cjashu
2 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
attributes(3pm) 					 Perl Programmers Reference Guide					   attributes(3pm)

NAME
attributes - get/set subroutine or variable attributes SYNOPSIS
sub foo : method ; my ($x,@y,%z) : Bent = 1; my $s = sub : method { ... }; use attributes (); # optional, to get subroutine declarations my @attrlist = attributes::get(&foo); use attributes 'get'; # import the attributes::get subroutine my @attrlist = get &foo; DESCRIPTION
Subroutine declarations and definitions may optionally have attribute lists associated with them. (Variable "my" declarations also may, but see the warning below.) Perl handles these declarations by passing some information about the call site and the thing being declared along with the attribute list to this module. In particular, the first example above is equivalent to the following: use attributes __PACKAGE__, &foo, 'method'; The second example in the synopsis does something equivalent to this: use attributes (); my ($x,@y,%z); attributes::->import(__PACKAGE__, $x, 'Bent'); attributes::->import(__PACKAGE__, @y, 'Bent'); attributes::->import(__PACKAGE__, \%z, 'Bent'); ($x,@y,%z) = 1; Yes, that's a lot of expansion. WARNING: attribute declarations for variables are still evolving. The semantics and interfaces of such declarations could change in future versions. They are present for purposes of experimentation with what the semantics ought to be. Do not rely on the current implementation of this feature. There are only a few attributes currently handled by Perl itself (or directly by this module, depending on how you look at it.) However, package-specific attributes are allowed by an extension mechanism. (See "Package-specific Attribute Handling" below.) The setting of subroutine attributes happens at compile time. Variable attributes in "our" declarations are also applied at compile time. However, "my" variables get their attributes applied at run-time. This means that you have to reach the run-time component of the "my" before those attributes will get applied. For example: my $x : Bent = 42 if 0; will neither assign 42 to $x nor will it apply the "Bent" attribute to the variable. An attempt to set an unrecognized attribute is a fatal error. (The error is trappable, but it still stops the compilation within that "eval".) Setting an attribute with a name that's all lowercase letters that's not a built-in attribute (such as "foo") will result in a warning with -w or "use warnings 'reserved'". What "import" does In the description it is mentioned that sub foo : method; is equivalent to use attributes __PACKAGE__, &foo, 'method'; As you might know this calls the "import" function of "attributes" at compile time with these parameters: 'attributes', the caller's package name, the reference to the code and 'method'. attributes->import( __PACKAGE__, &foo, 'method' ); So you want to know what "import" actually does? First of all "import" gets the type of the third parameter ('CODE' in this case). "attributes.pm" checks if there is a subroutine called "MODIFY_<reftype>_ATTRIBUTES" in the caller's namespace (here: 'main'). In this case a subroutine "MODIFY_CODE_ATTRIBUTES" is required. Then this method is called to check if you have used a "bad attribute". The subroutine call in this example would look like MODIFY_CODE_ATTRIBUTES( 'main', &foo, 'method' ); "MODIFY_<reftype>_ATTRIBUTES" has to return a list of all "bad attributes". If there are any bad attributes "import" croaks. (See "Package-specific Attribute Handling" below.) Built-in Attributes The following are the built-in attributes for subroutines: lvalue Indicates that the referenced subroutine is a valid lvalue and can be assigned to. The subroutine must return a modifiable value such as a scalar variable, as described in perlsub. This module allows one to set this attribute on a subroutine that is already defined. For Perl subroutines (XSUBs are fine), it may or may not do what you want, depending on the code inside the subroutine, with details subject to change in future Perl versions. You may run into problems with lvalue context not being propagated properly into the subroutine, or maybe even assertion failures. For this reason, a warning is emitted if warnings are enabled. In other words, you should only do this if you really know what you are doing. You have been warned. method Indicates that the referenced subroutine is a method. A subroutine so marked will not trigger the "Ambiguous call resolved as CORE::%s" warning. locked The "locked" attribute has no effect in 5.10.0 and later. It was used as part of the now-removed "Perl 5.005 threads". Available Subroutines The following subroutines are available for general use once this module has been loaded: get This routine expects a single parameter--a reference to a subroutine or variable. It returns a list of attributes, which may be empty. If passed invalid arguments, it uses die() (via Carp::croak) to raise a fatal exception. If it can find an appropriate package name for a class method lookup, it will include the results from a "FETCH_type_ATTRIBUTES" call in its return list, as described in "Package-specific Attribute Handling" below. Otherwise, only built-in attributes will be returned. reftype This routine expects a single parameter--a reference to a subroutine or variable. It returns the built-in type of the referenced variable, ignoring any package into which it might have been blessed. This can be useful for determining the type value which forms part of the method names described in "Package-specific Attribute Handling" below. Note that these routines are not exported by default. Package-specific Attribute Handling WARNING: the mechanisms described here are still experimental. Do not rely on the current implementation. In particular, there is no provision for applying package attributes to 'cloned' copies of subroutines used as closures. (See "Making References" in perlref for information on closures.) Package-specific attribute handling may change incompatibly in a future release. When an attribute list is present in a declaration, a check is made to see whether an attribute 'modify' handler is present in the appropriate package (or its @ISA inheritance tree). Similarly, when "attributes::get" is called on a valid reference, a check is made for an appropriate attribute 'fetch' handler. See "EXAMPLES" to see how the "appropriate package" determination works. The handler names are based on the underlying type of the variable being declared or of the reference passed. Because these attributes are associated with subroutine or variable declarations, this deliberately ignores any possibility of being blessed into some package. Thus, a subroutine declaration uses "CODE" as its type, and even a blessed hash reference uses "HASH" as its type. The class methods invoked for modifying and fetching are these: FETCH_type_ATTRIBUTES This method is called with two arguments: the relevant package name, and a reference to a variable or subroutine for which package- defined attributes are desired. The expected return value is a list of associated attributes. This list may be empty. MODIFY_type_ATTRIBUTES This method is called with two fixed arguments, followed by the list of attributes from the relevant declaration. The two fixed arguments are the relevant package name and a reference to the declared subroutine or variable. The expected return value is a list of attributes which were not recognized by this handler. Note that this allows for a derived class to delegate a call to its base class, and then only examine the attributes which the base class didn't already handle for it. The call to this method is currently made during the processing of the declaration. In particular, this means that a subroutine reference will probably be for an undefined subroutine, even if this declaration is actually part of the definition. Calling "attributes::get()" from within the scope of a null package declaration "package ;" for an unblessed variable reference will not provide any starting package name for the 'fetch' method lookup. Thus, this circumstance will not result in a method call for package- defined attributes. A named subroutine knows to which symbol table entry it belongs (or originally belonged), and it will use the corresponding package. An anonymous subroutine knows the package name into which it was compiled (unless it was also compiled with a null package declaration), and so it will use that package name. Syntax of Attribute Lists An attribute list is a sequence of attribute specifications, separated by whitespace or a colon (with optional whitespace). Each attribute specification is a simple name, optionally followed by a parenthesised parameter list. If such a parameter list is present, it is scanned past as for the rules for the "q()" operator. (See "Quote and Quote-like Operators" in perlop.) The parameter list is passed as it was found, however, and not as per "q()". Some examples of syntactically valid attribute lists: switch(10,foo(7,3)) : expensive Ugly('(") :Bad _5x5 lvalue method Some examples of syntactically invalid attribute lists (with annotation): switch(10,foo() # ()-string not balanced Ugly('(') # ()-string not balanced 5x5 # "5x5" not a valid identifier Y2::north # "Y2::north" not a simple identifier foo + bar # "+" neither a colon nor whitespace EXPORTS
Default exports None. Available exports The routines "get" and "reftype" are exportable. Export tags defined The ":ALL" tag will get all of the above exports. EXAMPLES
Here are some samples of syntactically valid declarations, with annotation as to how they resolve internally into "use attributes" invocations by perl. These examples are primarily useful to see how the "appropriate package" is found for the possible method lookups for package-defined attributes. 1. Code: package Canine; package Dog; my Canine $spot : Watchful ; Effect: use attributes (); attributes::->import(Canine => $spot, "Watchful"); 2. Code: package Felis; my $cat : Nervous; Effect: use attributes (); attributes::->import(Felis => $cat, "Nervous"); 3. Code: package X; sub foo : lvalue ; Effect: use attributes X => &foo, "lvalue"; 4. Code: package X; sub Y::x : lvalue { 1 } Effect: use attributes Y => &Y::x, "lvalue"; 5. Code: package X; sub foo { 1 } package Y; BEGIN { *bar = &X::foo; } package Z; sub Y::bar : lvalue ; Effect: use attributes X => &X::foo, "lvalue"; This last example is purely for purposes of completeness. You should not be trying to mess with the attributes of something in a package that's not your own. MORE EXAMPLES
1. sub MODIFY_CODE_ATTRIBUTES { my ($class,$code,@attrs) = @_; my $allowed = 'MyAttribute'; my @bad = grep { $_ ne $allowed } @attrs; return @bad; } sub foo : MyAttribute { print "foo "; } This example runs. At compile time "MODIFY_CODE_ATTRIBUTES" is called. In that subroutine, we check if any attribute is disallowed and we return a list of these "bad attributes". As we return an empty list, everything is fine. 2. sub MODIFY_CODE_ATTRIBUTES { my ($class,$code,@attrs) = @_; my $allowed = 'MyAttribute'; my @bad = grep{ $_ ne $allowed }@attrs; return @bad; } sub foo : MyAttribute Test { print "foo "; } This example is aborted at compile time as we use the attribute "Test" which isn't allowed. "MODIFY_CODE_ATTRIBUTES" returns a list that contains a single element ('Test'). SEE ALSO
"Private Variables via my()" in perlsub and "Subroutine Attributes" in perlsub for details on the basic declarations; "use" in perlfunc for details on the normal invocation mechanism. perl v5.16.2 2012-10-25 attributes(3pm)
All times are GMT -4. The time now is 07:56 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy