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 825137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:12:25+00:00 2026-05-15T03:12:25+00:00

i’m developing an array structure just for fun. This structure, generalized by a template

  • 0

i’m developing an array structure just for fun.
This structure, generalized by a template parameter, pre allocates a given number of items at startup, then, if “busy” items are more than available ones, a function will realloc the inner buffer .
The testing code is :

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

template <typename T> struct darray_t {
    size_t items;
    size_t busy;
    T     *data;
};

#define DARRAY_REALLOC_ITEMS_STEP 10

#define da_size(da) (da)->busy

template <typename T>
void da_init( darray_t<T> *da, size_t prealloc ){
    da->items = prealloc;
    da->busy  = 0;
    da->data  = (T *)malloc( sizeof(T) * prealloc );
}

template <typename T> T *da_next( darray_t<T> *da ){
    if( da->busy >= da->items ){
        da->data   = (T *)realloc( da->data, sizeof(T) * DARRAY_REALLOC_ITEMS_STEP );
        da->items += DARRAY_REALLOC_ITEMS_STEP;
    }
    return &da->data[ da->busy++ ];
}

int main(){
    darray_t<int> vi;
    int *n;

    da_init( &vi, 100 );

    for( int i = 0; i < 101; ++i ){
        n = da_next(&vi);
        *n = i;
    }

    for( int i = 0; i < da_size(&vi); ++i ){
        if( vi.data[i] != i ){
            printf( "!!! %d != %d\n", i, vi.data[i] );
        }
    }

    return 0;
}

As you can see, i prealloc 100 integer pointers at the beginning and then i realloc them with 10 more pointers at time.
In the main function, i perform a for loop to check items integrity and, if an array item is not as i expect, i print its value and … you know what ?
I have the following message :

!!! 11 != 135121

In fact, item at index 11, that should be ’11’, is 135121 !!!! :S

Can you tell me if my code is not correct?

Thanks

NOTE
I perfectly know that mixing C and C++ this way is ugly, and i know too that this structure would screw up if used, for instance :

darray_t<std::string>

This is just a test for int pointers.

  • 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-15T03:12:26+00:00Added an answer on May 15, 2026 at 3:12 am

    realloc does not automatically grow the piece of memory – you’ll have to do that. Do e.g.:

    da->data=(T*)realloc(da->data, sizeof(T)*(da->items+DARRAY_REALLOC_ITEMS_STEP));
    

    (and you should handle realloc returning NULL)

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

Sidebar

Ask A Question

Stats

  • Questions 428k
  • Answers 428k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are sending the wrong type to the View() In… May 15, 2026 at 1:14 pm
  • Editorial Team
    Editorial Team added an answer The parts of the rewrite rule break down as follows:… May 15, 2026 at 1:14 pm
  • Editorial Team
    Editorial Team added an answer HTML <input type="text" id="test" name="test" value="1.23"> <div class='txt'></div> JQuery $('#test').next('.txt').text($('#test').val());… May 15, 2026 at 1:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.