Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I am very happy with the whole site and would like to extend my compliments to all of you who work to make it one of the most useful sites (If not THE Most Useful) ...and the easiest to navigate..."

Geography

Where in the world do Tek-Tips members come from?

Really strange segmentation fault

carlg (Programmer)
26 Sep 06 21:26
Writing a C program, fedora core linux system.

Here is the variable declaration of my main program

CODE

        MYSQL_RES *result;
        MYSQL_RES *sresult;
        MYSQL_ROW record;
        MYSQL_ROW srecord;
        char *dbserver="XXXXXXXXXXX";
        char *dbuser="XXXXXXXX";
        char *dbpassword="XXXXXXXXXXX";
        char *db="XXXXXXXXX";

        int billrunnumber;
        struct ServiceCharge *ServiceCharges;
        int servicecount;
        struct ChargeCharge *ChargeCharges;
        int chargecount;
        struct BillingDisc billdisc;

        struct BillRun br;
        char brn_as_str[15];
        char cyc_as_str[15];

        char custquery[1000];
        int current_cust;


It's a pretty big program so I didn't post the entire thing, but when my variable declarations are like above, everything works fine.

If I try to declate a new variable anywhere it will give me a segmentation fault.

For exmaple, I can add

int myvar=0;

anywhere in the above code and it will give me segmentation fault.


What could be happening ?


Thanks for the help


Carl



cpjust (Programmer)
26 Sep 06 22:54
Are those global variables or in a function like main()?

Do you get the segmentation fault if you just increase the size of one of those arrays (without adding any new variables)?
sedj (Programmer)
27 Sep 06 2:58
[Stating the obvious I'm afraid]

In my experience, whenever a programme blows up when you seem to an an innocuous line of code (like you suggested), it means you are corrupting memory somewhere else in the programme - which is being hidden until you make some seemingly random addition, which ends up moving the stack and or heap around, which exposes the errors.

I would run the programme through a debugger like gdb or totalview, which I expect will highlight the true cause of the issue.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
http://www.primrose.org.uk

carlg (Programmer)
27 Sep 06 5:56
These variable are defined in the main function.

Salem (Programmer)
27 Sep 06 13:21
> What could be happening ?
As sedj says, the problem is elsewhere in your code, and you've just upset the delicate balance from success to failure.

Things to look out for include
- stepping off the ends of arrays (a couple of your arrays seem very short)
- not allocating memory at all (uninitialised pointers)
- not allocating enough memory
  int *p = malloc(10); does NOT reserve space for 10 integers on most machines.
- getting the type wrong
  double *p = malloc( 10 * sizeof(int) ); was OK until you changed int to double
- using memory after free
  free(p); p[0] = 0;
- freeing the same thing twice
- freeing something which wasn't malloc'ed
  int *p = malloc(sizeof(int)); p++; free(p);
- forgetting that realloc can MOVE the whole block.

If the problem is in dynamic memory, then perhaps you could use electric fence.
gcc -g prog.c -lefence
Running such a program inside the debugger should trap at the point where the mistake happens (as opposed to where it finally gets noticed later on).

Another option is valgrind, but that can slow down a program quite a bit.

--

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close