Thread: Dynamic Memory Allocation - A Simple DOS Text Game

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    5

    Dynamic Memory Allocation - A Simple DOS Text Game

    I am using DEV-C++ 5 on Windows, XP first off. Now, in general, I know how a video game works. You need an engine to govern the game, maps in order to create the worlds, you need scenario data in order to control events in the game, you need entities like characters, items, and enemies, you need a battle system, and most importantly, you need something to clean up memory after the program. All this is put into a loop and it makes a video game.

    Right now I am in a process of creating an engine, seeing as a "blank" engine can be used to make an array of different games, that is what I am working on. I am currently learning how to apply graphics and layers, so for the time being I am starting a text based video game, and then I will improve the engine from there.

    My engine will be used to make RPG type games, and the current piece of code I am working on is classically known as the "shop" menu. I have inventory slots made, and currently I cannot sell items, but I am halfway done with finishing the code for buying items. In summary what the code currently does is as follows. You can open the shop menu and then choose "buy". The program will generate a list with 16 "slots", each slot being an integer. The items are also integers, and have a set value, as to indicate a set numeric code that lets the program determine the different stats for each item.

    Example:
    Code:
    //Slots do not have a set value, they are manipulated throughout the game. int slot1; int slot2; int slot3; //Items have a set integer to be used as an item code. int daggar = 1; int mace = 2;


    Before the do while loop inclosing the code for the shop starts, the different slots are assigned a value. Then when the shop code runs the value of the slots are compared to the values of the different items. When the computer finds a match, it will then display the stats of the item indicated by the item code. This is done by comparing one slot value at a time to each item value using if and else if statements, with the else statement naturally being a user input error message.

    Example:
    Code:
    slot1 = 3; if(slot1 == 1) { //Display Dagger Stats } else if(slot2 == 1) { //Display Mace Stats } else if(slot3 == 1) { //Display Helm Stats }


    This code is then wrapped into a series of
    #define statements and made into a single command. Then the game will populate your list with items and item stats according to the item code, your gold, and then prompts for user input. The item list is numbered, and the user inputs the number that correlates to the item they desire to buy. The program then checks the slot values again, compares them to the item code, checks to make sure you have enough gold, deducts your gold, adds the item, and then, if you are not buying potions, sets a switch to make sure you cannot have the same item twice (Personal preference for a small game.).

    Example:
    Code:
    (Populate list. Prompt for input.) (User input of 2) if(input == "1") { //Buy Item in Slot 1 } else if(input == "2" ) { //Buy Item in Slot 2 }
    Now, my problem, basically, is that I am using too much memory to keep track of these lists, I believe the memory it takes to store, compare and keep track of the slots is what has become my problem, because when I remove the code for actually buying the items (Not populating the list that works fine.), the program will compile. Now that I have explained how my item system works, I can ask my question in a manner that makes sense. I will go down the list with two different snips and bits of my code first.

    "itemhandler.h"

    Code:
    #define BUY1 if(slot1 == 1){ //Buy Dagger } else if(slot1 == 2)... (21 More if statements checking slot1) #define BUY2 if(slot2 == 1){ //Buy Dagger } else if(slot2 == 2)... (21 More if statements checking slot2) #define BUY3 if(slot3 == 1){ //Buy Dagger } else if(slot3 == 2)... (21 More if statements checking slot3) //Ect. ect. (Goes up to BUY16)
    Then in order to implement the code to check the items and buy them
    "main.cpp"

    Code:
    #include "itemhandler.h" //Other #include's //... string input; //..... slot1 = 3; slot2 = 7; slot3 = 11; //Ect. ect. until values have been set for integers slot1 - slot16 flag3 = 1; do { //Ask user to buy, sell, or cancel. Assuming user chooses to buy, then populate the list, show gold and wait for user input. cin>>input; if(input == "1") { BUY1 } else if(input == "2") { BUY2 } else if(input == "3") { BUY3 } //Ect. ect. List repeats pattern all the way down to BUY16 //More stuff, if user chooses to exit flag = 0 }while (flag == 1);
    As you can imagine, even just #define BUY1 alone is going to be a lengthy line of code, seeing as there are 23 different if statements that's used to check it. The idea behind this system is that when I get done, all I will have to do is plug the desired item integer values into the list, and the game will automatically run through every item possibility and display and keep track of the items. That way all I have to do in each world is put in the lines of code for the shop and plug in my numbers to set the different selections each "shop keeper" offers.

    !!!!! NOW HERE IS MY PROBLEM AND QUESTION !!!!

    When I place the code I have summarized in the previous snips of itemhandler.h and main.cpp into the program, the compiler will not make an executable. Instead I get the following error.

    out of memory allocating 65536 bytes
    [Build Error] [main.o] Error 1

    Now, from what I have researched, something needs to go on its own block of memory, and then it has to be freed when we are done with the new memory block, but I am not sure if free(); can be used with integers. If I use something like this I also get an error message.

    Example:

    Code:
    if(input == "1") { slot1 = malloc(sizeof(slot1)); BUY1 free(slot1); }
    The error message ends up as so...

    (The lines specified below containing errors reference the lines in the code above, not the lines in the original code. I did this to be sure you can specify which commands the errors are assigned to.)

    Line 4 |||| invalid conversion from (void*) to (int)
    Line 6 |||| invalid conversion from (int) to (void*)

    Am I using malloc() right? Should I use malloc() at all? Should I use calloc()? Is there a different, simpler way to clear memory? Would my slots actually be the problem as I have assumed, or could it be something else I am storing?

    Any information on getting this code to work is greatly appreciated! Thank you in advanced!

    - @pavil

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Right now I am in a process of creating an engine, seeing as a "blank" engine can be used to make an array of different games, that is what I am working on. I am currently learning how to apply graphics and layers, so for the time being I am starting a text based video game, and then I will improve the engine from there.
    A text based game would have zero impact on anything like a game engine which usually have cameras and rendering and sound to deal with. You are wasting your time.

    Now, my problem, basically, is that I am using too much memory to keep track of these lists, I believe the memory it takes to store, compare and keep track of the slots is what has become my problem, because when I remove the code for actually buying the items (Not populating the list that works fine.)
    Then don't do it.

    Make a game that is smart enough to only instantiate what the player loots or buys. Keep the game's entire inventory in a file or a database and refer to that when you need to add something to the player's inventory. It's the same for any chests or bags of holding the player may have -- if it's not on the player's person, the game only needs to know that it is a thing that exists while the player isn't using it.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I would rarely need to complain about an excess of information.

    *shrug*

    Anyway, it seems as if your compiler is crashing from a lack of memory. Changing a few variables shouldn't alter the outcome.

    How much memory do you have available before building?

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    5
    White flags, doesn't that defeat the purpose of being able to plug in numbers for different results? I wanted to avoid having pre-set item lists, and having to edit different parameters for each shop. I just want to be able to implement the code, and plug and chug the numbers. Storing that in a file and then having the program find the info and import it as usable data, defeats that purpose, I might as well just make a list of items set in stone for each shop. During the game, there will be times when you go back to town, and after setting off different flags the game will trigger different items codes to be plugged into the shop code, to make it seem as though they were buying and trading items with other people while you were gone, as to keep the item selections fresh. Besides, even if I did create a file, lets say a .con file using my Notepad program to write the file and save it with the .con extension, all that data still has to be imported and assigned to characters, strings, ints, ect. So in the end, all that data I would import to run the shop, would have to be cleared anyway wouldn't it? I could be making a dummy out of myself didn't sleep last night, and here it is tonight again around 3 AM so if I catch myself looking over something you guys are saying, tomorrow I will post and let u guys know.

    And phantomotap, are you asking for how much memory the compiler is restricted from exceeding, or are you asking how much memory my computer itself has?
    Last edited by pavil; 05-10-2014 at 04:49 AM.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    5
    And btw whiteflags, I will be adding graphics and such later, my cousin is a programmer and he told me just start with this and then build on it. So it's not like this is the final set in stone release. All I am looking to do here guys is get tips on freeing memory, I know the code could probably be improved a lot, I know that there is always a technically this, technically that, but not everyone on here is an expert, I am not looking for "you should do this" or "you should do that" I just want to know how to clear memory for the system I already have going for myself. If it's not possible, I can take a logical explanation, but I know it's possible, I am just looking for tips about my current code, not tips on how should be changing my code. I am not trying to be rude guys, there is a method to my madness, also, I do not claim to be an amazing programmer, so please be patient.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    White flags, doesn't that defeat the purpose of being able to plug in numbers for different results?
    No.

    I wanted to avoid having pre-set item lists, and having to edit different parameters for each shop.
    If you have a bunch of slots and magic numbers as a master item list, your form factor of choice does not stop it from being a master item list.

    During the game, there will be times when you go back to town, and after setting off different flags the game will trigger different items codes to be plugged into the shop code, to make it seem as though they were buying and trading items with other people while you were gone, as to keep the item selections fresh.
    Again, this is the behavior of NPCs or other people, it has nothing to do with the representation of items.
    Storing that in a file and then having the program find the info and import it as usable data, defeats that purpose, I might as well just make a list of items set in stone for each shop.
    That's a particularly lazy way of looking at the solution to the problem. Is the logic of how your shops' inventories change really that closely coupled to how the items are represented? Why? You really would have to explain to me why this is working the way it apparently does.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    And phantomotap, are you asking for how much memory the compiler is restricted from exceeding, or are you asking how much memory my computer itself has?
    O_o

    According to your original post, the compiler itself is crashing while trying to produce an executable. I've only ever seen that happen in severely memory limited situations. The "allocation" of memory for your program doesn't exist until the program is executed, but you can't execute your program because the compiler crashed. If that is your problem, changing to dynamically allocate the variables isn't the solution.

    White flags, doesn't that defeat [...] keep the item selections fresh.
    Every aspect of your understanding with regards to whiteflags comment is completely wrong.

    You are effectively "hardcoding" several limits into your "engine". Look at the example you gave: the value 1 is a dagger for the given example store. That relationship between the store, the dagger, and the value is all added to your code.

    The suggestion by whiteflags is to transform that information so that it may be read from a file during program execution never appearing in source code.

    Code:
    FILE * sData = fopen("stores.dat", "rb");
    /* ... */
    struct StoreInventory * sInventory = engine_craftinventoryfromfile(sData, "store id");
    /* ... */
    /* use sInventory->slot[1] and similar */
    All I am looking to do here guys is get tips on freeing memory, I know the code could probably be improved a lot, I know that there is always a technically this, technically that, but not everyone on here is an expert, I am not looking for "you should do this" or "you should do that" I just want to know how to clear memory for the system I already have going for myself.
    The compiler is crashing. You could add code to "clear memory for the system you already have", yet the compiler will still crash.

    So, one thing at a time. Let's find out why the compiler is crashing. Until the code compilers, you can't confirm that any code you change is working.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  8. #8
    Registered User
    Join Date
    Dec 2013
    Posts
    5
    ok cool guys this is my second night in a row being up, so bare with me, thank you for your patience thus far. So ok one step at a time. Now the compiler crashes when I add my code to buy items, when I take that out, the rest of the game runs fine. So now, if I am right, what you guys are telling me, is that by transferring my games inventory into a file, it is not hardcoded into the game, the game doesn't have to keep track of an item until its actually being used, it's only just going to know it exists until called upon. So this, in return, correct me if I am wrong, will cut down on the amount of info the program will have to remember at one time. Now before I get off to the races with my programing, let me run an example of what I am going to try by you guys, and you tell me if I got it.

    So first, I would have to cut the code that is causing the compiler to crash, that way I can compile and know if my changes are working without any error. then I would have to trade all my items and item stats into a file. I know at this point, the items don't have to be integers, or floats, or anything like that, The item is just named, and then you would enter in different numbers behind it for your different stats. Then from there I can have the program open the file for reading, and go to the line in the file with item I am looking for, skip so many characters over, and take the numbers, one at a time, into usable information for the program, from there the file would be rewound and then closed. That way the info the program seeks is only used and remembered when needed. That would also clear up having to have multiple integers for nearly the same thing. Instead of macedmg = 8; and bowdamage=7; I could have it look up the value of the damage for either or in the file and use it to modify one integer, just called weapon damage.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is there a reason you are using a stone age compiler?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Is there a reason you are using a stone age compiler?
    O_o

    Well, "Dev-C++" is just an IDE. You are right though to point out the possibility of him using "Bloodshed Dev-C++".

    *shrug*

    For that matter, "Windows XP" is official deceased so even using it as a development machine is a painful indicator.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by phantomotap View Post
    Well, "Dev-C++" is just an IDE. You are right though to point out the possibility of him using "Bloodshed Dev-C++".
    Right, but I assume that the OP has not replaced the out-of-the-box compiler that ships with the IDE.
    This also sounds ominous:
    >>out of memory allocating 65536 bytes

    Right you are about XP too, but people are pretty stubborn and refuse to upgrade the OS...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Right, but I assume that the OP has not replaced the out-of-the-box compiler that ships with the IDE.
    O_o

    I agree that changing the shipped compiler is unlikely, but my reference to him using "Bloodshed Dev-C++" is the relevant bit of my comment.

    The person/group responsible for the fork "Orwell Dev-C++" ships later versions of the "MinGW" port of "GCC"--currently "4.8.1".

    This also sounds ominous:
    Indeed. I fear our original poster may having something else slurping memory.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  13. #13
    Registered User
    Join Date
    Dec 2013
    Posts
    5
    I have tried compiling on other computers same results and my memory diagnostics tools read my memory is running at its, ideal capacity

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    my memory diagnostics tools read my memory is running at its, ideal capacity
    O_o

    Who are you trying to fool?

    Does your compiler produces the error as in your original post? If it doesn't,we need to know what is actually occuring on your side. If it does, what your "memory diagnostics tools" say about "ideal capacity" isn't remotely relevant.

    We can't help with insufficient informant, and I am not even going to try.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  15. #15
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    my memory diagnostics tools read my memory is running at its, ideal capacity
    O_o

    Who are you trying to fool?

    Does your compiler produces the error as in your original post? If it doesn't,we need to know what is actually occuring on your side. If it does, what your "memory diagnostics tools" say about "ideal capacity" isn't remotely relevant.

    We can't help with insufficient informant, and I am not even going to try.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory Allocation
    By exclusive in forum C Programming
    Replies: 4
    Last Post: 11-10-2011, 07:16 AM
  2. dynamic memory allocation
    By silver_comet in forum C Programming
    Replies: 2
    Last Post: 05-30-2011, 12:05 PM
  3. what is dynamic memory allocation
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 06-08-2009, 08:11 AM
  4. Memory dynamic allocation
    By elvio.vicosa in forum C Programming
    Replies: 8
    Last Post: 10-16-2007, 03:30 AM
  5. dynamic memory allocation
    By inquisitive in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2004, 02:07 AM

Tags for this Thread