Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Really strange segmentation fault

Status
Not open for further replies.

carlg

Programmer
Jun 23, 2004
88
0
0
US
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



 
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)?
 
[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
 
These variable are defined in the main function.

 
> 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
[tt] int *p = malloc(10);[/tt] does NOT reserve space for 10 integers on most machines.
- getting the type wrong
[tt] double *p = malloc( 10 * sizeof(int) );[/tt] was OK until you changed int to double
- using memory after free
[tt] free(p); p[0] = 0;[/tt]
- freeing the same thing twice
- freeing something which wasn't malloc'ed
[tt] int *p = malloc(sizeof(int)); p++; free(p);[/tt]
- forgetting that realloc can MOVE the whole block.

If the problem is in dynamic memory, then perhaps you could use electric fence.
[tt]gcc -g prog.c -lefence[/tt]
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.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top