Question

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.

Était-ce utile?

La solution 3

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!

Autres conseils

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.

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)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top