Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -



Chapter:C memory tools

From Juneday education
Revision as of 00:50, 13 December 2016 by Henrik Sandklef (Talk | contribs) (Created page with "Category:Functions ==Meta information about this chapter== <div class="mw-collapsible mw-collapsed"> Expand using link to the right to see the full content. <div class="...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Meta information about this chapter

Expand using link to the right to see the full content.

Introduction

Purpose

Requirements

The student shall be familiar with the following concepts:

Goal

The student shall be able use the functions:

Instructions to the teacher

Common problems

Chapter videos

All videos in this chapter:

  • C Memory tools [ (eng)]

See below for individual links to the videos.

Memory leaks

Description

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations[1] in such a way that memory which is no longer needed is not released. In object-oriented programming, a memory leak may happen when an object is stored in memory but cannot be accessed by the running code.[2] A memory leak has symptoms similar to a number of other problems and generally can only be diagnosed by a programmer with access to the program's source code.

Memory_leak (wkipedia link)

Tools

Valgrind / memcheck

Valgrind is a tool to help you analysememory management and profile your program (among other things).

Memcheck detects memory-management problems, and is aimed primarily at C and C++ programs. When a program is run under Memcheck's supervision, all reads and writes of memory are checked, and calls to malloc/new/free/delete are intercepted. As a result, Memcheck can detect if your program:

  • Accesses memory it shouldn't (areas not yet allocated, areas that have been freed, areas past the end of heap blocks, inaccessible areas of the stack).
  • Uses uninitialised values in dangerous ways.
  • Leaks memory.
  • Does bad frees of heap blocks (double frees, mismatched frees).
  • Passes overlapping source and destination memory blocks to memcpy() and related functions.

Memcheck reports these errors as soon as they occur, giving the source line number at which it occurred, and also a stack trace of the functions called to reach that line. Memcheck tracks addressability at the byte-level, and initialisation of values at the bit-level. As a result, it can detect the use of single uninitialised bits, and does not report spurious errors on bitfield operations. Memcheck runs programs about 10--30x slower than normal.

Valgrind tools

Videos

Memory management error

Description

Reading or writing to memory not "belonging to you", reading unitialised memory, reading/writing beyond an arrays limit etc are examples of memory management errors. As an example consider the code below:

int values[100];
values[123] = 137;

It is easy to see that the code above will write outside of the memory we (most liklely) want to use. It is equally easy to fix the problem, either by allocating a bigger array or writing inside the limits of the array. But how do we detect these kind of problems in a big program?

Same as above, we use memory tools.

Tools

There's a variety of tools out there for this purpose. We will give you some examples here:

Valgrind

See previous introduction to valgrind.


Videos