Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to convert C++ code to C and it was all fine until I got to the .push_back() function. After some research, I found out this wasn't a function in C. How do I write a function in C that replicates the uses of the .push_back() function?

What I have tried:

Researching how the .push_back() function works in the first place and then on my own trying to implement it only for it to fail.
Posted
Updated 24-Jan-23 7:39am

If you are using linux you can approximate a C++ vector using a dynamic memory buffer stream (aka a memstream).
Here's an example of how you might go about that:
C
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *ints = NULL;  /* a dynamic array of ints */
    size_t len = 0;    /* size in BYTES of ints */
    FILE *mem; /* in memory data structure */

    if( (mem = open_memstream( (char **)&ints, &len)) == NULL ) {
        /* handle error */
        return 1;
    }


    for(int i = 0; i < 10; ++i) {
        int n = i * 2;
        ssize_t written = fwrite(&n, sizeof n, 1, mem); /* "push_back" an int  */
        if(written != 1) {
            /* handle error */
        }
    }

    fflush(mem); /* needed to update ints buffer */

    size_t nints = len / sizeof(int);;
    for(int i = 0; i < nints; ++i) {
        printf("%d\n", ints[i]);
    }

    /* add some more to ints */

    for(int i = 0; i < 10; ++i) {
        int n = (100+i);
        size_t written = fwrite(&n, sizeof n, 1, mem); /* "push_back" an int  */
        if(written != 1) {
            /* handle error */
        }
    }


    fclose(mem); /* flushes memory to ints, closes file handle */
    /* "ints" is still valid here */
    nints = len/sizeof(int);
    for(int i = 0; i < nints; ++i) {
        printf("%d\n", ints[i]);
    }

    free(ints);  /* clean up ints array */

    return 0;
}
Next step might be to write a struct like
C
struct MEMDATA {
     char *data;       /* pointer to memory buffer */
     size_t data_size; /* buffer size in bytes */
     size_t mem_size;  /* size of one data element */
     FILE *fp;    
};
and write some functions that take a struct MEMDATA to create a new MEMDATA, push data, get the data pointer, etc.
 
Share this answer
 
v3
Comments
Chillzy 24-Jan-23 13:45pm    
Thanks for the help appreciate it!
You cannot do it easily, as this is a method of the STL vector class. So you would need to write a complete set of functions to manage whatever data you are trying to collect. Which all begs the question, why on earth are you trying to convert C++ to C?
 
Share this answer
 
Comments
Chillzy 24-Jan-23 12:30pm    
Currently using this website: https://learnopengl.com to get into graphics programming. I am currently on this article which uses the .push_back() function very often: https://learnopengl.com/Guest-Articles/2021/Tessellation/Height-map as you can see, all the code is in C++ and not C, which is a problem because I work in C, due to it being better than C++ (rather structs and unions than classes and templates and whatever bs C++ has to offer).
Richard MacCutchan 24-Jan-23 12:32pm    
Well the problem remains the same, you have to replicate all the STL classes that the code is using.
Quote:
ll the code is in C++ and not C, which is a problem because I work in C, due to it being better than C++ (rather structs and unions than classes and templates and whatever bs C++ has to offer).
Use C++ if you can.
C++ is multi-paradigm programming language and can be used as 'a better C' for structured programming.
On the other hand if you're really stubborn (or simply you cannot use C++, then you have to replicate at least a subset of the std::vector class subset. You may find sample code on the web, just Google for 'vector in C'.
 
Share this answer
 
Comments
Chillzy 24-Jan-23 13:46pm    
Thanks for the help I'll get to researching. (Still not using C++ though but still thanks)
CPallini 24-Jan-23 13:51pm    
You are welcome.
Recreating C++ facilities using C is more difficult than using C++ directly, in my opinion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900