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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:08:10+00:00 2026-05-20T11:08:10+00:00

Hey guys, I taught myself PHP a few years back and am now in

  • 0

Hey guys, I taught myself PHP a few years back and am now in college and am relatively well versed in Java (and by that I really mean I’m a beginner but did my data structures course in it. I’ll be taking Algorithms next semester)

In any case…I want to learn C++ and stumbled upon this:
http://newdata.box.sk/bx/c/

What is really tripping me up right now is pointers…I THINK I get the theory but a really simple “program” isn’t running right. It compiles but then there’s a memory error.

I’ve decided to purchase C++ Primer after doing a few searches here on SO so it’ll get here on Friday. Until then, can anyone tell me what is (horribly) wrong with this simple code:

class Number {
public:
    string *owner;

    int getNum() {
        return *num;
    }

    int getTwice() {
        return *twice;
    }

    Number(int our_num, string me) {
        *num = our_num;
        *twice = 2 * *num;
        *owner = me;
    }

private: 
    int *num;
    int *twice;

};

int main()
{
Number *nbr3 = new Number(3,"Bob");

cout << nbr3->getNum() << endl;
cout << nbr3->getTwice() << endl;
cout << nbr3->owner << endl;
delete nbr3;
system("PAUSE");
return 0;
}

The errors appear in the constructor…like the *num=our_num part.

But isn’t that line saying “set the value of the num pointer to our_num”? That IS what I want!

Thanks for helping with what I know to be a very silly and fundamental error…

  • 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-20T11:08:11+00:00Added an answer on May 20, 2026 at 11:08 am

    You didn’t allocate memory locations to point to for num, twice,owner. So, in your constructor –

    Number(int our_num, string me) {
    
        num = new int;
        twice = new int;
        owner = new string;
    
        *num = our_num;
        *twice = 2 * (*num);
        *owner = me;
    }
    

    Since, the class is managing resources, class Number should follow –Rule of Three

    Edit 1

    Pointers are like any other variables but holds a memory address. Just declaring int* ptr; doesn’t mean that ptr is pointing a to valid memory location right away. You should assign/initialize it to where it should point to.

    So,

    int *ptr;
    int num = 10;
    ptr = &num; // ptr points to num location
    
    int *ptr2 = new int; // The operator returns a memory location from free store that can hold an integer.
    *ptr2 = 10;   // Now, store 10 in the location ptr2 is pointing to.
    // ......
    
    delete ptr2;  // You should return the resources back to the free store since we are managing resources.
    

    Hope it helps to an extent.

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

Sidebar

Related Questions

Hey guys, I'm using Twitter's PHP API, called twitterlibphp , and it works well,
Hey guys I have a query that selects data and organizes but not in
Hey guys, I have a CS exam tomorrow. Just want to get a few
Hey guys quick question, I am currently echoing a lot of javascript that is
Hey guys, I want to make a function loop over all elements that have
Hey guys, I'm currently trying to implement a function using C that takes in
Hey guys. I have a method that gets called each second which I want
Hey guys, I have a raidan style 2d flying/fighter game that im working on.
Hey guys. Working on Mac OSX 10.6, My problem is that I would like
Hey guys, I have created a tree which is not a binary tree. Now,

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.