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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:29:31+00:00 2026-05-27T23:29:31+00:00

I have a macro for calculating array sizes in my C code: #define sizearray(a)

  • 0

I have a macro for calculating array sizes in my C code:

#define sizearray(a)  (sizeof(a) / sizeof((a)[0]))

When I test it, it works fine for statically defined arrays, but not so for dynamically defined arrays (see below). Not sure I understand why this is the case. Is there any way of calculating the size of an array allocated on the heap?

/* test sizearray macro */
void testSIZEARRAY(void)
{
    /* test case for statically defined array */
    int a[5] = {0,0,0,0,0};

    assert(sizearray(a) == 5);

    /* test case for dynamically defined array */
    int *b;

    b = calloc(5, sizeof(int));

    assert(sizearray(b) == 5);

    free(b);
}
  • 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-27T23:29:32+00:00Added an answer on May 27, 2026 at 11:29 pm

    The answer is no, there is no standard way to get the size of a dynamically allocated array.
    For all practical purposes, you have to keep track it yourself.

    However, there are some compiler-specific methods that do that:

    Windows (Visual Studio): _msize()

    GCC: msize() Can’t find it in the GCC docs…

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

Sidebar

Related Questions

I have a macro defined in C something like this: #define SOME_FIELD(_A_,_B_,_C_) \ MyObj[
I have a macro like this: #define SHOW_EXPR(x) printf (%s=%d\n, #x, (x)) It works:
I have this macro code, which allows me to define both a C enum
I have a macro in the catch section of code, say #define CATCH( doSomething
If you have a macro like this in C: #define SLICE_VERSION 20110614 How to
I have found this macro while digging in the source code of tweejump game.
I have macro like this: #define error_exit(format, ...) \ error_at_line(EXIT_FAILURE, 0, __FILE__, __LINE__, format,
I have a macro that looks like this: #define coutError if (VERBOSITY_SETTING >= VERBOSITY_ERROR)
I have this macro: #define kPaxZoneW 105.0 I want to define a subsequent macro
I have a macro I use for debugging. #define diagnostic_arg(message,...) fprintf(stderr,message,__VA_ARGS__) I've found that

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.