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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:23:04+00:00 2026-06-11T19:23:04+00:00

I’m experimenting with TDD and C. I’d like to write a simple malloc wrapper

  • 0

I’m experimenting with TDD and C. I’d like to write a simple malloc wrapper following a TDD approach. I’m trying to follow Bob Martin’s Three laws of TDD

  1. Do not write production code unless it is to make a failing unit test pass.
  2. Do not write more of a unit test than is sufficient to fail, and build failures are failures.
  3. Do not write more production code than is sufficient to pass the one failing unit test.

This is my code up until now:

    J_STATUS MemAlloc(long Size, void **OutPtr) {
        J_STATUS ReturnStatus;
        void *Ptr;

        Ptr = NULL;

        if (Size >= 0) {
            Ptr = malloc(Size);
            *OutPtr = Ptr;
            ReturnStatus = SUCCESS;
            //TODO controllare malloc error
        } else {
            ReturnStatus = ERROR;
        }
        return ReturnStatus;
    }

And these are my tests (I’m using Unity test framework):

    #include "../unity/unity.h"
    #include "../src/jMem.h"
    #include "stdlib.h"

    static int *ptr;
    static J_STATUS Result;
    static long Size;
    static long Count;

    void setUp() {
        ptr = NULL;
        Size = 10;
        Count = 5;
    }

    void tearDown() {
        if (ptr != NULL) {
            free(ptr);
        }
    }

    void test_MemAllocShouldAllocateMemoryAndReturnSuccess(void) {
        Result = MemAlloc(Size, (void **) &ptr);
        TEST_ASSERT_EQUAL(SUCCESS, Result);
        TEST_ASSERT_NOT_NULL(ptr);

    }

    void test_MemAllocShouldReturnErrorIfSizeIsNegative(void) {
        Size = -4;

        Result = MemAlloc(Size, (void **) &ptr);

        TEST_ASSERT_EQUAL(ERROR, Result);
    }

Now how do I write a test for a failing malloc? I know I can link a different version of malloc or I could write a macro to make malloc fail, but this should be true only for this test, while in the other I want to use the real malloc.

Any idea?
Thank you

  • 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-06-11T19:23:06+00:00Added an answer on June 11, 2026 at 7:23 pm

    To substitute malloc() you can use a function pointer like this:

    void* (*pMalloc)(size_t size)
    

    And then you use pMalloc(some size) instead of malloc(some size).

    If you set the pointer to point at malloc(), as in pMalloc = &malloc;, you’ll be using the real malloc(). If you set it to point at your own function, you’ll be using that function of yours.

    In your function you can simulate a malloc() failure by returning NULL. You can also call malloc() from it, if so is needed for the test.

    Similarly you can substitute your own free().

    In these substituted functions you can log what’s happening.

    For example, if you pass LONG_MAX to your wrapper function (I’m assuming it still takes the size as a long argument), your fake malloc() can log the size it has actually received. In the test you can then note that LONG_MAX has propagated from the wrapper to malloc() as (size_t)LONG_MAX, and if size_t is shorter than long (in terms of the number of bits) you can detect the discrepancy by comparing the values (LONG_MAX != (size_t)LONG_MAX).

    You can also log the pointer value that your fake malloc() returns (from inside of itself) and compare it with the value returned by your wrapper and see if these are different.

    You can elaborate the idea further to come up with more tests (e.g. the wrapper returns a non-NULL value, but the fake malloc() (and therefore the real one) isn’t called at all).

    While doing all this you don’t have to log to a file or to stdout. You can dedicate a data structure for the purpose and then examine it.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.