C++ vector function implentation in C

Monado_III

Well-Known Member
OP
Member
Joined
Feb 8, 2015
Messages
722
Trophies
0
Location
/dev/null
XP
1,443
Country
Canada
I'm rewriting a program that uses several C++ exclusive functions and operators, such as new, vectors and vector functions (begin, end, push_back, at). I'm trying to implant the program in straight C and I'm not sure how to replace the vector functions (the vectors themselves I replaced with arrays), I've replaced the new with a malloc and push_back with realloc. Would that be the right way to do it? and How would I replace those other functions?
 

spoonm

Can count to 3.
Member
Joined
May 20, 2015
Messages
192
Trophies
0
Website
spoonm.org
XP
317
Country
Brazil
I'm not familiar with vectors or C++ overall, but have you tried implementing other data structures? AFAIK, the vectors you're talking about are "dynamic arrays", lists, to which you can add or remove any element of the list type, right?

With linked lists in C, you can implement a data structure like this:

NULL <-> some node structs <-> NULL
some node structs -> NULL
NULL <- node *start <-> some node structs <-> node *end -> NULL

They can have a single link(meaning you'll navigate through it from one end, always going forward or backwards), two links(meaning you can navigate forwards and backwards), or n links(at which point you have something more complex than just a list).

Here is a double linked list implementation, made with "sentinel nodes", or node *start and node *end, that both don't really matter, but have the same memory fingerprint as a regular data-filled node:
Code:
C
#include <stdlib.h>

typedef struct list list_type;
typedef struct node node_type;

struct list
{
    node *start, *end;
    unsigned int length;
};

struct node
{
    node *next, *prev;
    data_type data;
};

int
create_list(list_type *l)
{
    if ((l->start = malloc(sizeof (node_type))) == NULL ||
        (l->end   = malloc(sizeof (node_type))) == NULL)
    {
        perror("Error allocating memory");

        /* Handle the error your way. This time I just return a non-zero value. */

        return 1;
    }

    /* You have to link the sentinel nodes, now. */
    l->start->prev = l->end->next = NULL;
    l->start->next = l->end;
    l->end->prev = l->start;

    l->length = 0;

    return 0;
}

int
insert_start(list_type *l, data_type dat)
{
    node_type *new;

    if ((new = malloc(sizeof (node_type))) == NULL)
    {
        perror("Error allocating memory for new node");

        /* Handle the error. This time I just return a non-zero value. */

        return 1;
    }

    /*
     * You have to copy the data into the new node. There are ways to do this if your data type
     * Is more complex than a basic data type or struct where you can simply make a shallow copy.
     */
    new->data = dat;

    /* Now change the list. */
    new->next = l->start->next;
    new->prev = l->start;

    l->start->next->prev = new;
    l->start->next = new;

    l->length++;

    return 0;
}

/* Functions go on and on... */

You can take advantage of pointers and make your own functions to have the list behave the way you want it to. As for the data type, you can use preprocessor directives to create templates. I did so for my data structures class months ago, but my templates sucked big time.

Here's a link to the repo where I put the template-y stuff I made: https://github.com/skewerr/dtstruct
 

Monado_III

Well-Known Member
OP
Member
Joined
Feb 8, 2015
Messages
722
Trophies
0
Location
/dev/null
XP
1,443
Country
Canada
I'm not familiar with vectors or C++ overall, but have you tried implementing other data structures? AFAIK, the vectors you're talking about are "dynamic arrays", lists, to which you can add or remove any element of the list type, right?

With linked lists in C, you can implement a data structure like this:

NULL <-> some node structs <-> NULL
some node structs -> NULL
NULL <- node *start <-> some node structs <-> node *end -> NULL

They can have a single link(meaning you'll navigate through it from one end, always going forward or backwards), two links(meaning you can navigate forwards and backwards), or n links(at which point you have something more complex than just a list).

Here is a double linked list implementation, made with "sentinel nodes", or node *start and node *end, that both don't really matter, but have the same memory fingerprint as a regular data-filled node:
Code:
C
#include <stdlib.h>

typedef struct list list_type;
typedef struct node node_type;

struct list
{
    node *start, *end;
    unsigned int length;
};

struct node
{
    node *next, *prev;
    data_type data;
};

int
create_list(list_type *l)
{
    if ((l->start = malloc(sizeof (node_type))) == NULL ||
        (l->end   = malloc(sizeof (node_type))) == NULL)
    {
        perror("Error allocating memory");

        /* Handle the error your way. This time I just return a non-zero value. */

        return 1;
    }

    /* You have to link the sentinel nodes, now. */
    l->start->prev = l->end->next = NULL;
    l->start->next = l->end;
    l->end->prev = l->start;

    l->length = 0;

    return 0;
}

int
insert_start(list_type *l, data_type dat)
{
    node_type *new;

    if ((new = malloc(sizeof (node_type))) == NULL)
    {
        perror("Error allocating memory for new node");

        /* Handle the error. This time I just return a non-zero value. */

        return 1;
    }

    /*
     * You have to copy the data into the new node. There are ways to do this if your data type
     * Is more complex than a basic data type or struct where you can simply make a shallow copy.
     */
    new->data = dat;

    /* Now change the list. */
    new->next = l->start->next;
    new->prev = l->start;

    l->start->next->prev = new;
    l->start->next = new;

    l->length++;

    return 0;
}

/* Functions go on and on... */

You can take advantage of pointers and make your own functions to have the list behave the way you want it to. As for the data type, you can use preprocessor directives to create templates. I did so for my data structures class months ago, but my templates sucked big time.

Here's a link to the repo where I put the template-y stuff I made: https://github.com/skewerr/dtstruct
Ok, thanks for the help, I think I'll do that :)
 

spoonm

Can count to 3.
Member
Joined
May 20, 2015
Messages
192
Trophies
0
Website
spoonm.org
XP
317
Country
Brazil
C doesn't have a resource like "Vector<type>" to create a vector of a given type, but you can use preprocessor directives and a bit of fiddling to make something more generic. I think I failed to explain that in a way it'd be clear to you, so allow me to compensate: do you use Telegram? Skype? E-mail? If you're interested, I can help through those so I don't spam GBAtemp with impertinent posts. Instant messaging allows me to respond faster, and using e-mails allows me to reply to you when I'm not home.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
  • Julie_Pilgrim @ Julie_Pilgrim:
    im sure half the responses won't be literal racial slurs or "drop table" jokes
  • Veho @ Veho:
    Look, it's China. They know what it's like when you give a poll to half a billion trolls.
    +1
  • K3Nv2 @ K3Nv2:
    How much dollar do you think it is?
  • Veho @ Veho:
    ONE MILLION DOLLA
    +1
  • Veho @ Veho:
    I know the pricing of electronics nowadays isn't "how much it actually costs" but "how much we can get away with", but putting up a poll is just cynical.
    +1
  • K3Nv2 @ K3Nv2:
    Probably $150 someone said Anbernic said around the same price as rg556
  • Julie_Pilgrim @ Julie_Pilgrim:
    you know which game i wish they would rerelease
  • Julie_Pilgrim @ Julie_Pilgrim:
    sonic unleashed
  • K3Nv2 @ K3Nv2:
    Make it a happy meal toy
  • Julie_Pilgrim @ Julie_Pilgrim:
    that game's engine is really fucking intensive so it runs like literal shit on xbox 360 and ps3
  • Veho @ Veho:
    Nah I'm getting value creep again. I look at a $50 console "but for just a few more dollars you could get XYZ" and I end up considering the Steam Deck.
    +1
  • Julie_Pilgrim @ Julie_Pilgrim:
    like the lighting in that game was genuinely so good
  • Veho @ Veho:
    Not getting dragged into that again.
  • Julie_Pilgrim @ Julie_Pilgrim:
    i dont get why they didn't port the one game that ran the worst on consoles, to pc
  • Julie_Pilgrim @ Julie_Pilgrim:
    like you port everything to pc except the one game where it would make the most sense. why. what do you gain from this
  • Julie_Pilgrim @ Julie_Pilgrim:
    is sega just personally fucking with me? are they laughing while watching me through my kinect camera as i get up to restart my xbox for the third time because the game froze again
  • K3Nv2 @ K3Nv2:
    Buy handhelds from five below better quality
  • K3Nv2 @ K3Nv2:
    Valve probably going to do another refresh of the deck this fall with rog ally like specs tbh
    +1
  • Veho @ Veho:
    A smaller form factor would be nice too.
    +1
  • K3Nv2 @ K3Nv2:
    A shield portable 2 would be nice aye Nvidia
    +2
  • Psionic Roshambo @ Psionic Roshambo:
    The big leap in all things tech is when carbon based chips start hitting.
  • Psionic Roshambo @ Psionic Roshambo:
    Longer battery life cooler temps and faster! What's not to like lol (probably expensive as hell)
  • AncientBoi @ AncientBoi:
    [checks my dildo(s) batteries, coolant and lube] :O [promptly replaces them] :D
    AncientBoi @ AncientBoi: [checks my batteries, coolant and lube] :O [promptly replaces them] :D