C/C++ Programming Pearls

Tree example
treeLRbuild.c
/*
   program: treeLRbuild.c

   description: sample build left right tree.

   output:

          LNR
          -1.000000
          0.000000
          -2.000000
          
          NLR
          0.000000
          -1.000000
          -2.000000
          
          LRN
          -1.000000
          -2.000000
          0.000000
          
          
*/
#include <stdio.h>
#include <stdlib.h>

struct linked_list
{
  double item;
  struct linked_list *left;
  struct linked_list *right;
};

void
insert_left (struct linked_list **main_list, double item_to_insert)
{
  struct linked_list *new_item;
  new_item = malloc (sizeof (struct linked_list));
  if (new_item == NULL)
    {
      fprintf (stderr, "Out of memory: insert_left\n");
      exit (1);
    }
  new_item->item = item_to_insert;
  if (*main_list == NULL)
    {
      new_item->left = NULL;
      new_item->right = NULL;
      *main_list = new_item;
    }
  else
    {
      new_item->left = (*main_list)->left;
      new_item->right = NULL;
      (*main_list)->left = new_item;
    }
}

void
insert_right (struct linked_list **main_list, double item_to_insert)
{
  struct linked_list *new_item;
  new_item = malloc (sizeof (struct linked_list));
  if (new_item == NULL)
    {
      fprintf (stderr, "Out of memory: insert_right\n");
      exit (2);
    }
  new_item->item = item_to_insert;
  if (*main_list == NULL)
    {
      new_item->left = NULL;
      new_item->right = NULL;
      *main_list = new_item;
    }
  else
    {
      new_item->left = NULL;
      new_item->right = (*main_list)->right;
      (*main_list)->right = new_item;
    }

}

void
print_LNR(struct linked_list *main_list)
{
  if (main_list == NULL)
    return;
  print_LNR(main_list->left);
  printf("%f\n",main_list->item);
  print_LNR(main_list->right);

}

void
print_NLR(struct linked_list *main_list)
{
  if (main_list == NULL)
    return;
  printf("%f\n",main_list->item);  
  print_NLR(main_list->left);
  print_NLR(main_list->right);
}

void
print_LRN(struct linked_list *main_list)
{
  if (main_list == NULL)
    return;
  print_LRN(main_list->left);
  print_LRN(main_list->right);
  printf("%f\n",main_list->item);  
}



int
main ()
{
  struct linked_list *main_list = NULL, *head = NULL;
  insert_right (&main_list, 0.0);
  insert_left (&main_list, -1);
  insert_right (&main_list, -2);
  printf("LNR \n");
  print_LNR(main_list);
  printf("\nNLR \n");
  print_NLR(main_list);
  printf("\nLRN \n");
  print_LRN(main_list);



}


Memory Problems?

  For a quick check of your application do the following:
  • $export MALLOC_TRACE="trace.log"    -- this is done at the shell prompt
  • Insert the header #include <mcheck.h>
  • insert in your code mtrace(); -- there is an muntrace();  if you only want certain sections.
  • compile and run the program
  • $mtrace trace.log   -- this is done at the shell prompt
 
Function pointer sample
mysmplFunPtrSample.c

/*
Copyright (C) 2003  Mike Chirico mmc mchirico@mail.med.upenn.edu  GPL


Function pointer sample


*/


#include <stdio.h>
#include <string.h>
#include <ctype.h>
void show(char* (* fp)(char *), char * str);
char* ToUpper(char *);

int main(void)
{

  char *s;  /* char *s="one"; will cause seg fault */
  char* (*pfun)(char *);

  s = (char *) malloc (sizeof(char)* 400);
  if ( s == NULL)
    {
      fprintf(stderr,"Out of memory\n");
      exit(1);
    }
  pfun = ToUpper;
  strcpy(s,"one");
  show(pfun,s);

}
char* ToUpper(char * str)
{

    while (*str != '')
      {
        *str = toupper(*str);
        str++;
      }
    return "ToUpper called";
  }
void show(char* (* fp)(char *), char * str)
{
    printf("%s    :str= %s\n",(*fp)(str),str);

  }





 
SourceForge.net Logo