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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:56:22+00:00 2026-06-08T04:56:22+00:00

I am fairly new to C++ and I keep getting segmentation fault with pointer

  • 0

I am fairly new to C++ and I keep getting segmentation fault with pointer assignment with code similar to the following, I know it means I’m accessing memory that hasn’t been allocated, but I don’t see where:

I have two classes:

class ClassA{ //class a decl.
  ClassB** oArray;
  unsigned int x;
 public:
  ClassA(unsigned int X);
  void oMember(ClassB* classb);
}


ClassA::ClassA(unsigned int X){ //Constructor for class a
  x = X;
  oArray = new ClassB* [x];

 for (unsigned int i = 0; i < x; i++){
        oArray = NULL; 
    }
}

class ClassB{ //rough decl of class B
  public:
   getId();
}

I have a class member function that takes in a pointer to another class like this:

void ClassA::oMember(ClassB* classb){
   unsigned int cID = classb.getId(); //defined in class b
   oArray[cID] = classb; //if cID is less than x defined in constructor, is this legal?
}

I just want to point the cIDth member of the array to classb.

I keep getting segmentation fault with an assignment similar to the above. I don’t quite know why, I printed the cID and it is definitely less than the size of the array we declared in ClassA’s constructor.

Why is that assignment illegal, or why am I getting a segmentation fault?

  • 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-06-08T04:56:23+00:00Added an answer on June 8, 2026 at 4:56 am

    As we realized in the chat, I just record the problem here for the reference.

    The problem lies in the piece

    RegisteredVMs = new VendingMachine *[nVendingMachines];
    for (unsigned int i = 0; i < nVendingMachines; i++){
       RegisteredVMs = NULL; 
    }
    

    RegisteredVMs is allocated and immediately set to NULL.
    This pointer is accessed later in the VMregister() function which causes the seg. fault.

    Pointers are hard and very error-prone. Use them only if there is no other way. Since this is a homework problem and since you says you have no say on the interface, I see that you have to use them.

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

Sidebar

Related Questions

I am creating a view for a database for an assignment, i keep getting
I'm fairly new to Haskell, and I'd like to keep reading lines from the
I am fairly new to Windows API Programming and I want to know in
Being fairly new to the .Net world, I am getting very confused about where
I have a fairly simple query that I keep getting timeouts (it takes over
I'm fairly new to MySQL and don't know how to write a query I
Fairly new to programming. I just can't wrap my head around how to get
fairly new iPhone developer here. Building an app to send RS232 commands to a
Fairly new to Objective C and trying to work within Storyboards given it's the
Fairly new to Azure and the whole worker role concept, previously if I wanted

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.