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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:47:47+00:00 2026-05-31T21:47:47+00:00

header file: private: vector<int*>* nums; public slots: void buttonClicked(); cpp file: NewWindow(){ int one

  • 0

header file:

private:
vector<int*>* nums;

public slots:
void buttonClicked();

cpp file:

NewWindow(){
    int one = 1;
    int* pone = &one;
    int two = 2;
    int* ptwo = &two;

    vector<int*> numbers;
    numbers.push_back(pone);
    numbers.push_back(ptwo);
    nums = &numbers;

    test();
}

//Prints size of nums vector
void NewWindow::test(){
    stringstream woo;
    woo << nums->size()<<endl;
    cout << woo.str();
}

//I just had one button on my gui
void NewWindow::buttonClicked(){
    test();
}

When I run this, the first test method executes in the contructor and outputs 2, as expected. However, after clicking the button (i.e executing buttonClicked() method), the test method outputs a seemingly arbitrary number (e.g. 4292719658). After messing around a bit I discovered that if I make the numbers vector an attribute, the test() method will output 2 each time. Why was I getting a weird output with numbers as a non-attribute?

  • 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-31T21:47:49+00:00Added an answer on May 31, 2026 at 9:47 pm

    If numbers is a local variable, as it is here:

    NewWindow(){
      vector<int*> numbers;
      nums = &numbers;
    }
    

    then it is destroyed, and ceases to exist, when it goes out of scope. In this case, when the NewWindow function returns. Referencing a non-existent object (as you do through the nums pointer), results in undefined behavior.

    If numbers is a member variable, on the other hand, it is destroyed when the object of which it is a member is destroyed.

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

Sidebar

Related Questions

I have the following in my Puzzle.h class Puzzle { private: vector<int> puzzle; public:
I have the following C++-class: // Header-File class A { public: A(); private: B
I have a class header file called Grid.h that contains the following 2 private
Header file is graph.h #ifndef _GRAPH_H_ #define _GRAPH_H_ #include <map> #include <vector> using namespace
I have this definition in a header file: class Owner { private: // Fields
I am having some trouble with vector declarations in the header file of a
I have set a header file's role in Xcode to private so that it
I have the following header file: #ifndef DATABASE_H #define DATABASE_H #include <vector> #include <iostream>
I am implementing a vector, and for some reason, my header file keeps saying
Header file: // pe10-8arr.h -- header file for a simple list class #ifndef SIMPLEST_

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.