Jump to content

Recommended Posts

This is another way of doing things instead of using objects as posted yesterday in http://forum.sourceboost.com/index.php?showtopic=5515

// file: students.h

#ifndef _STUDENTS_H_
#define _STUDENTS_H_

typedef unsigned char uchar;

#define STUDENTS    64

// S T U D E N T   D A T A   S T R U C T

typedef struct
{
    uchar s_id;                                 // id
    uchar s_age;                                // age
}Student;

extern Student StuList[STUDENTS];               // turn it global / not needed if we plan to use it only by the students.cpp module

#endif
// file: students.cpp

#include <system.h>
#include "students.h"

Student StuList[STUDENTS];                      // array of struct

void AddStudent( uchar *pstudent, uchar index ) // add student
{
    StuList[index].s_id  = *pstudent++;
    StuList[index].s_age = * pstudent;
}

void main()
{
    uchar adata[2];
     
    for( uchar i=0; i<STUDENTS; i++ )           // all 64
    {
        adata[0] = i+1;                         // id data to be stored
        adata[1] = i+1;                         // age data to be stored
        
        AddStudent( adata, i );                 // add_student
    }
}

The advantage of this approach is that the entire collection consumes 128 bytes instead of 192 bytes when objects is used.

 

br

Edited by joli

Share this post


Link to post
Share on other sites

A more elegant way to do the same...

// header file

#ifndef _STUDENTS_H_
#define _STUDENTS_H_

typedef unsigned char uchar;

#define STUDENTS    64

// S T U D E N T   D A T A   S T R U C T

struct Student
{
    uchar s_id;                                 // id
    uchar s_age;                                // age
    
    void AddStudent( uchar *pstudent )          // add function member
    {
        s_id  = *pstudent++;
        s_age = *pstudent;
    }
};

extern Student StuList[STUDENTS];               // turn it global if need

#endif
// c file

#include <system.h>
#include "students.h"

Student StuList[STUDENTS];

void main()
{
    uchar adata[2];
     
    for( uchar i=0; i<STUDENTS; i++ )       // all 64 elements
    {
        adata[0] = i+1;                     // data to be stored
        adata[1] = i+1;
        StuList[i].AddStudent( adata );
    }
}

br

Edited by joli

Share this post


Link to post
Share on other sites

I did an atempt to use this in a real application by using several lists of structures but the compiler boostc/bostc++, failed.
After several tests i concluded that the compiler do doesn't like to have "methodes" inside of the structure.
So, until better oppinion, i believe that this is a compiler issue.

 

Modifications:

1. delete AddStudent( uchar *pstudent ) "methode" inside of structure.

2. instead of StuList.AddStudent( adata ), StuList.s_id = data[0] and StuList.s_age = data[1] should be used.

 

 

br

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×