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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:54:37+00:00 2026-06-11T22:54:37+00:00

This seems like a fairly strange bug to me. My C++ program is segfaulting,

  • 0

This seems like a fairly strange bug to me. My C++ program is segfaulting, and I found something strange using GDB. I have the following constructor and copy constructor:

Bank::Bank(char mode, int floor_dimensions_, int num_floors_) : floor_dimensions(floor_dimensions_), num_floors(num_floors_) {
    for (int i = 0; i < num_floors; i++) {
        floors[i] = new Floor(floor_dimensions);
    }

    if (mode == 'M') {
        read_map(floors);
    } else if (mode == 'C') {
        read_coords(floors);
    }
}

Bank::Bank(const Bank& b) {
    floor_dimensions = b.floor_dimensions;
    num_floors = b.num_floors;

    cout << floor_dimensions << endl;
    cout << num_floors << endl;

    for (int i = 0; i < num_floors; i++) {
        floors[i] = new Floor(*b.floors[i]);
        floors[i]->print_map();
}

The definition of this class is something like

class Bank {
    /** The number of floors the bank has */
    int num_floors;

    /** The dimension of each of the floors */
    int floor_dimensions;

    /** The floors in the bank */
    Floor* floors[];

private:
    Bank(char mode, int floor_dimensions_, int num_floors_);
    Bank(const Bank& bank);
    ~Bank();
    void read_map(Floor** floor);
void read_coords(Floor** floor);
}

Now I set a breakpoint at the first line of the constructor using GDB. After executing the next two lines, the value of b.floors[1] has changed, which leads to the segfault when I call b.floors[i]->foo().

    Breakpoint 1, Bank::Bank (this=0x7fffffffe050, b=...) at bank.cpp:29
29      floor_dimensions = b.floor_dimensions;
(gdb) p b.floors[0]
$4 = (Floor *) 0x610070
(gdb) p b.floors[1]
$5 = (Floor *) 0x6103f0
(gdb) p b.floors[2]
$6 = (Floor *) 0x610770
(gdb) n
30      num_floors = b.num_floors;
(gdb) p b.floors[0]
$7 = (Floor *) 0x610070
(gdb) p b.floors[1]
$8 = (Floor *) 0x8006103f0
(gdb) p b.floors[2]
$9 = (Floor *) 0x610770
(gdb) n
32      cout << floor_dimensions << endl;
(gdb) p b.floors[0]
$10 = (Floor *) 0x610070
(gdb) p b.floors[1]
$11 = (Floor *) 0x800000003
(gdb) p b.floors[2]
$12 = (Floor *) 0x610770

Does anyone have any idea what’s going on?

  • 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-11T22:54:39+00:00Added an answer on June 11, 2026 at 10:54 pm

    The problem with your code is most likely the fact that you are attempting to access an uninitialized pointer (b.floors). Assuming b was a result of this copy constructor, then b.floors will be pointing to random memory. b.floors[i] will then be pointing to random memory some bytes ahead of what b.floors is pointing to, and eventually you use -> to dereference this random address, which causes a segmentation fault.

    The solution to your problem is to properly allocate and copy the floors array:

    floors = new Floor* [num_floors];
    for (int i=0; i<num_floors; ++i)
      floors[i] = b.floors[i];
    

    You may also want to copy the pointed object Floor, depending on the details of your implementation.

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

Sidebar

Related Questions

This seems like a fairly simple problem to me but I have been having
I am learning LINQ and this seems like a fairly simple problem. I have
This seems like it should be something fairly simple, but I just can't seem
This seems like another fairly simple thing to do, but I'm again struggling on
This seems like a fairly obvious question, but I haven't been able to think
This seems like it would be fairly simple, but I've been unable to find
I'd like to count and group rows by specific values. This seems fairly simple,
This seems like something that could be quite useful and yet and I can't
This seems like a must have form input for a mobile ui framework, but
This seems like a fairly simple issue, however I could not find any straight

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.