SE250:HTTB:Pointers:Common pointer mistakes

From Marks Wiki
Jump to navigation Jump to search

<html>

<image src="http://www.rajithaonline.com/SE250/httb/pointers/final/common_mistakes_head.png" width="549" height="92" alt="Common Pointer Mistakes Header Logo" />

</html>

Previous Page Contents Next Page



Wild pointers

When a pointer is declared in C it will point to a random location in memory until initialised

Example: Dereferencing a wild pointer

int *ptr;

*ptr = 2;

This changes the data at some random address in memory to the value of 2, which is almost always unintended mistake! At this point the program will close, because you are trying to change a section of memory not allocated to the program.

There are many things that can cause a wild pointer:


  • Incorrect pointer arithmetic

Example: A pointer is incremented to point outside of its intended memory. See Pointer Arithmetic section below.


  • "Freeing" the used memory, this is called a dangling pointer

Example: A pointer is allocated an int sized section of memory to point to. The memory is then freed.

For more details, see Dangling pointers.

Dangling pointers

int *ptr = malloc( sizeof(int) );

free( ptr );

The pointer will still be pointing at that section of memory, as C has no garbage collection system. This is dangerous if the above situation is unintended and the pointer is still used. If malloc allocates the same section of memory to something else, the dangling pointer could mess up the program. malloc() and free() are explained in detail above.

C Strings

Have a look at the code below. What the programmer who wrote this code intended to do was to change the C string str so it now contains the string "world" instead of "hello". In more technical terms, he wants to make the character pointer str point to where the constant string "world" resides. As in, the output should be "world" followed by "world" again.

#include <stdio.h>

void function( char *str )
{
    str = "world";
    puts( str );
}

int main( void )
{
    char *str;

    str = "hello";
    function( str );

    puts( str );

    return 0;
}

If you run this code with the command as shown below:

gcc string_err.c && ./a.exe

You get the output:

world
hello

Why is this? Explanation using screen cast shown below:

<html>

           <object id="csSWF" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="498" codebase="http://active.macromedia.com/flash7/cabs/ swflash.cab#version=9,0,28,0">
               <param name="src" value="http://www.rajithaonline.com/SE250/httb/pointers/rwan064_1/rwan064_1.swf"/>
               <param name="bgcolor" value="#1a1a1a"/>
               <param name="quality" value="best"/>
               <param name="allowScriptAccess" value="always"/>
               <param name="allowFullScreen" value="true"/>
               <param name="scale" value="showall"/>
               <param name="flashVars" value="autostart=false"/>
               <embed name="csSWF" src="http://www.rajithaonline.com/SE250/httb/pointers/rwan064_1/rwan064_1.swf" width="640" height="498" bgcolor="#1a1a1a" quality="best" allowScriptAccess="always" allowFullScreen="true" scale="showall" flashVars="autostart=false" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>
           </object>

</html>

Intranet Link (Faster if at university)

NOTE: If the screen cast seems too fast when reading the text, pause it when you come to the text. This is to keep the screen cast nice and short and also reduces the file size.


Previous Page Contents Next Page