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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:38:05+00:00 2026-05-15T10:38:05+00:00

I am new to C++ and I’m having trouble using a map correctly in

  • 0

I am new to C++ and I’m having trouble using a map correctly in class that I’m writing. Basically, when I make a new Block object in a test program and call its write method, the program totally shits itself and gives me a double free or corruption error. What is weird is that everything works fine if I uncomment that one line in the Block constructor. I’m guessing I’m missing some basic c++ knowledge but I’m not finding anything useful on google.

Block.h (includes not shown but they are there):

namespace SSDSim{
    class Block{
        public:
            Block(uint block_num);
            ~Block(void);
            void read(uint page_num);
            void write(uint page_num, void *data);
            void erase(void);
        private:
            uint block_num;
            std::map<uint, void *> page_data;
    };
}

Block.cpp:

#include "Block.h"

using namespace std;
using namespace SSDSim;

Block::Block(uint block){
    //page_data[4]= (void *) 0xfeedface;
    block_num= block;
}

...

void Block::write(uint page_num, void *data){
    if (page_data.find(page_num) == page_data.end()){
        page_data[page_num]= data;
    } else{
        cerr<<"Invalid write\n";
        exit(1);
    }
}

test.cpp:

#include <iostream>
#include "../Block.h"

using namespace std;
using namespace SSDSim;

int main(void){
    Block b= Block(0);
    b.write(0, (void *) 0xdeadbeef);
    b.read(0);
//  b.read(4);
    return 0;
}
  • 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-15T10:38:05+00:00Added an answer on May 15, 2026 at 10:38 am

    Are you sure the error isn’t when you call read()? After all, your constructor assigns page[4] to 0xfeedface, an arbitrary place in memory that could point to anything. If you try to then read that location in memory, bad things may happen.

    I would try to avoid creating pointers out of arbitrary addresses. Instead, test your program by making some object and passing its address to page[4].

    The error could also be in your destructor. Since you are allocating a Block on the stack, its destructor is called when it goes out of scope.

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

Sidebar

Related Questions

New class is a subclass of the original object It needs to be php4
New to javascript/jquery and having a hard time with using this or $(this) to
Being new to test based development, this question has been bugging me. How much
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
New to xml. Looking for XPath to search a xml file with python ElementTree
New to silverlight. Traditionally if I were to design a wizard-like process where a
New to both Ruby and Rails but I'm book educated by now (which apparently
New to WCF, but familiar with COM+ - can I wrap a WCF service
New to Linux programming in general. I am trying to communicate with a kernel
The new extensions in .Net 3.5 allow functionality to be split out from interfaces.

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.