This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

[TUTORIAL] Lists of Dynamically Allocated Variables


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 09-28-2013 , 12:45   [TUTORIAL] Lists of Dynamically Allocated Variables
Reply With Quote #1


Lists of dynamically allocated variables



There is a very util file named sh_list.h that helps you to keep many dynamically allocated variables into lists.
I find this file or, more exactly, this class very util, and, I think it's hard enough to get learnt by yourself about it's purpose.

Here you can see some of it's features.


PHP Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "CString.h"
#include "sh_list.h"

class ExtraItem
{
public:
  
String pName;
  
String pInformation;

  
size_t Index;
  
size_t Cost;
};

List <
ExtraItem *> g_ExtraItems;

size_t g_Jetpack 0U;

static 
size_t RegisterExtraItem(const char pName, const char pInformation, const size_t Cost)
{
  static 
ExtraItem pExtraItem NULL;

  
pExtraItem = new ExtraItem;

  
assert(pExtraItem);

  
pExtraItem->pName = new String;
  
pExtraItem->pInformation = new String;

  
pExtraItem->pName->assign(pName);
  
pExtraItem->pInformation->assign(pInformation);

  
pExtraItem->Index g_ExtraItems.size() + 1U;
  
pExtraItem->Cost Cost;

  
g_ExtraItems.push_back(pExtraItem);

  return 
pExtraItem->Index;
}

static 
void FreeAllExtraItems(void)
{
  static List <
ExtraItem *> ::iterator ExtraItem NULL;

  for (
ExtraItem g_ExtraItems.begin(); ExtraItem != g_ExtraItems.end(); ExtraItem++)
  {
    
delete (*ExtraItem)->pName;
    
delete (*ExtraItem)->pInformation;

    
delete (*ExtraItem);
  }

  
g_ExtraItems.clear();
}

static 
ExtraItem GetExtraItemByIndex(size_t Index)
{
  static List <
ExtraItem *> ::iterator ExtraItem NULL;

  for (
ExtraItem g_ExtraItems.begin(); ExtraItem != g_ExtraItems.end(); ExtraItem++)
  {
    if ((*
ExtraItem)->Index == Index)
    {
      return (*
ExtraItem);
    }
  }

  return 
NULL;
}

int main(void)
{
  
g_Jetpack RegisterExtraItem("Jetpack""Allows you to fly."32U);

  
ExtraItem pExtraItem GetExtraItemByIndex(g_Jetpack);

  
assert(pExtraItem);

  
printf("Jetpack has an information that equals to '%s'.\n"pExtraItem->pInformation->c_str());

  
FreeAllExtraItems();

  return 
0U;



Also, you may declare into classes, functions like listed below.

PHP Code:
class Task
{
public:
  
size_t Handler(String pInformationsize_t Indexfloat Radius);
  
size_t Repetitions;
}; 

Attached Files
File Type: zip CString + List.zip (4.1 KB, 63 views)
__________________

Last edited by claudiuhks; 09-30-2013 at 19:59.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:23.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode