简体   繁体   中英

C - Malloc char array access violation error

I'm not sure why malloc is allocating so much space. Here's a snippet of the problem code:

char * hamming_string = NULL;

void enter_params(){
printf("Enter the max length: ");


scanf_s("%d", &max_length);


hamming_string = (char *) malloc(max_length * sizeof(char));

     // to test what's going on with the hamming string
     for(int i = 0; i < strlen(hamming_string); i++){
          hamming_string[i] = 'a';
     }

     printf("hamming string = %s", hamming_string);
}

I set max_length to 2 and I'm seeing 12 a's. In another function, I was going to have the user input the hamming string using scanf_s("%s", &hamming_string); but I kept getting a access violation

hamming_string is not a string until one of its elements is a '\\0' .

The str*() functions can only be used on strings.

Your program invokes Undefined Behaviour (by calling strlen() with something that is not a string).

You are asking for the strlen of an uninitialized variable (this is undefined behaviour):

strlen(hamming_string);

(m)allocate one more in order to store the trailling \\0 :

hamming_string = malloc(max_length + 1);

change to

 for(int i = 0; i < max_length; i++){
      hamming_string[i] = 'a';
 }

and don't forget to add the trailling \\0 after the for loop:

hamming_string[i] = '\0'; /* or use calloc and skip this line */

malloc() allocates the amount of space that you ask for but it does not initialise it. When you call strlen() it scans the memory starting at what hamming_string points to and continues until it finds a null or it accesses memeory that it shouldn't and causes an exception.

In addition you need to allocate space for the null at the end of the string, if you want a string to hold 2 characters you need to allocate 3 characters to allow for the terminating null.

void check_code(){
    int actual_length, parity_bit, error_bit = 0, c = 0, i, j, k;
    printf("Enter the Hamming code: ");
    scanf_s("%s", &hamming_string);
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This scanf_s() call is incorrect.

According to the C11 documentation or MSDN documentation it needs to be

scanf_s("%s", hamming_string, size - 1);

Note that you don't know size inside the function.
Note that you don't pass the address of hamming_string ; hamming_string by itself gets converted to the address of its first element.

Example1:

char *hamming_string = malloc((max_length + 1) * sizeof(char));

for (i = 0; i < max_length; i++)
{
  hamming_string[i] = 'a';
}
hamming_string[i] = '\0';

printf("hamming string = [%s]\n", hamming_string);

Output:

sdlcb@Goofy-Gen:~/AMD$ ./a.out
hamming string = [aaaaaaaaaaaa]

Example2:

char s;
for (i = 0; i < max_length; i++)
{
  scanf(" %c", &s);
  hamming_string[i] = s;
}
hamming_string[i] = '\0';

printf("hamming string = [%s]\n", hamming_string);

Output:

sdlcb@Goofy-Gen:~/AMD$ ./a.out
a
b
c
d
e
f
g
h
i
j
k
l
hamming string = [abcdefghijkl]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM