Dynamic memory allocation problem

Hello,

I've been trying to get around my memory allocation problem for the past hour or so, but I seem to get nowhere.
This code works well (taken from the tutorial page):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// rememb-o-matic
#include <iostream>
#include <new>
using namespace std;

int main ()
{
  int i,n;
  int * p;
  cout << "How many numbers would you like to type? ";
  cin >> i;
  p= new (nothrow) int[i];
  if (p == 0)
    cout << "Error: memory could not be allocated";
  else
  {
    for (n=0; n<i; n++)
    {
      cout << "Enter number: ";
      cin >> p[n];
    }
    cout << "You have entered: ";
    for (n=0; n<i; n++)
      cout << p[n] << ", ";
    delete[] p;
  }
  return 0;
}


And you can have it initialise the memory block automatically by changing
1
2
3
4
5
6
7
8
9
10
for (n=0; n<i; n++)
    {
      cout << "Enter number: ";
      cin >> p[n];
    }
    cout << "You have entered: ";
    for (n=0; n<i; n++)
      cout << p[n] << ", ";
    delete[] p;
  }


Into:
1
2
3
4
for (n=0; n<i; n++)
      {
        p[n] = 0;
      }



What I am trying to do is to declare a pointer in a header file, then initialising my array of memory in the .cpp file.
This is my code:
memorymanagement.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <new>      // for dynamic memory allocation

using namespace std;

class MemoryManagement
{
public:
    void initialiseMainMemory();

private:
    int *mainMemory;

};


memorymanagement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void MemoryManagement::initialiseMainMemory()
{
    // initialise main memory
    mainMemory = new int [4096];

    if (mainMemory == 0) {
        // failed to allocate memory
        cerr << "Failed to allocate memory" << endl;

    } else {
        // memory allocation successful
        // initialise all values to 0
        cout << "Main memory allocated" << endl;

        for (int i = 0; i < 4096; i++) {
            mainMemory[i] = 0;
            cout << i << endl;

        }
    }
}


But my code crashes the program at runtime when I run it from my Mac OS, and on Windows the variable i (simply a counter) counts up to around 1700 (the value is always the same, I just do not remember exactly what it is).
I can't see what is wrong with my code, can anyone give me some advice?

Thanks
David
You did not show your program so it is not clear what code you run,
Last edited on
My main.cpp simply creates an object for MemoryManagement and calls initialiseMainMemory().
The problem is in the code I gave, as if I comment it out everything else works just fine.
Have you tried running it in a debugger? That should show you exactly where it crashes, and what the state of the memory is at the point of the crash.
@DavPin
The problem is in the code I gave, as if I comment it out everything else works just fine.


If the problem in this code as you are saying then why do you bother the forum? I do not see any problem with the code you showed except that the default destructor does not free the allocated memory. So the problem is not in the code you showed but in another code that you did not showed and only take our time with bla bla bla.
@MikeyBoy

I did, and it crashes when I try and initialise my block of memory.
Here:
1
2
3
4
5
for (int i = 0; i < 4096; i++) {
         mainMemory[i] = 0;
         cout << i << endl;

}


But with the array size of 4096, it does not crash immediately. It reaches i=1928 and then crash. With other numbers it crashes immediately.

The crash report from Windows reads the following:

Problem signature:
Problem Event Name: APPCRASH
Application Name: CacheCoursework.exe
Application Version: 0.0.0.0
Application Timestamp: 512e2678
Fault Module Name: CacheCoursework.exe
Fault Module Version: 0.0.0.0
Fault Module Timestamp: 512e2678
Exception Code: c0000005
Exception Offset: 000017ca
OS Version: 6.1.7601.2.1.0.256.4
Locale ID: 2057
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789



I searched for "Exception Code: c0000005" and it seems to be that my program is trying to reference memory that is not there.
But I don't understand why it does that, because that is the only dynamic memory allocation that my program does (and it is not that big, I have 8 GB of RAM in this machine)
Ok problem solved.
Thank you anyway.
Diid not you understand what I said did you? Show the full code that you run.
Topic archived. No new replies allowed.