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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:06:08+00:00 2026-05-22T01:06:08+00:00

gprof is not working properly on my system (MinGW) so I’d like to know

  • 0

gprof is not working properly on my system (MinGW) so I’d like to know which one of the following snippets is more efficient, on average.

I’m aware that internally C compilers convert everything into pointers arithmetic, but nevertheless I’d like to know if any of the following snippets has any significant advantage over the others.

The array has been allocated dynamically in contiguous memory as 1d array and may be re-allocated at run time (its for a simple board game, in which the player is allowed to re-define the board’s size, as often as he wants to).

Please note that i & j must get calculated and passed into the function set_cell() in every loop iteration (gridType is a simple struct with a few ints and a pointer to another cell struct).

Thanks in advance!

Allocate memory

grid = calloc( (nrows * ncols), sizeof(gridType) );

Snippet #1 (parse sequentially as 1D)

gridType *gp = grid;
register int i=0 ,j=0;      // we need to pass those in set_cell()

if ( !grid )
return;

for (gp=grid; gp < grid+(nrows*ncols); gp++)
{
    set_cell( gp, i, j, !G_OPENED, !G_FOUND, value, NULL );

    if (j == ncols-1) {     // last col of current row has been reached
        j=0;
        i++;
    }
    else                    // last col of current row has NOT been reached
        j++;
}

Snippet #2 (parse as 2D array, using pointers only)

gridType *gp1, *gp2;

if ( !grid )
    return;

for (gp1=grid; gp1 < grid+nrows; gp1+=ncols)
    for (gp2=gp1; gp2 < gp1+ncols; gp2++)
        set_cell( gp2, (gp1-grid), (gp2-gp1), !G_OPENED, !G_FOUND, value, NULL );

Snippet #3 (parse as 2D, using counters only)

register int i,j;           // we need to pass those in set_cell()

for (i=0; i<nrows; i++)
    for (j=0; j<ncols; j++)
        set_cell( &grid[i * ncols + j], i, j, !G_OPENED, !G_FOUND, value, NULL);

Free memory

free( grid );

EDIT:
I fixed #2 form gp1++) to gp1+=ncols), in the 1st loop, after Paul’s correction (thx!)

  • 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-22T01:06:09+00:00Added an answer on May 22, 2026 at 1:06 am

    This is the way I’d write it. IMHO it’s shorter, clearer and simpler than any of your ways.

    int i, j;
    gridType *gp = grid;
    
    for (i = 0; i < nrows; i++)
        for (j = 0; j < ncols; j++)
            set_cell( gp++, i, j, !G_OPENED, !G_FOUND, value, NULL );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know what this error means for gprof ? I'm running gcc -pg
I ran gprof on a C++ program that took 16.637s , according to time()
How do Window's programmers profile their native C++ code? On Unix/Linux you have gprof
The application is compiled with -O0 -g -pg and gprof is run with its
I'm using boost singletons (from serialization). For example, there are some classes which inherit
I was looking for a way of finding statically unreachable functions in a (very)
The below mentioned in the GCC Page for the function sections and data sections
I have downloaded Strawberry-Perl-5.12.1.0 for Windows and was using it for many days. Today
I am devolping a console math intensive application on Mac OS X. It's implemented
I am currently stumbled into this problem and I'd love to hear some suggestions

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.