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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:03:18+00:00 2026-05-17T22:03:18+00:00

I have the following code, which loops through an array of menuoptions and on

  • 0

I have the following code, which loops through an array of menuoptions and on each iteration, creates a ScaledRect object and pushes it to a vector. This vector is a member of a struct.

I have verified that the ScaledRect is created with the correct values, yet when I print back the contents of the regions vector ( in the second loop ), the loop never terminates and the values are garbage.

class ScaledRect : public Rect
{
public:
    ScaledRect(int x1, int y1, int x2, int y2);
};
ScaledRect::ScaledRect(int x1, int y1, int x2, int y2):
    _x1(x1), _y1(y1), _x2(x2), _y2(y2){}

// ScaledRect doesn't have copy constructor, but Rect does
Rect::Rect( const Rect &rect)
{
x1=rect.x1; y1=rect.y1; x2=rect.x2; y2=rect.y2; bClean=KD_FALSE;
}

typedef struct
{
    std::vector<ScaledRect> regions;
}interface;

void PushRegions( interface * myself )
{
    int i = 0;
    while(menuoptions[i].callback != -1 )
    {
        ScaledRect s = 
          ScaledRect(menuoptions[i].x1, 
                     menuoptions[i].y1,
                     menuoptions[i].x2, 
                     menuoptions[i].y2);            
        myself->regions.push_back( s );
        i++;
    }

    std::vector<ScaledRect>::iterator iter = myself->regions.begin();
    std::vector<ScaledRect>::iterator done = myself->regions.end();

    while(iter != done)
    {
        iter->Dump();
        iter++;
    }
}

EDIT
Please note – I’ve just edited – the memory for theinterface is created and I do actually pass in the address of theinterface to this function. (However, I have simplified those two lines here – what actually happens is that PushRegions gets called via a ptr to a function, on a piece of newly allocated memory the size of an interface ).

I can’t post all of the code here – but minimally its:

Func pfunc = GetPFuncForInterfaceObj();
size_t  numbytes = GetSizeForInterfaceObj();
char memory = new char[numbytes];
pfunc(memory);

pfunc ends up being PushRegions and memory ends up being passed as an interface.

When I push the ScaledRect object to a vector declared at the top of PushRegions() it works. Has anyone got any ideas why?

  • 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-17T22:03:18+00:00Added an answer on May 17, 2026 at 10:03 pm

    This is utterly wrong:

    size_t  numbytes = GetSizeForInterfaceObj();
    char memory = new char[numbytes];
    pfunc(memory);
    

    Even if we “fix” it:

    size_t  numbytes = GetSizeForInterfaceObj();
    char* memory = new char[numbytes]; // note pointer
    pfunc((interface*)memory); // and cast
    

    Your object has never been constructed, so the vector is in a garbage state. (Using the object leads to undefined behavior.)

    No, interface may not have a constructor explicitly defined, but there is an implicit constructor, and it’s there for a reason. It needs to construct the members. You can use “placement new” (by including <new>) to construct an object by placing it at a memory location:

    size_t  numbytes = GetSizeForInterfaceObj();
    char* memory = new char[numbytes]; // note pointer
    pfunc(new (memory) interface); // and CREATE
    

    Now you’re using a valid object.


    I’ll assume there’s a good reason for using pointers at all, let alone a manually constructed object. That said, your code does too much. It both manages a resource, and uses one; pick one or the other.

    That is:

    struct interface_obj
    {
        interface_obj() :
        mMemory(GetSizeForInterfaceObj()),
        mInterface(new (&mMemory[0]) interface)
        {}
    
        ~interface_obj()
        {
            mInterface->~interface(); // destruct
        }
    
        interface* get() const
        {
            return mInterface;
        }
    
    private:
        // noncopyable for now, easy to add
        interface_obj(const interface_obj&);    
        interface_obj& operator=(const interface_obj&);
    
        // again, with the vector we use a resource (dynamic buffer),
        // not manage one.
        std::vector<char> mMemory;
        interface* mInterface;
    };
    

    Much cleaner:

    interface_obj obj;
    pfunc(obj.get());
    

    And it will be released no matter what. (Your code wouldn’t in the face of exceptions, without messy try-catch blocks and other nonsense.) Again, preferable is to not have this kind of allocation in the first place.

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

Sidebar

Related Questions

I have code that looks like the following, which works fine for displaying the
I'm trying to use opengl in C#. I have following code which fails with
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return
Okay so I have the following Code which appends a string to another in
I have the following JQuery code which does similar functionality like Stackoverflow where the
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have the following JavaScript code: Link In which the function makewindows does not
I have the following code, which will not work. The javascript gives no errors

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.