-1

I keep getting this error trying to run the debugger:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x00007fff8c2414f0 in strlen ()

Here is my code:

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    char s2[25];
    strcpy(s2, argv[1]);
    int keyLen = strlen(s2);
    printf("Please enter a string of text to be encrypted!\n");
    string p = GetString();
    for (int i = 0, n = strlen(p); i < n; i++)
    {
        if (isupper(p[i])){
        int sum = (p[i] - 'A') + (s2[i % keyLen] - 'A');
        char c = 'A' + sum%26;
        printf("%c", c);
        }
    }
    printf("\n");
    printf("%d\n", keyLen);

}

I can compile the code with no errors and it works like it should. I am running the debugger to step into the for loop and look at what the math is doing to better understand it.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Yamaha32088
  • 4,125
  • 9
  • 46
  • 97

3 Answers3

1

If GetString() returns null, then calling strlen(null) will give this error. Other errors on the part of GetString() could be causing this as well.

mfsiega
  • 2,852
  • 19
  • 22
1

What is type 'string' in C? Strlen() expects C-type array and not some custom 'string' type. (+ there is a possibility of null input as pointed above)

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
0

I finally got it thanks to hmjd I was running it incorrectly I would start the program

gdb vignere HHHHH

Which is incorrect I ran it

gdb vignere
run HHHHHH

and it worked perfect!

Yamaha32088
  • 4,125
  • 9
  • 46
  • 97