pointers to pointers, very scalable. take a looksee.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

pointers to pointers, very scalable. take a looksee.

Post by avansc »

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUM_ATTRIBUTE 3
enum attribute { name, lastname, number };

class snip
{
public:
    snip();
    void setAttribute(attribute attr, char *data);
    char *getAttribute(attribute attr);
private:
    char **data;
};

snip::snip()
{
    this->data = (char**)malloc(sizeof(char*));
}

void snip::setAttribute(attribute attr, char *data)
{
    this->data[attr] = (char*)malloc(sizeof(char)*strlen(data));
    strcpy(this->data[attr], data);
}

char *snip::getAttribute(attribute attr)
{
    return this->data[attr];
}


int main (int argc, char * const argv[])
{
    snip *temp = new snip();
    
    temp->setAttribute(name, "andre");
    temp->setAttribute(lastname, "van");
    temp->setAttribute(number, "1234");
    
    for(int a = 0;a < NUM_ATTRIBUTE;a++)
    {
        printf(":= %s\n", temp->getAttribute((attribute)a));
    }
    
    return 0;
}

if you have any questions on this please feel free to ask.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Spikey
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 98
Joined: Sat Dec 13, 2008 6:39 am
Programming Language of Choice: C++
Location: Ottawa, Canada
Contact:

Re: pointers to pointers, very scalable. take a looksee.

Post by Spikey »

That's pretty cool!
I have 1 question though, I notice you never free() memory in this example. So to prevent leaks, would you have to free() each attribute individually or would just freeing **data in the destructor suffice?
In any case, thanks for sharing.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: pointers to pointers, very scalable. take a looksee.

Post by avansc »

Spikey wrote:That's pretty cool!
I have 1 question though, I notice you never free() memory in this example. So to prevent leaks, would you have to free() each attribute individually or would just freeing **data in the destructor suffice?
In any case, thanks for sharing.
yeah you would have to free some stuff. im not sure exactly now. so i dont wanna tell you the wrong thing. let me do some research and get back to you.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: pointers to pointers, very scalable. take a looksee.

Post by wtetzner »

Spikey wrote:So to prevent leaks, would you have to free() each attribute individually or would just freeing **data in the destructor suffice?.
You would have to free each item in data, then free data.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
Post Reply