Notices
Results 1 to 2 of 2

Thread: How to pass in a non-specified type into a function? How to get the memory address of an element in an array pointer?

  1. #1 How to pass in a non-specified type into a function? How to get the memory address of an element in an array pointer? 
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    Garbage Collection Class
    I am trying to implement my own garbage collector that will work with any program in C++.
    The purpose of this program is that it must be able to be used without using #including hundreds of classes that any program has.

    I am doing it by using a class with some methods.
    Code:
    void AllocateMemory (string/int/Cmyclass* &myPointer){
    //Check if the pointer is already pointing to something.
    //And check if that memory is already being used, or not, by any other pointer.
    //Garbage Collect, if so.
    myPointer=new string/int/Cmyclass;
    }
    and an overloaded version for an array pointer.
    Code:
    void AllocateMemory (string/int/Cmyclass* &myPointer, int ArraySize){
    //Check if the pointer is already pointing to something.
    //And check if any of the memory address used by this pointer is that memory is already being used, or not, by any other pointer.
    //Garbage Collect, if so.
    myPointer=new string/int/Cmyclass [ArraySize];
    }
    There are two problems here:
    Problem 1: How to i let these methods accept any type of data (int/string/float/double/CmyClass/CmyClass2)?
    Problem 2: How can i get the address of myptr[2]?

    If problem 1 can't be solved(if it really can't), is there no way out of this problem other than doing:
    #include "Cmyclass1.h"
    #include "Cmyclass2.h"
    #include "Cmyclass3.h"
    #include "Cmyclass4.h"
    #include "Cmyclass5.h"
    #include "Cmyclass6.h"
    #include "Cmyclass7.h"
    #include "...........
    You get what i mean.....
    Oh yea, and also
    void AllocateMemory (Cmyclass1* &myPointer);
    void AllocateMemory (Cmyclass1* &myPointer);
    void AllocateMemory (Cmyclass2* &myPointer);
    void AllocateMemory (Cmyclass3* &myPointer);
    void AllocateMemory (Cmyclass4* &myPointer);
    void AllocateMemory (Cmyclass5* &myPointer);
    void AllocateMemory (Cmyclass6* &myPointer);
    void AllocateMemory (Cmyclass7* &myPointer);
    void AllocateMemory (..........

    and i almost forgot

    void AllocateMemory (Cmyclass1* &myPointer,int s);
    void AllocateMemory (Cmyclass1* &myPointer,int s);
    void AllocateMemory (Cmyclass2* &myPointer,int s);
    void AllocateMemory (Cmyclass3* &myPointer,int s);
    void AllocateMemory (Cmyclass4* &myPointer,int s);
    void AllocateMemory (Cmyclass5* &myPointer,int s);
    void AllocateMemory (Cmyclass6* &myPointer,int s);
    void AllocateMemory (Cmyclass7* &myPointer,int s);
    void AllocateMemory (..........
    I would have to change every single of these #include and these functions every time i want one to change.
    And also every program has to hard code it.

    A small question, down here too, if a class does not contain any data members, how much/(will there be any) memory will get reserved when an object of this class is created? what data does the memory hold?


    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  2.  
     

  3. #2  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    You need to look up templates. This is exactly the kind of thing they're meant for.

    Also you can still use the reference operator to get the address of an array element: &(array[2]). There are other ways, but that makes your intent clearest, I think.


    Reply With Quote  
     

Similar Threads

  1. C++ pointer to string error
    By RamenNoodles in forum Computer Science
    Replies: 5
    Last Post: December 6th, 2013, 07:40 PM
  2. The Brain and memory function questions.
    By theorist in forum Biology
    Replies: 10
    Last Post: March 31st, 2013, 09:22 AM
  3. C++: Address all objects or structs of a certain type?
    By collred in forum Computer Science
    Replies: 1
    Last Post: December 25th, 2009, 03:14 AM
  4. Photographic memory (Eidetic memory)
    By blue_space87 in forum Behavior and Psychology
    Replies: 11
    Last Post: March 28th, 2009, 02:13 AM
  5. Mouse pointer freezes
    By leohopkins in forum Computer Science
    Replies: 5
    Last Post: January 28th, 2008, 12:59 PM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •