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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:38:53+00:00 2026-05-18T08:38:53+00:00

Below is some code from a C++ procedure. The original can be found here;

  • 0

Below is some code from a C++ procedure. The original can be found here;
https://code.ros.org/trac/opencv/browser/trunk/opencv/modules/imgproc/src/grabcut.cpp

My perspective is that I am a C# rather than C++ programmer so this code is a bit of a mystery to me even though I ran it through a code converter and it came out unchanged.

My questions are: a). How does ‘coefs’ become an array, and b). how does ‘c’ get populated?

To give a bit more information: ‘coefs’ does not seem to start out as an array and ‘c’ somehow seems to magically get at least 8 items

    // fields in class called MAT:
    // a distance between successive rows in bytes; includes the gap if any 
    size_t step;
    // pointer to the data
    uchar* data;

    // where ptr comes from:
    template<typename _Tp> _Tp* ptr(int y=0);
    template<typename _Tp> inline _Tp* Mat::ptr(int y)
    {
        CV_DbgAssert( (unsigned)y < (unsigned)rows );
        return (_Tp*)(data + step*y);
    }    
    .....

    // original question code:
    static const int componentsCount = 5;
    Mat model;
    float* coefs;
    float* mean;
    float* cov;

    coefs = model.ptr<float>(0);
    mean = coefs + componentsCount;
    cov = mean + 3*componentsCount;

    for( int ci = 0; ci < componentsCount; ci++ )
        if( coefs[ci] > 0 )
            calcInverseCovAndDeterm( ci );

    void GMM::calcInverseCovAndDeterm( int ci )
    {
        if( coefs[ci] > 0 )
        {
            float *c = cov + 9*ci;
            float dtrm =
                covDeterms[ci] = c[0]*(c[4]*c[8]-c[5]*c[7]) - c[1]*(c[3]*c[8]-c[5]*c[6]) + c[2]*(c[3]*c[7]-c[4]*c[6]);
  • 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-18T08:38:54+00:00Added an answer on May 18, 2026 at 8:38 am

    When you declare an array like this:

    int my_ints[10];
    

    my_ints is an array. But in C++ my_ints can also be evaluated as a pointer to the first item in the array. So code like this:

    int* the_first_int = my_ints;
    

    …is legal and valid, not to mention very common. You can also use the_first_int as if it were also an array, like this:

    int the_third_int = the_first_int[2];
    

    …which makes it practically seamless to go between C-style arrays and pointers and back again.

    In your code you declare a pointer to a float:

    float* coefs;
    

    (by the way, this should be initialized to NULLL, but that’s another story), and then you set it to some value:

    coefs = model.ptr<float>(0);
    

    I don’t know what model.ptr<float>(0) does, but it probably either allocates an array and returns a pointer to that, or returns a pointer to an array that was already set up.

    Now because the coefs is a pointer to float, you can use it like it were an array, which you do in the loop:

    for( int ci = 0; ci < componentsCount; ci++ )
        if( coefs[ci] > 0 )
    

    That’s why this works.

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

Sidebar

Related Questions

Note The question below was asked in 2008 about some code from 2003. As
In the code below I'm trying to load some images and put them in
See code below, for some reason it only works when I put a breakpoint
I've been using the idiom below for some time now. And it seems to
For some reason I'm not getting this. (Example model below) If I write: var
I have an xslt sheet with some text similar to below: <xsl:text>I am some
Some batch files on Windows use an IF syntax with multiple lines, as below:
I've just started with opengl but I ran into some strange behaviour. Below I
WinForms C#.. am getting some JSON in the format below (bottom of message) and
I have a simple form with some plain html input like bellow using ASP.NET

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.