Thread: Using fstream pointers

  1. #16
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Thanks to both for your feedback, for 'cleaner' solutions to 1 and 2 I'd:
    Code:
    //1;
    bool is_special(std::string result) {
       return result == "special";
    }
    //2.
    cin>>size;
    Coord coords[size];
    //... as before;
    I've just drunk the smart-pointer Kool Aid and therefore wherever I saw a raw pointer my inclination was indeed to get rid of it and replace it with a smart one. Such a sledgehammer approach is probably (definitely?) wrong and when I read Elysia's comments in #13 I realised that there are severe limitations to this approach.

  2. #17
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    #1 - naming the parameter "result" is kind of confusing. :P But to the meat of things - the copy might be optimized out, or it might not, it'd be even better if you passed the string by const reference.
    #2 - are you using VLA in C++? VLAs are not actually part of C++ even if they work for you as an extension (you're not compiling with -pedantic). Why not a... you know, std::array, or std::vector that you resize manually so that it doesn't waste time with reallocations.
    #4 - sometimes you'll work with legacy libraries - they will want a pointer to this or a pointer to that... In that case the memory is managed by the smart pointer, so you don't have to worry about that. You know it'll be 'live' during the time of that call, so you can safely just throw it a raw pointer. You'd have to do get to it anyway, since you can't change the library interface.
    More even - let's say you use a shared_ptr there instead of unique_ptr, and return a shared_ptr by copy. Your large frame goes out scope, but before that happens you took a shared_ptr to its data and stored it somewhere. Your frame is dead, but part of its internal data is alive. Is that good? Should you even let that happen?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How would this look if I used fstream?
    By chrishowarth in forum C++ Programming
    Replies: 2
    Last Post: 05-04-2007, 10:03 AM
  2. fstream not .h
    By siavoshkc in forum C++ Programming
    Replies: 3
    Last Post: 02-06-2006, 04:49 PM
  3. About "fstream" in <fstream.h> and <fstream>
    By L.O.K. in forum C++ Programming
    Replies: 5
    Last Post: 01-08-2005, 06:49 PM
  4. fstream
    By sdchem in forum C++ Programming
    Replies: 9
    Last Post: 05-07-2003, 12:12 AM
  5. fstream
    By ygf_person in forum C++ Programming
    Replies: 8
    Last Post: 02-19-2002, 07:46 PM

Tags for this Thread