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

  • Home
  • SEARCH
  • 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 1080463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:02:06+00:00 2026-05-16T22:02:06+00:00

I am writing a dynamically growing string buffer. I have the following in a

  • 0

I am writing a dynamically growing string buffer. I have the following in a .c file.

#ifndef STRBUF_GROWTH_SIZE
#define STRBUF_GROWTH_SIZE 4096
#endif

My code uses this constant to do the reallocation of the buffer. Now in the tests, I need to set this value to a small one so that I can check the reallocation. I tried defining this in the tests.cpp (All tests are in C++ using UnitTest++).

#define STRBUF_GROWTH_SIZE 10
TEST(StringBuffer)
{
    struct strbuf *string = strbuf_init();

    strbuf_add(string, "first");
    CHECK_EQUAL("first", string->buffer);
    CHECK_EQUAL(5, string->length);
    CHECK_EQUAL(10, string->allocated);   /* this fails */

    strbuf_add(string, " second");
    CHECK_EQUAL("first second", string->buffer);
    CHECK_EQUAL(12, string->length);
    CHECK_EQUAL(20, string->allocated); /* this fails */

    strbuf_destroy(string);
}

I am wondering why the value didn’t change to 10? How can I workaround this problem?

Any thoughts?

  • 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-16T22:02:08+00:00Added an answer on May 16, 2026 at 10:02 pm

    Preprocessing is done on a source-file-by-source-file basis (well, not quite, but it’s close enough). A #define in one source file won’t affect anything in another source file.

    You’ll either need to #define this in a header, which you can swap out with another header during testing, or you’ll need to parameterise your library (e.g. strbuf_init(int growth_size).

    Alternatively, why don’t you just test with strings that are of length-4096? Then you’ll be testing your actual production code.

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

Sidebar

Related Questions

I am writing a container framework that can dynamically deploy a Jar file containing
I'm writing some code to move dynamically an element on my page. If i
I am writing a some code that to dynamically include an HTML snippet in
For an application I am writing, I have the need to dynamically load content
I have a small WCF hosting engine that I am writing that will dynamically
I'm writing code this week to dynamically change the color of images. As long
I'm writing a program consisting of dynamically created panels that each have a few
I'm writing a Wordpress plugin which dynamically creates a string. I want to insert
I was writing a Python Code that creates dictionaries dynamically,initializes it to a reference
I'm writing a WIX script. I have a custom-action that adds rows dynamically to

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.