Online Book Reader

Home Category

Professional C__ - Marc Gregoire [11]

By Root 1353 0
function that takes no parameters just has an empty parameter list. It is not necessary to use void to indicate that no parameters are taken. However, you should still use void to indicate when no value is returned.

C++ functions can also return a value to the caller. The following function declaration and definition is for a function that adds two numbers and returns the result.

int addNumbers(int number1, int number2);

int addNumbers(int number1, int number2)

{

int result = number1 + number2;

return result;

}

In C++11, every function has a local predefined variable __func__ that looks as follows:

static const char __func__[] = "function-name";

This variable can for example be used for logging purposes:

int addNumbers(int number1, int number2)

{

std::cout << "Entering function " << __func__ << std::endl;

int result = number1 + number2;

return result;

}

Those Are the Basics

At this point, you have reviewed the basic essentials of C++ programming. If this section was a breeze, skim the next section to make sure that you’re up to speed on the more-advanced material. If you struggled with this section, you may want to obtain one of the fine introductory C++ books mentioned in Appendix B before continuing.

DIVING DEEPER INTO C++


Loops, variables, and conditionals are terrific building blocks, but there is much more to learn. The topics covered next include many features designed to help C++ programmers with their code as well as a few features that are often more confusing than helpful. If you are a C programmer with little C++ experience, you should read this section carefully.

Pointers and Dynamic Memory

Dynamic memory allows you to build programs with data that is not of fixed size at compile time. Most nontrivial programs make use of dynamic memory in some form.

The Stack and the Heap

Memory in your C++ application is divided into two parts — the stack and the heap. One way to visualize the stack is as a deck of cards. The current top card represents the current scope of the program, usually the function that is currently being executed. All variables declared inside the current function will take up memory in the top stack frame, the top card of the deck. If the current function, which we’ll call foo() calls another function bar(), a new card is put on the deck so that bar() has its own stack frame to work with. Any parameters passed from foo() to bar() are copied from the foo() stack frame into the bar() stack frame. Figure 1-2 shows what the stack might look like during the execution of a hypothetical function foo() that has declared two integer values.

FIGURE 1-2

Stack frames are nice because they provide an isolated memory workspace for each function. If a variable is declared inside the foo() stack frame, calling the bar() function won’t change it unless you specifically tell it to. Also, when the foo() function is done running, the stack frame goes away, and all of the variables declared within the function no longer take up memory.

The heap is an area of memory that is completely independent of the current function or stack frame. You can put variables on the heap if you want them to exist even when the function in which they were declared has completed. The heap is less structured than the stack. You can think of it as just a pile of bits. Your program can add new bits to the pile at any time or modify bits that are already in the pile.

Dynamically Allocated Arrays

Due to the way that the stack works, the compiler must be able to determine at compile time how big each stack frame will be. Since the stack frame size is predetermined, you cannot declare an array with a variable size. The following code will not compile because the arraySize is a variable, not a constant.

int arraySize = 8;

int myVariableSizedArray[arraySize]; // This won't compile!

Because the entire array must go on the stack, the compiler needs to know exactly what size it will be, so variables aren’t allowed. However, it is possible to specify the size of an array at run time by using dynamic memory and

Return Main Page Previous Page Next Page

®Online Book Reader