Check if a pointer exsists

This is a discussion on Check if a pointer exsists within the C++ Programming forums, part of the General Programming Boards category; I am currently working on a program that needs to create as many files as the user specifies and I ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    Check if a pointer exsists

    I am currently working on a program that needs to create as many files as the user specifies and I was having a little problem with the naming of the file. I tried to use my count variable to assign a name but fstream will not accept it. So now I am trying to use a char pointer and the heap to hold names. I am more worried with getting the pointer and all to work before I start to worry about how I will assign a value to the where the pointer is pointing. My code so far-
    Code:
    #include <fstream>
    #include <iostream>   //I know, I know, but VC++6 complains
    using namespace std;
    
    char CreateName(int num)
    {
    	if (!pName)
    	{
    		*pName = new char;
    		*pName = num;
    		return *pName;
    	}
    	else
    	{
    		delete pName;
    		*pName = new char;
    		*pName = num;
    		return *pName;			
    	}
    }
    
    int main()
    {
    	int amnt;
    	cout << "How many files?: ";
                    cin >> amnt;
    	for(int i; i < amnt; i++)
    	{
    		ofstream newfile(CreateName(i));
    		newfile.close();
    	}
    	return 0;
    }
    I am not even sure if char is an acceptable return value, I don't see why not though. But I have not been able to test this out on a compiler yet (I already know it would bring up errors).

    How would I check to see if the pointer already exsisted so I know if I need to delete it or not before trying to create a new one? Or should I try something else for the naming?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What is pName???
    If it is a char** it might work, if you allocate memory for an array of char* first, but that wouldn't be so dynamic. I'd make a linked list of names instead:
    Code:
    typedef struct _NODE
    {
       char* Name
       struct _NODE* NextNode;
    }NODE;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    pName is the pointer to where I will be storing the name of the file...

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    if pName is a pointer you can for instance do 2 things:

    first, if pName has already been declared before somewhere in the code, you can do the following:

    Code:
        if(pName == NULL)
              cout << "pName is not allocatted b/c it point to NULL ";

    or perhpas inside of 'constructor' at the time of intended allocation you can do the following:

    Code:
    #include<assert.h>   // include at the top of the program
    
    
         assert(pName != NULL);    //   checks if pName has allocated
                                   //   a value (address) in your case
                                   //    on the heap...
    keep in mind that assert() function tends to terminate the execution process...


    hope that answers your question
    ...........................
    matheo917

  5. #5
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Well what exactly is a char**, a pointer to a pointer?

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Munkey01
    Well what exactly is a char**, a pointer to a pointer?
    Yup!
    And since an array and a pointer is basically the same thing, char** can also be an array of pointers or a pointer to an array (depending on how you allocate the memory).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 03:29 PM
  2. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 07:41 PM
  3. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 08:35 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 06:40 PM

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