views:

124

answers:

2

Every time I run this I encounter a segmentation fault?

I have not found my fault as sometimes gcc emits a segmentation fault and sometimes the memory stack limit is exceeded.

This is the code.

#include <stdio.h>
#include <stdlib.h>
#include "labheader.h"

void my_main()
{             
    char *tutar[50],tempc;
    int i=0,temp,g=0,a=0,j=0,d=1,current=0,command=0,command2=0;

    do{
        tutar[i]=malloc(sizeof(int));
        scanf("%s",tutar[i]);
        temp=*tutar[i];
    } while(temp=='x'); 

    i=0;
    num_arrays=atoi(tutar[i]);
    i=1;
    tempc=*tutar[i];

    if(tempc!='x' && tempc!='d' && tempc!='a' && tempc!='j')
    {
        current=1; 

        arrays[current]=malloc(sizeof(int));
        l_arrays[current]=calloc(1,sizeof(int));
        c_arrays[current]=calloc(1,sizeof(int));
    }

    i=1; current=1;
    while(1)
    {  
        tempc=*tutar[i];

        if(tempc=='x')
            break;

        if(tempc=='n')
        {
            ++current;

            arrays[current]=malloc(sizeof(int));
            l_arrays[current]=calloc(1,sizeof(int));
            c_arrays[current]=calloc(1,sizeof(int));

            ++i;
            continue;
        }

        if(tempc=='d')
        {
            ++i;
            command=atoi(tutar[i])-1;
            free(arrays[command]);
            free(l_arrays[command]);  
            free(c_arrays[command]);
            ++i;
            continue;
        }


        if(tempc=='j')
        { 
            ++i;
            current=atoi(tutar[i])-1;

            if(arrays[current]==NULL)
            {
                arrays[current]=malloc(sizeof(int));
                l_arrays[current]=calloc(1,sizeof(int));
                c_arrays[current]=calloc(1,sizeof(int));

                ++i;
            }
            else
            {
                a=l_arrays[current];  
                j=l_arrays[current];
                ++i;
            }
            continue;
        }

    }
}
+5  A: 

Well, let's take a look at your code:

do{
    tutar[i]=malloc(sizeof(int));
    scanf("%s",tutar[i]);
    temp=*tutar[i];
} while(temp=='x'); 

This definitely leaks memory, and if you enter more than three characters at a time (on a typical system) it's going to write past the end of the buffer you provide. Why have you used sizeof(int) here? It seems to make no sense at all. It looks like you might have intended to read in multiple strings, but you never increment i, so each string you read overwrites the previous one (and in the process leaks the memory you allocated for it).

Edit: doing a bit more looking, there is a bit more than can be analyzed:

i=0;
num_arrays=atoi(tutar[i]);
i=1;
tempc=*tutar[i];

Since i wasn't incremented in the previous loop, only tutar[0] has been initialized, so when this tries to read from tutar[1], the result is undefined behavior.

[...end of edit.]

Unfortunately, the analysis I was going to post of the rest of the code seems to be close to impossible -- it starts to use variables you haven't defined. Worse, what you're trying to do with them isn't at all apparent, so I can't even guess at what they really are or what you should be doing with them.

Jerry Coffin
+3  A: 

Well, starting with the first two statements,

  tutar[i]=malloc(sizeof(int));
  scanf("%s",tutar[i]);

is nonsense. You allocated an int and put a string in it. Depending on your platform, this may crash for a string of 2, 4, or 8 characters.

GCC does not emit a segmentation fault, your program does. Try running it in GDB to find out the line number of the crash, and debugging.

Potatoswatter
I agree with all but one part of what you've said: I think trying to sort out what this code is doing is kind of a waste of time. Trying to fix this will take more time than starting over with something that makes sense. +1 anyway though...
Jerry Coffin
@Jerry: Well, if he has a job to do, he's gotta do it… you did him a huge favor fixing the indentation, very cool.
Potatoswatter
@Potatoswatter: The question is, what's the easiest way to do the job? Starting over *may* not be easier (hard to say when you don't know what it's supposed to do) but it looks to me like it probably is.
Jerry Coffin