Like Tree1Likes
  • 1 Post By grumpy

Segmentation Fault -> Main not initializing

This is a discussion on Segmentation Fault -> Main not initializing within the C Programming forums, part of the General Programming Boards category; This program is compiling just fine but is not running. Even, Main is not initializing, Here is my code Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    12

    Segmentation Fault -> Main not initializing

    This program is compiling just fine but is not running. Even, Main is not initializing,

    Here is my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int pow2(int n) {
        printf("POW2");
        int pow = 1;    
        while(n <= pow) {        
            if (n < pow) {
                return 0;
            }
            else if(n == pow) {
                return 1;
            }
            else pow = pow * 2;
        }
        return 0;
    }
    
    int average(int c[], int len) {
        printf("average");
        int *firstHalf = c;
        int *secondHalf = c + (len/2);
        /*if((len%2) != 0) {    
            int *firstHalf = c;
            int *secondHalf = c + (len/2);
            return (average(firstHalf, (len/2)) + average(secondHalf,(len/2)))/2);
        }*/
    
        return (average(firstHalf, (len/2))+average(secondHalf,(len/2))/2);
    }
    
    /* ********************************************************* MAIN */
    int main(int argc, char **argv) { printf ("MAIN"); 
        int c[100];
        int size;    
        int i;    
        int sum;
        float avg;
        i = 0;
        size = 0;
        sum = 0;
        avg = 0;
        
        /* LER OPERAÇÃO */
        printf ("antes while");
        char currentOp[1]; printf ("antes while");
        while(strcmp(argv[i],"\0") != 0) {printf ("durante while");
            i++;
            strcpy(currentOp,argv[i]);        
            if(currentOp[0] == 'a') { /* ADD */
                i++;
                c[size] = atoi(argv[i]);
                size++;
                sum += c[size];
                if(pow2(sum) == 1) {
                    avg = average(c,size);
                    printf("%.3f\n", avg);
                }
            }
            else if(currentOp[0] == 'r') { /* REMOVE */
                int j;
                int y;
                j = 0;
                y = 0;
                i++;
                int current = atoi(argv[i]);            
                for (j=0; j<size; j++) {                                
                    if (c[j] == current) {
                        for(y=j; y<size; y++) {                    
                            c[y] = c[y+1];                        
                        }
                        sum = sum - current;
                        size--;
                    }    
                }
                if(pow2(sum) == 1) {
                    avg = average(c,size);
                    printf("%.3f\n", avg);
                }
            }
            else { /* REPLACE */
                int old;
                int new;
                int k;    
                k = 0;        
                i++;
                old = atoi(argv[i]);
                i++;
                new = atoi(argv[i]);
                if (new != old)    {
                    for (k=0; k<size; k++) {                    
                        if(c[k] == old) {
                            c[k] = new;
                            sum = sum - old + new;    
                        }
                    }
                }
                if (pow2(sum) == 1) {
                    avg = average(c,size);
                    printf("%.3f\n", avg);
                }
            }
        }
        return 0;
    }
    Thanks for your help.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,306
    There is nothing in the code you've shown that will stop the program starting.

    The problem causing a crash is in line 50. The first time through the enclosing while loop will attempt to copy argv[0] (a string containing the program name) into an array with exactly one character. That will cause a buffer overrun - and often a program crash - unless argv[0] is a string of length zero - which is possible according to the C standard, but rarely true with real-world compilers.

    More generally, however, the command line arguments are an array of pointers to char. The last one (argv[argc]) is a NULL pointer. It should not be passed to strcmp() at all, as the result is undefined behaviour (of which one symptom is a program crash).

    In other words, do NOT use strcmp() to check for the last command line argument.

    If you're not seeing output before the program crashes, the reason is probably that stdout (the standard output stream) is buffered, and your code is doing nothing to flush pending output before the crash occurs. Either include a '\n' in output to stdout (which typically cases a pending output to be flushed) or periodically called fflush(stdout).
    Dude22 likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    12
    Thank You, you solved my problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-07-2009, 08:49 AM
  2. Segmentation Fault in main()
    By sa125 in forum C Programming
    Replies: 5
    Last Post: 07-27-2008, 06:20 AM
  3. Segmentation faults when initializing large arrays
    By MathFan in forum C++ Programming
    Replies: 5
    Last Post: 07-14-2008, 05:24 AM
  4. segmentation fault before main
    By Cristian Negresco in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2002, 09:14 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21