/[release]C++ code peices

Results 1 to 8 of 8
  1. #1
    Ace of Hearts Reimniess is offline
    Grand MasterRank
    Jul 2009 Join Date
    in your headLocation
    783Posts

    /[release]C++ code peices

    well while working on my mmo in C++ I had made up some classes and functions I figured maybe somone could use

    some of it is new functions and some is off the top of my head so no garentee's

    and yes I made them


    first off is my own improvision of the rand function that allows you to set the max return please note it is advised you start use the line


    srand((unsigned)time(0)); befor useing it or will be prone to patterns and that is not good

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <cstdlib> 
    #include <ctime> 
    
    using namespace std;
    
    int rand2(int div)
    {
    	int a = rand();
    	long int round = 0;
    	int rhold = RAND_MAX / div;
    	for(;;)
    	{
    		if(a > rhold * round && a < rhold * (round+1))
    		{
    			return round+1;
    		}
    		else
    		{
    			round++;
    		}
    
    	}
    }

    next off is my refrence file reader
    each file is a list of either int or multi char seperated by ','
    please note you need a , at the very end


    declare a memory type
    example:

    memory test;

    then you call load
    example:

    test.load("test", 3, false);

    this will load the name in the first arguement as <name>.ref where it till look for it in a data folder in the folder it is in

    second one is the number of ID's you want it to load

    third is true or false put false only if they are all numbers or will crash


    to call char arrays call for <memory name>.id[<number of the one you want to load>]

    to call a in <memory name>.gint[<number of the one you want to load>]


    Code:
    Removed Until made a little more user freindly 
    
    side note: some of your comments actualy caused me to look back at the code and though of yet an even better method of doing this


    the memory is good for use on single lists or entire groups of lists by use of multiple classes


    I was useing them to read refrence files for monster stats / names for my server files as well as attributes of most things in them and so far it works fast and accuratley without error as long as you make no error in it's use


    if you can use it good for you if not oh well please don't spam and I plan to release more later just these are all I had on hand at the moment I could remember exacty how worked




    also these are my original works just to restate that for skimmers <_<;
    Last edited by Reimniess; 23-10-09 at 05:13 PM.


  2. #2
    Grand Master BBim is offline
    Grand MasterRank
    Sep 2008 Join Date
    127.0.0.1Location
    1,110Posts

    Re: /[release]C++ code peices

    May we give suggestions?
    Read this for a better implementation of rand(): http://www.eternallyconfuzzled.com/a..._art_rand.aspx

    Instead of int gint[999]; you could use the stl vector(http://www.cplusplus.com/reference/stl/vector/) so you wont waste the unused memory.
    Instead of passing "void load(char list[99]", you should pass a pointer to const char, so it wont need to copy the string, you can also use std::string(http://www.cplusplus.com/reference/string/string/) instead of char array.
    You should also use variables as private and create a get method that will check if that value exists to avoid problems.

    Other then that it seems good :P

  3. #3
    Ace of Hearts Reimniess is offline
    Grand MasterRank
    Jul 2009 Join Date
    in your headLocation
    783Posts

    Re: /[release]C++ code peices

    Quote Originally Posted by BBim View Post
    May we give suggestions?
    Read this for a better implementation of rand(): http://www.eternallyconfuzzled.com/a..._art_rand.aspx

    Instead of int gint[999]; you could use the stl vector(http://www.cplusplus.com/reference/stl/vector/) so you wont waste the unused memory.
    Instead of passing "void load(char list[99]", you should pass a pointer to const char, so it wont need to copy the string, you can also use std::string(http://www.cplusplus.com/reference/string/string/) instead of char array.
    You should also use variables as private and create a get method that will check if that value exists to avoid problems.

    Other then that it seems good :P
    well they are what they are for a reason of them having other perposes in the program they were made for but if somone wants to modify it to those that is fine

  4. #4
    Grand Master Organic is offline
    Grand MasterRank
    May 2007 Join Date
    2,077Posts

    Re: /[release]C++ code peices

    Quote Originally Posted by Reimniess View Post
    well they are what they are for a reason of them having other perposes in the program they were made for but if somone wants to modify it to those that is fine
    How bout you take the goddamn criticism and use it to assist further works?

  5. #5
    Ace of Hearts Reimniess is offline
    Grand MasterRank
    Jul 2009 Join Date
    in your headLocation
    783Posts

    Re: /[release]C++ code peices

    Quote Originally Posted by Organic View Post
    How bout you take the goddamn criticism and use it to assist further works?
    I do appreciate critisizm but what he brought up I put like they are on perpose

  6. #6
    Grand Master GlaphanKing is offline
    Grand MasterRank
    Sep 2008 Join Date
    World of MorrowLocation
    2,593Posts

    Re: /[release]C++ code peices

    Since we are developers we will give criticism that is either end of the spectrum. Take it or leave it.

    I'm sure you wrote these without doing some heavy researching.

    If you plan on rewriting code that is already there, it is a good first attempt. I don't know the level of your coding but I do believe you should use the libraries already provided and accessible when using c++. Hundreds of libraries, like Allegro, boost, DirectX, .NET are there for a reason. They have taken the time to perfect the methods and functions.

    As for the memory writer. It has potential to be something. However, when messing with memory it is good practice to research a lot of stuff, like call stacks, buffer and buffer overflow, pointers and threading.

    C++ has the potential to manipulate memory in such a way that you can crash your system and overflow the stack so bad you will need to reformat. Personal quote from myself. Happened to me.


    But tell me? What is the purpose of these classes? are they for developing servers or just for anything?

  7. #7
    Grand Master BBim is offline
    Grand MasterRank
    Sep 2008 Join Date
    127.0.0.1Location
    1,110Posts

    Re: /[release]C++ code peices

    Quote Originally Posted by Reimniess View Post
    well they are what they are for a reason of them having other perposes in the program they were made for but if somone wants to modify it to those that is fine
    Well, sorry then, its cause I never saw anyone wanting to waste memory and other things, but if it was on purpose ok I guess.

    Next time I wait for you to answer the "May we give suggestions?".
    Last edited by BBim; 22-10-09 at 04:22 AM.

  8. #8
    Ace of Hearts Reimniess is offline
    Grand MasterRank
    Jul 2009 Join Date
    in your headLocation
    783Posts

    Re: /[release]C++ code peices

    Quote Originally Posted by BBim View Post
    Well, sorry then, its cause I never saw anyone wanting to waste memory and other things, but if it was on purpose ok I guess.

    Next time I wait for you to answer the "May we give suggestions?".
    yes you may and I had remembered I had not updated that ptograms function so yes indead it has some downfalls in it I would update it but in truth looking back at even the fully functional code I though of a way to prevent stack overflows so I;d rather get that done and out rather then release code that could cause problems to those who dont look at the code they use



Advertisement