Question

So I have two problems:

I'm using netbeans to code this.

The first is that the array value that I am setting in c.sArr is getting changed from 7 to some random number, and I can't figure out why.

The second is that when I try to run debug in netbeans, the code gives me a segfault, whereas when i run it normally it doesn't. It gives a segfault at the atoi function.

Whats going on here?

#include <stdio.h>
#include <stdlib.h>
#include "spoonMatrix.c"

int main(int argc, char** argv) {
    int iterations;
    int argCounter = 0;
    int debug = 1;
    int i,j,q;

    if(argc < 2)
        return -1;

    if(debug == 1){
        for(q=0;q<argc;q++)
            printf("%s\n", argv[argCounter++]);                                     //Checking the params
    }

    argCounter = 1;
    iterations = atoi(argv[argCounter++]);

    if(debug == 1)
        printf("%d", iterations);

    for(i=0;i<iterations;i++){
        int rows = 0;
        int columns = 0;
        int m = 0, n, p, elemCount;
        int posCount = 0;
        int temp; 
        cm c;
        c.row = rows;
        c.column = columns;
        c.elems  = (char*)calloc(rows*columns, sizeof(char));
        c.sArr   = (int*)calloc(rows*columns, sizeof(int));

        rows = atoi(argv[argCounter++]);
        columns = atoi(argv[argCounter++]);

        for(m=0;m<rows*columns;m++)
        {
            c.sArr[m] = -2;
            //printf("Here");
        }

        if(debug == 1)
        {
            printf("Rows : Columns - %d : %d\n", rows, columns);
        }

        temp = argCounter;
        printf("argCounter is: %d\n", argCounter);
        for(elemCount = 0 ; argCounter < temp + rows; argCounter++)
        {
            for(n=0; n<columns; n++, elemCount++)
            {
                c.elems[elemCount] = argv[argCounter][n];
                //if(debug == 1)
                //  printf("%c\t", c.elems[elemCount]);
                if(c.elems[elemCount]== 's' || c.elems[elemCount] == 'S')
                {
                    c.sArr[posCount] = elemCount;
                    printf("%c\t%d\t%d\t%d\n", c.elems[elemCount], elemCount, c.sArr[posCount++], posCount);

                }
            }
        }

        printf("%d\n", c.sArr[0]);
        if(debug == 1)
        {
            for(j=0; j<rows*columns; j++)
            {
                printf("%c ", c.elems[j]);
            }

            printf("\n");

            for(j=0;j<rows*columns;j++)
            {
                printf("%d ", c.sArr[j]);
            }
        }
    }

    return (EXIT_SUCCESS);
}

and

the other file is:

struct charMat{
    int row;
    int column;
    char* elems;
    int* sArr;
};

typedef struct charMat cm;

Coded in the hurry, excuse the weird debugging statements.

Thanks

Was it helpful?

Solution

You aren't allocating (enough) memory:

int rows = 0;
int columns = 0;

c.elems = (char*)calloc(rows*columns, sizeof(char)); // rows * columns is 0
c.sArr = (int*)calloc(rows*columns, sizeof(int)); // rows * columns is 0

rows = atoi(argv[argCounter++]);
columns = atoi(argv[argCounter++]);

From calloc:

If the size of the space requested is 0, the behavior is implementation-defined: the value returned shall be either a null pointer or a unique pointer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top