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

mylasthope
  • 89
  • 7
  • [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – unwind Oct 21 '14 at 08:20
  • Ok. I removed the casting. – mylasthope Oct 21 '14 at 08:24
  • related: https://stackoverflow.com/questions/9333680/allocating-memory-using-malloc-and-find-the-strlen/9333707#9333707 – Jens Gustedt Oct 21 '14 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; – user3629249 Oct 21 '14 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); – user3629249 Oct 21 '14 at 14:22
  • the malloc() function can fail, so the returned value should always be checked before using it. – user3629249 Oct 21 '14 at 14:23

5 Answers5

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

pmg
  • 106,608
  • 13
  • 126
  • 198
  • 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? – mylasthope Oct 21 '14 at 08:21
  • I believe you are calling `scanf_s()` with incorrect parameters. Read the function description on MSDN: http://msdn.microsoft.com/en-us/library/w40768et.aspx – pmg Oct 21 '14 at 08:23
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 */
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 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. – mylasthope Oct 21 '14 at 08:19
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.

Jackson
  • 5,627
  • 2
  • 29
  • 48
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.

pmg
  • 106,608
  • 13
  • 126
  • 198
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]
Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27