Sponsored Content
Full Discussion: segmentation fault
Top Forums Programming segmentation fault Post 302435210 by Corona688 on Tuesday 6th of July 2010 05:58:21 PM
Old 07-06-2010
Yes, local variables like that and pointers to them don't remain valid after the function call they exist inside returns. That it works at all is simply a coincidence. Make a few more function calls and the memory pointed to will probably be overwritten with garbage.

The compiler is probably warning you about this mistake, but as a warning, not an error.

You should pass a pointer to that function instead of getting one from it. Don't let it give you its local variables, give yours to it; perfectly valid when done before your function returns.
Code:
void fn(int *val)
{
       (*val)=3;
}

int main(void)
{
        int n;

        fn(&n);

        printf("n=%d\n", n);

        return(0);
}

You could also use a static variable, like:

Code:
int *fn(void)
{
       static int n;

        n=3;
        return(&n);
}

But this has several caveats. Static variables act like global variables; it will be pointing to the same pointer every time, so you can't keep the pointer around and expect the value to stay the same when other things use it. Also, this isn't thread-safe since multiple threads would be competing for the same variable. Probably best to pass a pointer.

Last edited by Corona688; 07-06-2010 at 07:04 PM..
This User Gave Thanks to Corona688 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. UNIX for Dummies Questions & Answers

Segmentation Fault

Hi, While comparing primary key data of two tables thr bteq script I am getting this Error. This script is a shell script. *** Error: The following error was encountered on the output file. Script.sh: 3043492 Segmentation fault(coredump) Please let me know how to get through it. ... (5 Replies)
Discussion started by: monika
5 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. Programming

Segmentation fault

I keep getting this fault on a lot of the codes I write, I'm not exactly sure why so I'd really appreciate it if someone could explain the idea to me. For example this code #include <stdio.h> main() { unsigned long a=0; unsigned long b=0; int z; { printf("Enter two... (2 Replies)
Discussion started by: sizzler786
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
g_spatial(1)					 GROMACS suite, VERSION 4.5.4-dev-20110404-bc5695c				      g_spatial(1)

NAME
g_spatial - calculates the spatial distribution function VERSION 4.5.4-dev-20110404-bc5695c SYNOPSIS
g_spatial -s topol.tpr -f traj.xtc -n index.ndx -[no]h -[no]version -nice int -b time -e time -dt time -[no]w -[no]pbc -[no]div -ign int -bin real -nab int DESCRIPTION
g_spatial calculates the spatial distribution function and outputs it in a form that can be read by VMD as Gaussian98 cube format. This was developed from template.c (GROMACS-3.3). For a system of 32,000 atoms and a 50 ns trajectory, the SDF can be generated in about 30 minutes, with most of the time dedicated to the two runs through trjconv that are required to center everything properly. This also takes a whole bunch of space (3 copies of the .xtc file). Still, the pictures are pretty and very informative when the fitted selection is properly made. 3-4 atoms in a widely mobile group (like a free amino acid in solution) works well, or select the protein backbone in a stable folded structure to get the SDF of solvent and look at the time-averaged solvation shell. It is also possible using this program to generate the SDF based on some arbitrary Cartesian coordinate. To do that, simply omit the preliminary trjconv steps. USAGE: 1. Use make_ndx to create a group containing the atoms around which you want the SDF 2. trjconv -s a.tpr -f a.xtc -o b.xtc -center tric -ur compact -pbc none 3. trjconv -s a.tpr -f b.xtc -o c.xtc -fit rot+trans 4. run g_spatial on the .xtc output of step 3. 5. Load grid.cube into VMD and view as an isosurface. Note that systems such as micelles will require trjconv -pbc cluster between steps 1 and 2 WARNINGS: The SDF will be generated for a cube that contains all bins that have some non-zero occupancy. However, the preparatory -fit rot+trans option to trjconv implies that your system will be rotating and translating in space (in order that the selected group does not). There- fore the values that are returned will only be valid for some region around your central group/coordinate that has full overlap with system volume throughout the entire translated/rotated system over the course of the trajectory. It is up to the user to ensure that this is the case. BUGS: When the allocated memory is not large enough, a segmentation fault may occur. This is usually detected and the program is halted prior to the fault while displaying a warning message suggesting the use of the -nab (Number of Additional Bins) option. However, the program does not detect all such events. If you encounter a segmentation fault, run it again with an increased -nab value. RISKY OPTIONS: To reduce the amount of space and time required, you can output only the coords that are going to be used in the first and subsequent run through trjconv. However, be sure to set the -nab option to a sufficiently high value since memory is allocated for cube bins based on the initial coordinates and the -nab option value. FILES
-s topol.tpr Input Structure+mass(db): tpr tpb tpa gro g96 pdb -f traj.xtc Input Trajectory: xtc trr trj gro g96 pdb cpt -n index.ndx Input, Opt. Index file OTHER OPTIONS
-[no]hno Print help info and quit -[no]versionno Print version info and quit -nice int 0 Set the nicelevel -b time 0 First frame (ps) to read from trajectory -e time 0 Last frame (ps) to read from trajectory -dt time 0 Only use frame when t MOD dt = first time (ps) -[no]wno View output .xvg, .xpm, .eps and .pdb files -[no]pbcno Use periodic boundary conditions for computing distances -[no]divyes Calculate and apply the divisor for bin occupancies based on atoms/minimal cube size. Set as TRUE for visualization and as FALSE ( -nodiv) to get accurate counts per frame -ign int -1 Do not display this number of outer cubes (positive values may reduce boundary speckles; -1 ensures outer surface is visible) -bin real 0.05 Width of the bins in nm -nab int 4 Number of additional bins to ensure proper memory allocation SEE ALSO
gromacs(7) More information about GROMACS is available at <http://www.gromacs.org/>. Mon 4 Apr 2011 g_spatial(1)
All times are GMT -4. The time now is 10:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy