Finals ~Complete~

Yay.

Winter break…! Here I come…!

Happiness.

View post on imgur.com

 

Working on the next Dragon’s Bloodline right now, but it’s pretty big (~50% longer than average) and will definitely be out… tomorrow!


 

Random link of the C++ group project source code for anyone interested. First semester in C++ so don’t expect anything profound. Most of it was done by me… if something looks brute forced, very likely added by the other 5 people? Lol.

ALSO!

Does anyone know a good way to delete ‘pointer to array of pointers’?

Like —

int main() {
____int** thing;

____thing = new int*[10];

____for (int x = 0; x < 10; x++) {
________thing[x] = new int(x);
________delete thing[x];
____}

____delete [] thing;
____return 0;
}

I used one to create an array of a objects with inputs requested from user for my C++ final, but it all just seemed clunky to ‘delete’ the ‘new’s… I dunno.  The above is a really shortened example, but hopefully my point gets across…?

Planning on learning everything I’ll need for my Data Structures class next semester before it starts to have an easier time. Hoping I could leech from you guys every so often for things I can’t find that easily on Google…? *shrug*

On the subject about “Next Semester”… egh. No, better to wait for the Schedule post for that.


 

Have a good night everyone!

34 thoughts on “Finals ~Complete~

  1. ? delete ‘pointer to array pointer’? uhh.. meatbun is confused..
    do you mean to delete one of the item inside of the array? or the slot(like thing has 10 index to 9 index) ?

    1. Well, pointers are preallocated spaces of memory in RAM. What I’m looking to delete is the preallocation, not just removing the contents of the pointer.

      1. uguu.. sorry meatbun don’t know.. if you want to reduce the preallocated space.. why not just change the format type like “thing= new bool();”or other types ..or just re use it for other functions too?
        hmm.. don’t know if this works but why not try “thing= null;”

        1. C++ doesn’t have garbage collection like some other popular languages (See: Java, C#), so it’s necessary to delete every ‘new’ created in the program.

          Trying to find a more elegant way to do it if possible.

          1. Bleh the discussion is all over the place so I’m just going to put everything here.

            First is I wouldn’t exactly call a pointer “preallocated spaces of memory in RAM”, but rather references to preallocated data.

            There’s really no way to get around with not pairing all your “new” with “delete”. Every object that is instantiated with “new” has to be deleted, but you can make it more elegant though. One way is by having a helper function to deallocate whatever object for you and in this case the array. If you think about it that’s pretty much the purpose of the destructor in a object. The second way depends on your design choice. Instead of using a array to store pointers, use a vector from the std to store your objects and have the other parts of your program use pointer to reference those object. This way you can clear the vector with one function call without having to loop through each element. You can do this with vectors because they’re kind of special.

            Technically, you don’t have to use “exit(0);”. All “exit(0);” really does is terminate the current process. Where your at right now, it’s essentially the same as “return 0;” in the main. The difference between “return 0;” and “exit(0);” is that “exit(0);” stands for a successful termination of a process and your more likely to see it when you take Operating System. It’s really used in a child process to tell the parent process if the child process completed it’s task or not. FYI, “exit(1);” stands for a unsuccessful termination.

            But what Anonymous said is correct. Most modern operating system allocates each process with a limited amount of resource so that when the process ends, the operating system can reclaim that resources.
            http://stackoverflow.com/questions/2975831/is-leaked-memory-freed-up-when-the-program-exits

            Note that by definition there is a huge difference in process and program. A program is essentially your source code. A process is the operating system executing the program. So the operating system would reclaim the memory from the process not program.

            Also I took a quick look at your code and there was a few issues that stood out. The way your using your header files is wrong. The header files should have actual code inside it, but rather for your decalarations. You should have another cpp file that has the code and inherit the header files declaration. For example your main would inherit from input.h and you would also have a input.cpp inherit from input.h. Here’s a simple example.
            http://stackoverflow.com/questions/7109964/creating-your-own-header-file-in-c

            Another thing the code has very inconsistent spacing and formatting. That’s kind of bad styling.
            You also shouldn’t include debugging code commented out. You should also learn how to use a actual debugging tool instead of printing stuff out to debug. If your using linux and command line then you want something like GDB. If your using a IDE, then you can probably just use that.

            Sorry about being brutal about the code, but I hope it helps.

            If your looking to start studying Data Structure I would say start looking at the difference between linkedlist and arrays. If you can understand that then go on to stacks and queue. If your interested I can give you more topic that you’ll likely have to cover for that class.

            It’s late so now I go pass out.

            1. Hey Ziru can you delete the above post. It wasn’t showing up before so I answered the question separately. in individual replies instead.

          2. Ow man don´t even talk about allocation and memory manipulation, i am already having headaches just from recalling my tests and group project of creating a game with C by using data estructures, we had to make one using Queue Urgh… the pain..

    1. Could you elaborate what you mean by unused pointers?

      After looking through this, it appears that ‘exit(0)’ acts like ‘return 0’ in ‘main()’. As C++ does not have garbage collection to automatically handle deletion of allocated pointers, what exactly allows it to delete them?

      Am I misunderstanding something? I mean, I guess this would could be referring to pointers? ‘Objects with static storage duration are destroyed (C++) and functions registered with atexit are called.’

      However, the page does state that objects with automatic storage are not destroyed. I thought that included pointers? Egh, semantics. I wonder if they aren’t referring to dynamic allocation by automatic storage?

      1. At the very least, I’m pretty sure pointers aren’t considered static storage. (Unless they are defined like ‘static int thing*;’ ?)

        So much to learn…

        1. OS frees all memory the process had reserved after it’s closed.

          If you want a serious solution – write a template pointer or use std::vector.

          1. Well that depends. For that to work you’d have to be storing the objects in the vector and not the pointer. Otherwise you’ll run into the same problem.

            The simplest answer I would say is to make a helper function to delete the pointers for you. You’ll still have to loop through everything, but it’ll make your code look more elegant.

      2. Bleh my other post didn’t get posted. Anyways, “exit(0);” and “return 0;” is the exact same thing in at the current level your at right now. It’s something you’ll likely see when you take Operating System. It’s used when you fork a process and want to see if the child process terminated successfully or not.

    2. That’s kind of bad practice to leave it to the Operating System to free up the memory. This definitely won’t work if your writing code on the kernel level though. So probably not a ideal solution in most cases.

  2. No, what you have there is pretty much it.

    As for exit … Well your program has space allocated to it by the operating system, so when your program exits the operating system just reclaims it.

    1. Sfaik C++ doesn’t deallocate without purposely using delete. School server actually locked up from memory leak from a classmate not deleting pointer allocations after it hit 100% ram from it lol

      1. Not during the lifetime of your program. However, the operating system knows what memory is associated with your program and that it doesn’t need to last beyond the lifespan of the program.

        1. Any decent programmer would not assume the memory will be deallocated though. If you can clear the memory, do so. Or reuse it. It’s a best practice that should be followed.

      2. Anonymous is correct that memory is deallocated when the process is terminated in MOST modern system. Operating System don’t usually let a process have free range on the memory. So what happens is that the operating system assigns each process with VM or virtual memory. You can kind of think of this as a sort of encapsulation and that kind of makes it possible for the operating system to reclaim memory leaks once a process terminates. That whole idea is kind of why you have inter-process communication or (IPC).

        Note that a process is not the same as a program by it’s definition. A program is essentially just code while a process is a program being executed. So the process would exit, not the the program

        http://stackoverflow.com/questions/2975831/is-leaked-memory-freed-up-when-the-program-exits

    1. (Laying down & on phone right now so can’t double check, BUT)

      delete[] myRectRegion[i];

      When I tried the equivalent of mine, gcc on Linux would spit out core dump segmentation fault. Was fixed after troubleshooting on stackoverflow and removing the [] directly following delete inside the loop.

        1. What does your code look like? Do you have a link to the stackoverflow question? I’m more of a C person, but it could be that you didn’t initialize it properly. Without seeing the code, that’s my best guess.

          Could also be a difference in compiler.

  3. I don’t understand a word people above talking but congrats on finishing your final and hope you’ll get good result on it..

  4. :3 i know that feeling

    as profesional programer , here my advice…
    1. focus make web based apps
    2. dont ever write your own design code, search “admin page template”
    3. less than 1 second load page, is must… use $(“#id”).load(‘url’); instead href=’url’
    4. set your standar not just “acceptable” but “could be sold”

    currently iam writing essay for final project, (semester 7)… 90% student here, can’t even write a code :v

    learn this (basic) -> html, php, mysql basic, css, javascript
    learn this advance -> mysql advance, jquery, responsive apps

    good luck

    #P.S
    still waiting next chapter “Cheat Receptionist”

    1. This is the worst advice I’ve ever read. Should have expected it would come from a web dev though, can’t blame em.

      1. Yeah… i agree. If your going that route your going to be missing out on a lot of concepts and skill. I mean come on at least learn SASS instead of CSS. At least with SASS it’s actually programmable.

  5. Sorry but I think the for cycle you are using is the best way to delete the array of pointers. Can’t give you much advice since I had to use C and not C++ and linked lists or binary trees lol. So I often worked with dynamic lists instead of static ones. “For” is the correct solution for static lists I think.

  6. Took a quick look at your code and FYI your using headers incorrectly. Headers should only have declaration and have no implementation inside it.

    http://www.learncpp.com/cpp-tutorial/19-header-files/

    So for example you should have about file called input.cpp with the implementation and input.cpp include the input.h. Your main driver should also include input.h.

    Also some of your code styling is inconsistent, but that isn’t too big of a issue since it was a group project. A lot of extra white spaces and inconsistent formatting. Styling is important though. If your not sure you should take a look at google’s styling guide as a reference.

    You should leave in debugging code even if its commented out.If your using linux take a look at the GDB command line bugger. Other then that there are debuggers built into a lot of IDE. Knowing how to use a real debugger is a really powerful tool as a software engineer.

    Sorry about being brutal on your code, but hope it helps you improve.

    If your looking to start on Data Structure, then start with arrays and linkedlist. After then it’s be queue and stacks. You should be able to related it to FIFO and LIFO. You should understand them conceptually and know how to implement them. You’ll also want to at least know how to add, remove and look at elements for each of them. That’s probably what you learn the first month of the class.

Leave a Reply