Page 3 of 3 FirstFirst 1 2 3
Results 21 to 30 of 30

Thread: [C/C++] Stuck On a Create-A-Level

  
  1. #21
    Razzlegames is offline Programmer -Hacks Neophyte
    Join Date
    Nov 2010
    Posts
    77
    Rep Power
    63

    Default

    Quote Originally Posted by HadesMinion View Post
    I wish I had enough time to do both, and I would if my mom wasn't pushing me for these God damn college classes, but she's right, it's good I'm getting them free because the books alone cost like 100-200 bucks for a fucking bundle of paper... what a rip off. Anyways, hopefully I'll finish my side-project quickly and incorporate it into Stitch'd soon.
    #1) Focus on College man. Seriously. You can do this stuff on your free time, no worries.

    With what you have been doing, you'll easily get a scholarship if you keep your grades up and all that.

    You'll blow by all the Computer Science courses. Don't forget to pay close attention in all your Math and sciences. All this will pay off big.

    Even if you aren't a very lucky guy, the average software engineer earns about 100K after you've been around for a bit. It's worth all the school work especially if you enjoy engineering and programming.

    #2) When you're in college (at the latest midway) look for an internship to get hands on experience in a work setting. It makes you a LOT more valuable when you graduate.

    Hope I haven't beaten these topics around with you before, if so sorry to sound like a broken record

    BTW, feel free to ask me anything. I went through all of this not so long ago, and am still fooling around in grad school.

    Check out my first PSP release: TimeCube The Game
    Some of my art:
    YouTube

  2. #22
    HadesMinion's Avatar
    HadesMinion is offline -Hacks Smarty
    Join Date
    Oct 2010
    Location
    Wisconsin
    Posts
    176
    Rep Power
    10914

    Default

    Yeah, I guess you're right, just a lot of work x.x Thanks lol, and no you haven't really talked about this to me, so you're not a broken record (I can't make any future promises though)

  3. #23
    Razzlegames is offline Programmer -Hacks Neophyte
    Join Date
    Nov 2010
    Posts
    77
    Rep Power
    63

    Default

    Quote Originally Posted by hardhat View Post
    Well, new and delete are only available in C++, so if you happen to be using C you can't use them. The nice thing about new is that it creates the correct type automatically, and calls any class constructor if it is defined.

    On the other hand, C++ doesn't have a parallel to realloc() which can be used to make a buffer larger: malloc, then as the buffer grows call realloc() to expand the buffer. Then free() it when it is done.

    So basically some code expects new/delete and other code expects malloc/calloc/realloc/free.
    Yes and to add to this, if you define destructors for your class, free doesn't make any guarantees to call the destructor. Also malloc will not call your constructor (which means your class could have an invalid state if you're not careful).

    So use new and delete for classes in C++

    e.g.:
    Code:
    class B
    {
        char* buff;
        B(){ buff = new buff[1]; }
    
        ~B(){ delete[] buff; } 
    };
    
    /// Somewhere in another cpp file, etc in main() etc
    // ....
    
    // This case is good (all memory is freed up properly including buff! :) )
    B* best = new B();
    delete best;
    
    
    // This case is bad, buff may not be deleted and the 
    // destructor ~B() never called.
    B* worst = (B*)malloc(sizeof(B));
    
    // Initialize B to default values, we remembered this!
    *worst = B();
    
    //  Uh oh we forgot, free will never call destructor and now buff is never 
    // freed, meaning... dun dun dun... dreaded memory leak!
    free(worst);
    Check out my first PSP release: TimeCube The Game
    Some of my art:
    YouTube

  4. #24
    HadesMinion's Avatar
    HadesMinion is offline -Hacks Smarty
    Join Date
    Oct 2010
    Location
    Wisconsin
    Posts
    176
    Rep Power
    10914

    Default

    Wow I didn't know that. Seems the further you get into C++ the easier it is to cause memory leaks lol. Thanks for all your help. Sorry if my comment seems a bit emotionless or rude or anything like that, stuff is just coming out wrong because I've been completely swamped with work lately. Plus I got a butterfly knife and I've been trying to learn some stuff with that.

  5. #25
    Razzlegames is offline Programmer -Hacks Neophyte
    Join Date
    Nov 2010
    Posts
    77
    Rep Power
    63

    Default

    No worries man, I didn't even detect the slightest bit of rude

    BTW, one more thing I forgot to mention:

    "C++ doesn't have a parallel to realloc()"
    This is not a real problem, since most cases of realloc involve resizing arrays/buffers or strings (char arrays). In fact that's the only practical use I can think of for reallocing something.

    C++ has containers such as vector<T> which will resize automatically when adding new elements, as well as a true string class which will grow when concatenating.

    Most of these issues are handled for you, as long as you follow the interface to these containers.
    Check out my first PSP release: TimeCube The Game
    Some of my art:
    YouTube

  6. #26
    hardhat is offline Programmer -Hacks Enthusiast
    Join Date
    May 2007
    Posts
    544
    Rep Power
    65

    Default

    Well the C++ standards group is doing their best to actually make something like realloc in the new C++2011 standard. So although stl does it's best to make it transparent to you, the lower level language could be more efficient.

    My 2D homebrew | My 3D homebrew with Team Sushi. | My YouTube channel. | My Twitter
    Spoiler:

    hardhat: F1 == help on Windows
    J697: oh I have never used the help, so I wouldnt know
    hardhat: so that's why you're always lost. :-P

  7. #27
    Razzlegames is offline Programmer -Hacks Neophyte
    Join Date
    Nov 2010
    Posts
    77
    Rep Power
    63

    Default

    RE efficiency of realloc: Yep, in the case of free contiguous memory adjacent to the array. I wonder if they could not implement this anyhow underneath at the system level, for at least STL? It would seem trivial to wrap.
    Check out my first PSP release: TimeCube The Game
    Some of my art:
    YouTube

  8. #28
    HadesMinion's Avatar
    HadesMinion is offline -Hacks Smarty
    Join Date
    Oct 2010
    Location
    Wisconsin
    Posts
    176
    Rep Power
    10914

    Default

    Speaking of C++11, when is that actually going to be released? Will it be compatible with the PSPSDK? I know it's a bit late to post back but I've been even busier than normal.

  9. #29
    Razzlegames is offline Programmer -Hacks Neophyte
    Join Date
    Nov 2010
    Posts
    77
    Rep Power
    63

    Default

    It's pretty much when GCC decides to adopt it. Also, though, when the custom PSP compilation tool would include that version of GCC to build against.

    http://www2.research.att.com/~bs/C++0xFAQ.html

    Note: Sometimes the new additions/omissions by creating new standards (or even just compiler versions that are more/less compliant), will cause your old code to break.

    In the mean time you can use some of the features they will be including via boost (shared_ptr Smart pointers and all this).

    Also, if you really need a buffer of primitives, using malloc and realloc makes sense (since not classes, you don't have constructor/destructor issues I've mentioned). So I do this sometimes for primitive arrays of data if I ever need a quick resize etc (rare).
    Check out my first PSP release: TimeCube The Game
    Some of my art:
    YouTube

  10. #30
    HadesMinion's Avatar
    HadesMinion is offline -Hacks Smarty
    Join Date
    Oct 2010
    Location
    Wisconsin
    Posts
    176
    Rep Power
    10914

    Default

    Oh, so it's ready but not adopted yet? That's cool, wonder when it'll become a/the new standard.

Page 3 of 3 FirstFirst 1 2 3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •