Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3796156
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:11:34+00:00 2026-05-19T13:11:34+00:00

In the days before c++ and vector/lists, how did they expand the size of

  • 0

In the days before c++ and vector/lists, how did they expand the size of arrays when they needed to store more data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:11:35+00:00Added an answer on May 19, 2026 at 1:11 pm

    Vector and list aren’t conceptually tied to C++. Similar structures can be implemented in C, just the syntax (and error handling) would look different. For example LodePNG implements a dynamic array with functionality very similar to that of std::vector. A sample usage looks like:

    uivector v = {};
    uivector_push_back(&v, 1);
    uivector_push_back(&v, 42);
    for(size_t i = 0; i < v.size; ++i)
        printf("%d\n", v.data[i]);
    uivector_cleanup(&v);
    

    As can be seen the usage is somewhat verbose and the code needs to be duplicated to support different types.

    nothings/stb gives a simpler implementation that works with any types:

    double *v = 0;
    arrpush(v, 1.0);
    arrpush(v, 42.0);
    for(int i = 0; i < arrlen(v); ++i)
        printf("%g\n", v[i]);
    arrfree(v);
    

    It also provides hash maps, and the trick it uses for type-safe containers in C can be applied to other generic containers too.

    Any of these methods can expand the underlying storage either by a call to realloc (see below), or by allocating new storage with malloc and freeing the old one with free — which is equivalent to how std::vector grows its memory in C++.


    A lot of C code, however, resorts to managing the memory directly with realloc:

    void* newMem = realloc(oldMem, newSize);
    if(!newMem) {
        // handle error
    }
    oldMem = newMem;
    

    Note that realloc returns null in case of failure, yet the old memory is still valid. In such a situation this common (and incorrect) usage leaks memory:

    oldMem = realloc(oldMem, newSize);
    if(!oldMem) {
        // handle error
    }
    

    Compared to std::vector and the C equivalents from above, the simple realloc method does not provide O(1) amortized guarantee, even though realloc may sometimes be more efficient if it happens to avoid moving the memory around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm debugging my application using my ipod touch. Two days before also I did
Back in the days before Visual Studio Web Server we would host our local
Scenario: Change a member to ABC 60 days before anniversary date Given Repeat When+Then
Hi everyone I am in my last three days before I present my final
I'm doing an agent to send late in notification for 2 days before today
In xquery, How to get 90 days before date from a given date? Say
Before 2 days there was a automatic update for wordpress, which I for my
Two days ago I spontaneously bought myself a domain. The day before that, I
So this is as far as I got. Before I spend days throwing darts
In the days before a release we'd like to be able to prevent developers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.