SegFault in little arrays, I can't find the bad call...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sitko
    New Member
    • Sep 2007
    • 140

    SegFault in little arrays, I can't find the bad call...

    Sometimes it just takes another's unbiased eyes to see a problem in code:
    The arrays are set up as: "int gintCasePart[30];" and "gstrCasePa rt[19,40];"
    Here are two code snipets I added to the previously working code (hint: it works on Windows, but not Linux):

    Code:
    ...
        gintCasePart[0] = 1;
    	gintCasePart[1] = 2;
    	gintCasePart[2] = 3;
    	gintCasePart[3] = 4;
    	gintCasePart[4] = 5;
    	gintCasePart[5] = 6;
    
    	if (gblnAngledFlaps == 1)
    	{
    		gintCasePart[6] = 7;
    		gintCasePart[7] = 8;
    		gintCasePart[8] = 9;
    		if (gblnInsertX == 1)
    		{
    			gintCasePart[9] = 10;
    		}
    	}
    	else
    	{
    		gintCasePart[6] = 9;
    		if (gblnInsertX == 1)
    		{
    			gintCasePart[7] = 10;
    		}
    	}
    and (and I know some of the below is redundant, but it works on the windows side, just not the Linux side...
    Code:
    if (gblnAngledFlaps==0)
    	{
    		strcpy(gstrCasePart[6],"Cover");
    		gintCasePart[6]=9;
    		gintPartsCount = 7;
    		if (gblnInsertX==1)
    		{
    			strcpy(gstrCasePart[7],"Insert");
    			gintCasePart[7]=10;
    			gintPartsCount = gintPartsCount + 1;
    		}
    	}
    	else
    	{
    		strcpy(gstrCasePart[6],"Inside, Angled Flaps +X+Y, -X-Y");
    		gintCasePart[6]=7;
    		strcpy(gstrCasePart[7],"Inside, Angled Flaps +X-Y, -X+Y");
    		gintCasePart[6]=8;
    		strcpy(gstrCasePart[8],"Cover");
    		gintCasePart[6]=9;
    		gintPartsCount = 9;
    		if (gblnInsertX==1)
    		{
    			strcpy(gstrCasePart[9],"Insert");
    			gintCasePart[6]=10;
    			gintPartsCount = gintPartsCount + 1;
    		}
    	}
    I also added a debug print statement to attempt to see what was going on in my arrays:
    Code:
    void check_array(char * func, char * area)
    {
    	int x;
    	x = 0;
    	printf("In function: %s, In Area: %s",func,area);
    
    	for (x=0;x<=11;x++)
    	{
    		printf("intCasePart = %d, gstrCasePart = %s",gintCasePart[x],gstrCasePart[x]);
    	}
    }
    I appreicate any help you can offer, I've been staring at this for nearly 12 hours, and can find no solutions...I'm sure I've just been looking at it too long, without getting a fresh perspective....

    Thanks,
    Sitko.
  • sitko
    New Member
    • Sep 2007
    • 140

    #2
    After reading through more of the SegFault posts, I see a common theme, check your arrays....I've done that, to head you off at the pass.

    If I set the arrays to values Greater than the number of elements I'd ever need, should that cover those situations?

    I also initialize both arrays with values, 0 for the intArrray, and either the name of the part "Cover", etc, or "" null for the strArray.

    Thanks,
    Sitko.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Probably it's your strArray elements that cause the problem:
      Originally posted by sitko
      strcpy(gstrCase Part[6],"Inside, Angled Flaps +X+Y, -X-Y");
      How are you initializing gstrCasePart[6] ?

      Like, did you allocate memory:
      [code=c]
      gstrCasePart[6] = (char*)malloc(( strlen(,"Inside , Angled Flaps +X+Y, -X-Y") + 1) * sizeof(char));
      [/code]

      Comment

      • sitko
        New Member
        • Sep 2007
        • 140

        #4
        Originally posted by weaknessforcats
        Probably it's your strArray elements that cause the problem:


        How are you initializing gstrCasePart[6] ?

        Like, did you allocate memory:
        [code=c]
        gstrCasePart[6] = (char*)malloc(( strlen(,"Inside , Angled Flaps +X+Y, -X-Y") + 1) * sizeof(char));
        [/code]
        No, I used
        Code:
        "char gstrCasePart[19][40] = {{"Outside, Long Sides"},{"Outside, Bottom"}, {"Outside, Short Flaps"},{"Outside, Long Flaps"}, {"Inside, Ends"}, 
        {"Inside, Side Flaps"},{"Inside, Angled Flaps +X+Y, -X-Y"},
        {"Inside, Angled Flaps +X-Y, -X+Y"},{"Cover"},{""} //,{""},{""},{""},{""},{""},{""}
        };
        To create it. I set it up as a double dimensioned array, where the second dimension would be the chars. And 40 being plenty to contain the largest string of "Inside, Angled Flaps +X-Y, -X+Y"

        Is that not a kosher way of doing it?

        Thanks,
        Sitko.

        Comment

        • sitko
          New Member
          • Sep 2007
          • 140

          #5
          All,

          I found the problem.

          It wasn't anything to do with my arrays, although there was one error in there....see if you can find it, and if not, I'll post it later, think of it as a puzzle.

          The problem was that I had commented and uncommented out some error checking printf's lower in the code (not presented), and when I was un-commenting them, I by accident un-commented out a bit of code that shouldn't of been un-commented...so basically, I was attempting to call a function that had its declaration commented out, AND its code commented out. But, not the call to it. This leads me to the question, why would this be grouped as a SegFault error, and not an 'un-declared' function, or some such?

          Thanks,
          Sitko.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by sitko
            It wasn't anything to do with my arrays, although there was one error in there....see if you can find it, and if not, I'll post it later, think of it as a puzzle.
            You didn't allocate memory. What you did was assign the address of a constant string to your array elements. Any attempt to copy to that location will crash the program.

            Originally posted by sitko
            so basically, I was attempting to call a function that had its declaration commented out, AND its code commented out. But, not the call to it. This leads me to the question, why would this be grouped as a SegFault error, and not an 'un-declared' function, or some such?
            It should cause a build error.

            Comment

            • sitko
              New Member
              • Sep 2007
              • 140

              #7
              Originally posted by weaknessforcats
              You didn't allocate memory. What you did was assign the address of a constant string to your array elements. Any attempt to copy to that location will crash the program.



              It should cause a build error.
              thats what I thought as well. In Windows (vb.net) it caused a Linking error...but in Unix, all I got was the segFault.

              Thanks.

              Comment

              Working...