Читаем Linux программирование в примерах полностью

16 /* main - вызов setjmp, действия с переменными, вывод значений */

17

18 int main(void)

19 {

20  int i = 5;

21  volatile int j = 6;

22

23  if (setjmp(env) == 0) { /* первый раз */

24   i++;

25   j++;

26   printf("first time: i = %d, j = %d\n", i, j);

27    comeback));

28  } else /* второй раз */

29   printf("second time: i = %d, j = %d\n", i, j);

30

31  return 0;

32 }

В этом примере сохранение своего значения ко второму вызову printf() гарантируется лишь j (строка 21). Значение (строка 20) в соответствии со стандартом С 1999 г. не определено. Это может быть 6, может быть 5, а может даже какое-нибудь другое значение!

В-четвертых, как описано в разделе 12.5.2 «Обработка масок сигналов: sigsetjmp() и siglongjmp()», стандарт С 1999 г. не делает никаких утверждений о влиянии, если оно есть, setjmp() и longjmp() на состояние сигналов программы. Если это важно, вам придется вместо них использовать sigsetjmp() и siglongjmp().

В-пятых, эти процедуры содержат поразительные возможности для утечек памяти! Рассмотрим программу, в которой main() вызывает setjmp(), а затем вызывает несколько вложенных функций, каждая из которых выделяет с помощью malloc() динамическую память. Если наиболее глубоко вложенная функция делает longjmp() обратно в main(), указатели на динамическую память теряются. Взгляните на ch12-memleak.c:

1  /* ch12-memleak.с --- демонстрирует утечки памяти с помощью setjmp()/longjmp(). */

2

3  #include

4  #include /* для определения ptrdiff_t в GLIBC */

5  #include

6  #include

7

8  jmp_buf env;

9

10 void f1(void), f2(void);

11

12 /* main --- утечка памяти с помощью setjmp() и longjmp() */

13

14 int main(void)

15 {

16  char *start_break;

17  char *current_break;

18  ptrdiff_t diff;

19

20  start_break = sbrk((ptrdiff_t)0);

21

22  if (setjmp(env) == 0) /* первый раз */

23   printf("setjmp called\n");

24

25  current_break = sbrk((ptrdiff_t) 0);

26

27  diff = current_break - start_break;

28  printf("memsize = %ld\n", (long)diff);

29

30  f1();

31

32  return 0;

33 }

34

35 /* f1 --- выделяет память, осуществляет вложенный вызов */

36

37 void f1(void)

38 {

39  char *p = malloc(1024);

40

41  f2();

42 }

43

44 /* f2 --- выделяет память, выполняет longjmp */

45

46 void f2(void)

47 {

48  char *p = malloc(1024);

49

50  longjmp(env, 1);

51 }

Эта программа устанавливает бесконечный цикл, используя setjmp() и longjmp(). Строка 20 использует для нахождения текущего начала кучи sbrk() (см. раздел 3.2.3 «Системные вызовы: brk() и sbrk()»), а затем строка 22 вызывает setjmp(). Строка 25 получает текущее начало кучи; это место каждый раз изменяется, поскольку longjmp() повторно входит в код. Строки 27–28 вычисляют, сколько было выделено памяти, и выводят это количество. Вот что происходит при запуске:

$ ch12-memleak /* Запуск программы */

setjmp called

memsize = 0

memsize = 6372

memsize = 6372

memsize = 6372

memsize = 10468

memsize = 10468

memsize = 14564

memsize = 14564

memsize = 18660

memsize = 18660

...

Память утекает из программы, как через решето. Она работает до тех пор, пока не будет прервана от клавиатуры или пока не закончится память (в этом случае образуется основательный дамп ядра).

Перейти на страницу:

Похожие книги

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных