Uninitrilized Variables

This is a discussion on Uninitrilized Variables within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <fstream> #pragma warning ( disable : 4101 ) char file[] = "tsd.txt"; int main () { ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    141

    Uninitrilized Variables

    Code:
    #include <iostream>
    #include <fstream>
    #pragma warning ( disable : 4101 )
    
    char file[] = "tsd.txt";
    
    
    int main  ()
    {
    	
    	std::fstream * fp;
    	fp->open(file, fp->in | fp->out | fp->app);
    	fp->get();
    	fp->close();
    
    }
    The compiler is giving me back an error of fp being uninitrilized..

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    That's because it is. Why are you declaring a pointer and not making it point anywhere?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Why are you using a pointer in the first place?

    >> uninitrilized
    uninitialized

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by MacGyver View Post
    That's because it is. Why are you declaring a pointer and not making it point anywhere?
    Its pointing to a memory address, but I dont know what memory address, I would think it would be fstream, it gives me an error if I dont use a pointer so.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    141
    Nevermind, it works now, I just used the period instead of the object operand. , but why does it do that?

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by bobbelPoP View Post
    Its pointing to a memory address, but I dont know what memory address, I would think it would be fstream, it gives me an error if I dont use a pointer so.
    Declaring a pointer makes it point to something random. That means you're trashing memory when trying to use it.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    If you create an object like this:
    Code:
    std::fstream fstr;
    Then an object is constructed in memory saved for it on the stack. When the current block of code ends, then the object is destroyed automatically and the memory is automatically freed.

    If you're using dynamic memory, then you use new to allocate memory for the object somewhere else and construct the object there.
    Code:
    std::fstream * fp = new fstream;
    The result of the call to new is an address to the location of the new memory allocated. That's what you might normally assign to the fp pointer. When you just created the pointer, it wasn't pointing to an object anywhere, so calling functions on it wouldn't work correctly.

    You should use the first version wherever possible in C++. The second version requires you to manually call delete to destroy the object and clean up the memory when you're done, which is easy to forget and error prone:
    Code:
    delete fp;
    >> I just used the period instead of the object operand.
    The period works on objects like fstr in the first example, but you need to use -> on pointers. It probably wasn't the period that fixed the problem, it was the switch to using a local object instead of a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM

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