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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:21:23+00:00 2026-06-04T07:21:23+00:00

I am having troubles with I/O to file with 2 dimensional dynamic array. It

  • 0

I am having troubles with I/O to file with 2 dimensional dynamic array. It compiles good but it does not work as I want. Example, I save “map” of numbers 1 and after this I change number in source code to example 5 and compile it, now I load my “map” of numbers 1 but when it writes in cycle for at the end it, the output is 5 not 1. Please can somebody help to fix the code?

#include <iostream>
#include <fstream>
int main()
{
int ** array;

array = new int*[20];
for(int y=0;y<20;y++)
    array[y] = new int[30];

for(int y=0;y < 20;y++)
    for(int x=0;x < 30;x++)
        array[y][x] = 1;

int volba = 1;
std::cin >> volba;

if(volba)
{
    std::ifstream in("map",std::ios::in | std::ios::binary);
    if(!in.is_open())
        std::cout << "in map open error\n";
    in.read((char*)&array, sizeof(array));
    in.close();
    std::cout << "loaded\n";
}
else
{
    std::ofstream out("map",std::ios::out | std::ios::binary);
    if(!out.is_open())
        std::cout << "out map open error\n";
    out.write((char*)&array, sizeof(array));
    out.close();
    std::cout << "saved\n";
}

std::cout << "array\n";
for(int y=0;y < 20;y++)
{
    for(int x=0;x < 30;x++)
        std::cout << array[y][x] << " ";
    std::cout << std::endl;
}

for(int y=0;y<20;y++)
    delete [] array[y];
delete [] array;

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-06-04T07:21:24+00:00Added an answer on June 4, 2026 at 7:21 am

    The main problem is this: Hereby

    array = new int*[20];
    

    you allocate an array of pointers, and this will not become a two dimensional array as you later do:

    array[y] = new int[30];
    

    Note that there is a difference between this

    // array of pointers to integer arrays
    int ** array = new int*[20];
    for(int y=0;y<20;y++)
      array[y] = new int[30];
    

    and this

    // two dimensional integer array
    int array[20][30];
    

    You cannot assume that your array of arrays will lie in contiguous memory.


    Besides:
    Hereby

    out.write((char*)&array, sizeof(array));
    

    you just write out the pointer, not the actual data. Try to print the sizeof(array):

    #include <iostream>
    
    int main() {
      int * array = new int[10];
      std::cout << sizeof(array) << std::endl; // probably prints 4 or 8
      return 0;
    }
    

    Conclusion: Unless you need to implement this for educational purposes, std::vector will far more conveniently serve you memory management for free. Also have a look on Boost Serialization. It provides functionality for serialization of STL collections.

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

Sidebar

Related Questions

I have been having troubles getting my makefiles to work the way I want.
I'm developing a wordpress 3.3.1 theme and I'm having troubles with the single.php file.
Hi I am having trouble getting an embedded js file to work. I have
I'm having troubles to create a regular expression who checks if a file has
I am having troubles with the UIScrollView. I have a file called: ExhibitionViewController.h /
I'm having troubles getting this to work. Basically I have a python program that
I'm having troubles with converting file content into list of dictionaries, could you advise?
I'm having troubles reading values from an xml file. Here is the xml file:
I'm having troubles getting a regex to work. I'm trying to parse a large,
I'm trying to compile OpenEXR with cygwin but i'm having troubles doing it. What

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.