0

I've been trying to figure out what I'm doing wrong in this block of code for a while now and still no luck. According to GDB, I'm receiving a SIGSEV, Segmentation Fault which means that I tried to access an invalid memory address.

Here I'm trying to find the length of the char pointer that is being passed

int getLength(char *start) {
 int count = 0;
 while(*start) {
   count++;
   start++;
 }
 return count;
}

Here is my full C file. I think it might be useful

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

  int main(int argc, char *argv[]) {
    int len1 = getLength(argv[1]);
    int len2 = getLength(argv[2]);  

    if (argc != 3) {
     fprintf(stderr, "Invalid number of items input.\n");
     return 1;
    }

    if (len1!=len2) {
     fprintf(stderr,"From and to are not the same size.\n");
     return 1;
    }
    return 0;   
 }

 int getLength(char *start) {
    int count = 0;
    while(*start) {
     count++;
     start++;
    }
   return count;
 }

 int duplicatesFrom(char *from) {
   int i;
   int j;

   int len = getLength(from); 

   for(i=0;i<len;i++) {
    for(j=0;j<len;j++) {
      if (from[i]==from[j] && i!=j) {
        return 1;
      }
    }
   }
   return 0;
}
6
  • 3
    Show us how you allocate and initialize start Commented Nov 10, 2015 at 20:44
  • ...and how you call getLength. Commented Nov 10, 2015 at 20:47
  • You must own the memory at start, up to and including the null terminator. Also, it would be nice to see const char* as the parameter type. Commented Nov 10, 2015 at 20:50
  • Just updated post guys. @EdS Commented Nov 10, 2015 at 20:53
  • You are checking argc after using getLength on argv. Commented Nov 10, 2015 at 20:57

1 Answer 1

1

First, if you don't give at least 3 arguments to your program, it will crash since you call getLength with the second and third argument as parameters at the beginning of main().

I suggest you move the two first lines below the argument count check.

Then, as suggested by BathSheba, you shoud consider making the char * passed to getLength() const since getLength() doesn't modify it. It is a good practice and make the code more self-explanatory.

Also, do not forget the forward declaration of the functions you use in main() if their definition are below main(), unless you put the forward declaration in a header file (neither are present in your code snippet).

On last thing : in getLength(), you should check if the char * passed is valid by doing something like if (start){...}. Because if start == NULL, your program will crash since you will try to access a null pointer.

Following your edit : you can not separate an if() statement and the else if() logically following it. The else if() can be changed to if() here since in the case you enter the first if(), you quit the program (return 1;). And if you don't (if there is 3 arguments passed to the program), you can check safely if argv[1] and argv[2] have the same length.

Your main, slightly modified :

int main(int argc, char *argv[]) {

    if (argc != 3) {
        fprintf(stderr, "Invalid number of items input.\n");
        return 1;
    }

    int len1 = getLength(argv[1]);
    int len2 = getLength(argv[2]);

    if (len1 != len2) {
        fprintf(stderr, "%s and %s are not the same size.\n", argv[1], argv[2]);
        return 1;
    }
    else {
        printf("%s", "Hello\n");
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Oh shoot, I thought I moved that. Probably forgot or something...now I'm getting an else without a previous if error
Okay, cool. Thanks for the explanation. Really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.