The dangers of pointers

I am trying to learn C. Since I am already a little familiar with higher-level languages ​​(PHP, Javascript, Python), I feel that most of my work involves learning how to replace structures that I would give for granted (e.g. arrays with variable size) using pointers and manual memory management. My problem is that I'm a little worried about playing with pointers.

I usually try to experiment with other language functions, but my problem is that poor use of pointers can lead to unexpected results. In particular: is it possible, if I am mistaken, that I can damage the memory segments that are used by other programs, which makes these programs behave badly? Or does the operating system (in my case various Ubuntu variants) prevent me from integrating with the memory allocated for different processes?

In the first case, I assume that it would be possible (albeit unlikely) that I can force other programs to write bad data to disk, distorting some of the information that I have on the hard drive. Or even the worst (and, even more unlikely, I think), it can damage some hardware - for example, older monitors can be burned by software that would set the refresh rate out of range.

I know that probably my worries are not justified, but I would like to know how far the compiler / operating system prevents me from doing dangerous operations when I make mistakes while manipulating pointers.

+3
source share
4 answers

( Linux) , . , , , , - . , root ( root, - , .... ).

, , , :

  • (, malloc, ..) null .
  • .
  • free . . (, , , .)
+3

: , , , , ?

(?) , . Unix/Linux, Windows Mac OS . - , , . , , , , , ( ).

, , , , C , . C, , , !:)

, , . , , , , . wiki :

http://en.wikipedia.org/wiki/Buffer_overflow http://en.wikipedia.org/wiki/Memory_protection

+7

, , , , , , HARDCODE, .

, . , , - . ( , iOS - , , , . )

int *i = malloc(sizeof(int));
// Use I further.
free(i);

int *i;
i = malloc(sizeof(int));
double j;
i = &j;
j = 3.1415// Mem corruption due to differently sized values, given a double is LARGER than an int.

( , . .)

: 99%

int *i = 0x00ABCDEF; // Hard coded mem adress.
int j = 123;
int *k = &j;
memcpy(i, k, sizeof(int));
+4

( Linux) , , , , , " ", ( , RW, , ).

By the way, often, if you make mistakes with pointers, you find yourself with pointers pointing to invalid memory cells, and the first time you try to dereference them, the OS will close your application with a segmentation error; don't worry about it, it's just an operating system telling you that you are messing with pointers. :) The most serious problems, on the other hand, are those in which the OS cannot detect that you pointed some pointers incorrectly.

+2
source

Source: https://habr.com/ru/post/1785736/


All Articles