0

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

6
  • Please don't cast the return value of malloc() in C. Commented Oct 21, 2014 at 8:20
  • Ok. I removed the casting. Commented Oct 21, 2014 at 8:24
  • related: stackoverflow.com/questions/9333680/… Commented Oct 21, 2014 at 11:01
  • this line: hamming_string = (char *) malloc(max_length * sizeof(char)); only returns a ptr to some memory in the heap, it does not set that memory in the heap to any specific value, so the call to strlen() could return anything; Commented Oct 21, 2014 at 14:21
  • 1) there is no need to cast the return value from malloc() 2) sizeof(char) is always 1 so no need to have that as part of the parameter to malloc() Therefore, a better statement would be: hamming_string = malloc(max_length); Commented Oct 21, 2014 at 14:22

5 Answers 5

2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

I'm just looking to let the user specify the length of the hamming string by creating a character array of max_length size. I then hope to allow the user to input the string into the character array using scanf_s("%s", hamming_string);. However, I keep getting an access violation error. How do I go about fixing this?
I believe you are calling scanf_s() with incorrect parameters. Read the function description on MSDN: msdn.microsoft.com/en-us/library/w40768et.aspx
1

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.

Comments

1

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 */

1 Comment

Even with the change, when I print the string using %s, I see the proper number of a's along with a lot of weird looking symbols.
0
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.

Comments

0

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]

Comments

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.