problems with function template

This is a discussion on problems with function template within the C++ Programming forums, part of the General Programming Boards category; I have written a function template that takes a pointer to an object and prints out its contents and memory ...

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    problems with function template

    I have written a function template that takes a pointer to an object and prints out its contents and memory address.

    Here is the code:

    Code:
    #include <iostream>
     #include <string>
     #include "employee.h"
     #include "employee.cpp"
     #include "myfuncts.h"
     #include "myfuncts.cpp"
    
    
     template <class Type>
     Type iaddress(Type * obj);
    
     int main()
     {
    	 double num1 = 1.23;
    	 double num2 = 100.75;
    	 char *str1 = new char[26];
    	 strcpy(str1, "Pointers are interesting!");
    	 double num3 = -35.5;
    	 double *ptr4 = new double;
    
    
    	 // str1 output
    	 cout << "str1 - main: address: "  << &str1 << " contents: " << str1 << endl;
    
    	 //	num1 output and function call
    	 cout << "num1 - main: address: " << &num1 << " contents: " << num1 << endl;
    	 ptr4 = &num1;
    	 iaddress(ptr4);
    
    	 //	num2 output and function call
    	 cout << "num2 - main: address: " << &num2 << " contents: " << num2 << endl;
    	 ptr4 = &num2;
    	 iaddress(ptr4);
    
    	 //	num3 output and function call
    	 cout << "num3 - main: address: " << &num3 << " contents: " << num3 << endl;
    	 ptr4 = &num3;
    	 iaddress(ptr4);
    
    	 //	ptr4 output and function call
    	 *ptr4 = 123.4;
    	 cout << "ptr4 - main: address: " << ptr4 << " contents: " << *ptr4 << endl;
    	 iaddress(ptr4);
    
    	 int num4 = 10;
    	 int * ptr2 = new int;
    	 cout << "num4 - main: address: " << &num4 << " contents: " << num4 << endl;
    	 ptr2 = &num4;
    	 iaddress(ptr2);
    
    	 string str2 = "Computer Science is Cool!";
    	 string *ptr5 = new string;
    	 cout << "str2 - main: address: "  << &str2 << " contents: " << str2 << endl;
    	 ptr5 = &str2;
    	 iaddress(ptr5);
    
    	 Employee emp1("Bugs Bunny", 12.47);
    
    	 emp1.print();
    
    	 //	testing output
    
    	 return 0;
     	 }
    
     	 template <class Type>
     	 Type iaddress(Type * obj)
     	 {
    		 cout << "  function: address: " << obj << " contents: " << *obj << endl;
    		 delete obj;
    		 return (0);
    	 }
    My program crashes after the last function call, and I can't figure out why. Any ideas?

    Brian

  2. #2
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    string is causing the problem

    the program compiles and runs through all of the doubles, the int, and even the Employee objects. However, when I add the string object, it crashes my program, even though it compiles.

    Once again, here is the code:

    Code:
     #include <iostream>
     #include <string>
     #include <string.h>
     #include "employee.h"
     #include "employee.cpp"
     #include "myfuncts.h"
     #include "myfuncts.cpp"
    
    
     template <class Type>
     Type iaddress(Type * obj);
    
     int main()
     {
    	 double num1 = 1.23;
    	 double num2 = 100.75;
    	 char *str1 = new char[26];
    	 strcpy(str1, "Pointers are interesting!");
    	 double num3 = -35.5;
    	 double *ptr4 = new double;
    
    
    	 // str1 output
    	 cout << "str1 - main: address: "  << &str1 << " contents: " << str1 << endl;
    
    	 //	num1 output and function call
    	 cout << "num1 - main: address: " << &num1 << " contents: " << num1 << endl;
    	 ptr4 = &num1;
    	 iaddress(ptr4);
    
    
    	 //	num2 output and function call
    	 cout << "num2 - main: address: " << &num2 << " contents: " << num2 << endl;
    	 ptr4 = &num2;
    	 iaddress(ptr4);
    
    	 //	num3 output and function call
    	 cout << "num3 - main: address: " << &num3 << " contents: " << num3 << endl;
    	 ptr4 = &num3;
    	 iaddress(ptr4);
    
    	 //	ptr4 output and function call
    	 *ptr4 = 123.4;
    	 cout << "ptr4 - main: address: " << ptr4 << " contents: " << *ptr4 << endl;
    	 iaddress(ptr4);
    
    	 int num4 = 10;
    	 int * ptr2 = new int;
    	 cout << "num4 - main: address: " << &num4 << " contents: " << num4 << endl;
    	 ptr2 = &num4;
    	 iaddress(ptr2);
    
    
    	 string str2 = "Computer Science is Cool!";
    	 string * ptr5 = new string[26];
    	 cout << "str2 - main: address: "  << &str2 << " contents: " << str2 << endl;
    	 ptr5 = &str2;
    	 iaddress(ptr5);
    
    	 Employee emp1("Bugs Bunny", 12.47);
    	 Employee * ptr_emp = new Employee;
    	 cout << "emp1 - main: address: " << &emp1 << " contents: " << emp1 << endl;
    	 ptr_emp = &emp1;
    	 iaddress(ptr_emp);
    
    	 //	testing output
    
    	 return 0;
     	 }
    
     	 template <class Type>
     	 Type iaddress(Type * obj)
     	 {
    		 cout << "  function: address: " << obj << " contents: " << *obj << endl;
    		 delete obj;
    	}
    Obviously you don't have the Employee class to compile and run this program as necessary, but you could just comment it out to run it.

    Once again, thanks for any help on this.

    Brian

  3. #3
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    problem

    if i remove the function call for the string then everything works fine.
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    Which leads to the question "why are you delegating memory management to class like this?". The template should simply print addresses/contents.

    Next, what are you doing here:

    int * ptr2 = new int;
    //...
    ptr2 = &num4;

    delete ptr2

    A pointer is simply a variable that holds an address. You don't need to "allocate" it. Just point it to a piece of memory. Sometimes we need a chunk of memory on the fly. So we "new" it, and point a pointer at it so that we can keep up with it.

    int num;
    int * p;
    int * z;
    p = new int;

    Now let's say we want to repoint 'p' to 'num'. Well, first we should "save" a pointer to the new'ed memory that 'p' points to:

    z = p;
    p = &num;
    delete z; //..clean up memory initially pointed to by 'p'.



    ITSA
    Socket Library!

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    First -- you can only delete stuff you dynamically allocate!

    The function is fine in using delete.

    Everything dynamically allocated up to the string version of the function is completely fine both syntactically and logically!

    The reason the string function doesn't work is because when you dynamically allocate an array you can't just say

    delete ArrayName

    you have to say

    delete [] ArrayName

    This is because, if you don't, it will assume that ArrayName only points to one instance of the particular datatype. delete [] will delete all the memory you allocated.

  6. #6
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    thanks for the help

    Thanks for your help. I took a little advice from all and it seems to be working now. Here is the working program:


    Code:
    #include <iostream>
     #include <string>
     #include <string.h>
     #include "employee.h"
     #include "employee.cpp"
     #include "myfuncts.h"
     #include "myfuncts.cpp"
    
    
     template <class Type>
     Type iaddress(Type * obj);
    
     int main()
     {
    	 double num1 = 1.23;
    	 double num2 = 100.75;
    	 char *str1 = new char[26];
    	 strcpy(str1, "Pointers are interesting!");
    	 double num3 = -35.5;
    	 double *ptr4 = new double;
    
    
    	 // str1 output
    	 cout << "str1 - main: address: "  << &str1 << " contents: " << str1 << endl;
    	 iaddress(&str1);
    
    	 //	num1 output and function call
    	 cout << "num1 - main: address: " << &num1 << " contents: " << num1 << endl;
    	 ptr4 = &num1;
    	 iaddress(ptr4);
    
    
    	 //	num2 output and function call
    	 cout << "num2 - main: address: " << &num2 << " contents: " << num2 << endl;
    	 ptr4 = &num2;
    	 iaddress(ptr4);
    
    	 //	num3 output and function call
    	 cout << "num3 - main: address: " << &num3 << " contents: " << num3 << endl;
    	 ptr4 = &num3;
    	 iaddress(ptr4);
    
    	 //	ptr4 output and function call
    	 *ptr4 = 123.4;
    	 cout << "ptr4 - main: address: " << ptr4 << " contents: " << *ptr4 << endl;
    	 iaddress(ptr4);
    
    	 int num4 = 10;
    	 int * ptr2 = new int;
    	 cout << "num4 - main: address: " << &num4 << " contents: " << num4 << endl;
    	 ptr2 = &num4;
    	 iaddress(ptr2);
    
    
    	 char *str2 = new char[26];
    	 strcpy(str2, "Computer Science is Cool!");
    	 cout << "str2 - main: address: "  << &str2 << " contents: " << str2 << endl;
    	 iaddress(&str2);
    
    	 Employee emp1("Bugs Bunny", 12.47);
    	 Employee * ptr_emp = new Employee;
    	 cout << "emp1 - main: address: " << &emp1 << " contents: " << emp1 << endl;
    	 ptr_emp = &emp1;
    	 iaddress(ptr_emp);
    
    	 delete ptr4;
    	 delete ptr2;
    	 delete [] str1;
    	 delete [] str2;
    
    
    
    	 return 0;
     	 }
    
    
     	 template <class Type>
     	 Type iaddress(Type * obj)
     	 {
    		 cout << "  function: address: " << obj << " contents: " << *obj << endl;
    	 }
    Thanks again.


    Brian
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    You're still missing a very important point. Look:

    double *ptr4 = new double;
    ptr4 = &num1;// <-- You just lost track of the memory you new'ed
    delete ptr4; // <-- because of that, you are effectively deleting num1, do you see why?



    ITSA
    Socket Library!

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You're not understanding new and delete

    Think of it like this:

    a pointer's value represents a location in memory.

    new SomeDatatype

    Allocates for you sizeof( datatype ) amount of memory (and calls the default constructor if it's a class). It returns the location in memory of that data it just made.

    delete says "delete the memory at this location" (it knows the amount to delete from the type (and from another value if it's delete [] ).

    So, if you do

    double *ptr4 = new double; // Allocates memory and stores the location in ptr4
    ptr4 = &num1; // Now you don't know where the "new" memory is!
    delete ptr4; // Now you're deleting num1 instead and leaving ptr4 -- both very very bad

  9. #9
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    ok

    Then how can I make ptr4 point to num1? And then change the pointer to point to num2 and so on?

    Brian
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Just keep assigning it the address of those variables and don't use new or delete.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21