Home c Why does Segmentation Fault error occur?

Why does Segmentation Fault error occur?

Author

Date

Category

I want to dynamically highlight the memory and write there a certain buffer on the sign, in this case the string:

# include & lt; stdlib.h & gt;
# INCLUDE & LT; stdio.h & gt;
INT MAIN ()
{
  char * a = (char *) malloc (6 * SizeOF (char));
  char * str = "Hello";
  * a = * str;
  PrintF ("% s \ n", * a);
  FREE (A);
Return 0;
}

But the compiler gives an error. What am I wrong? How to use dynamically allocated memory? Strcpy ?
Error occurs when using printf .

I have another program, but there is another compiler, not GCC. This code also occurs error:

char * array = (char *) malloc (6 * Sizeof (Char));
Array = "Hello";

Answer 1, Authority 100%

In this program

# include & lt; stdlib.h & gt;
# INCLUDE & LT; stdio.h & gt;
INT MAIN ()
{
  char * a = (char *) malloc (6 * SizeOF (char));
  char * str = "Hello";
  * a = * str;
  PrintF ("% s \ n", * a);
  FREE (A);
Return 0;
}

You first dynamically highlight the memory under the symbol array

char * a = (char *) malloc (6 * SizeOF (CHAR));

Consequently, the a pointer now contains the address of the first byte of this memory site.

Then you assign the first Symbol of the string literal "Hello" The first byte of memory addressed to the a

pointer

* a = * str;

Now you have in allocated memory

'H' and some "trash"

Since the remaining bytes of the allocated memory were not initialized.

In the printf function, you use the % S format specifier, which suggests that the corresponding argument is a string, that is, a symbolic array that has the final zero symbol ' 0 '.

However, your string does not contain the final zero. Therefore, this function is trying to display all the characters on the console, even the limit of the memory selected by you, until you encounter the completion symbol '\ 0' .

As a result, you get a memory segmentation error.

Moreover, you specified as an argument this first string symbol instead of the address

printf ("% s \ n", * a);
        ^^^

must be at least

printf ("% s \ n", a);
        ^^

As for this fragment of the code

char * array = (char *) malloc (6 * Sizeof (Char));
Array = "Hello";

That first, there is a “leakage” of memory. You first allocated dynamically memory and its address assigned the Array pointer. And then this pointer was assigned the address of the string literal (its first symbol). As a result, the address of the allocated dynamically memory was lost.

For this code fragment causes a mistake of most likely, the fact that you tried to free up the memory using the Free function by writing

free (array);

In this case, the function will try to remove the static memory occupied by a string literal, as at the moment Array indicates a string literal. However, it is impossible to remove static memory using the Free function, since this memory was not allocated dynamically. It was reserved by the compiler even during compilation when he met a string literal in your program. This is the memory that is released by the system after the program is completed.

It would be right to write

# include & lt; string.h & gt;
// ...
Char * Array = (Char *) Malloc (6 * SizeOF (Char)); 
STRCPY (Array, "Hello");
// ...
FREE (Array);

and in the first program instead of

* a = * str;

You should write

strcpy (a, str);

Pre-enabled the header & lt; String.h & gt;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions