473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Problem in large memory allocating

Hi everyone,

I have a problem with large dynamic memory allocating. this is my code (in briefness):

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <mem.h>
  3. #include <assert.h>
  4. #define SML_BOUNDS_CHECK
  5.  
  6. class Vector_d
  7. {
  8.    private:
  9.       int Dim_;
  10.       double* Val_;
  11.    public:
  12.       Vector_d() { Dim_ = 0; Val_ = NULL; }
  13.       ~Vector_d() { delete[] Val_; }
  14.       int SetDim( int N ); // Get memory for Val_
  15. };
  16.  
  17. class SVector_d
  18. {
  19.    private:
  20.       int NNZ_;
  21.       double* Val_;
  22.       int* Ind_;
  23.    public:
  24.       SVector_d() { Val_ = NULL;    Ind_ = NULL; NNZ_ = 0; };
  25.       ~SVector_d() { delete[] Val_; delete[] Ind_; };
  26.       int SetNNZ( int NNZ ); // Get memory for Val_ and Ind_
  27.       int& Index( int NZI );
  28.       double& Value( int NZI );
  29.       SVector_d& operator=( const SVector_d& V );
  30.       friend class SMatrix_d;
  31. };
  32.  
  33. class SMatrix_d
  34. {
  35.    private:
  36.       int NVec_;
  37.       SVector_d* Vec_;
  38.    public:
  39.       SMatrix_d() { NVec_ = 0; Vec_ = NULL; };
  40.       ~SMatrix_d() {    delete[] Vec_; };
  41.       int SetNVec( int NV );
  42.       SVector_d& operator()( int Index );
  43. };
  44.  
  45. int Vector_d::SetDim( int N )
  46. {
  47.    if(N==Dim_) return 0;
  48.    delete[] Val_;
  49.    Val_ = new double[N];
  50.    Dim_ = N;
  51.    return 0;
  52. }
  53.  
  54. int SVector_d::SetNNZ( int NNZ )
  55. {
  56.    if(NNZ==NNZ_) return 0;
  57.    delete[] Val_;
  58.    Val_ = new double[NNZ];
  59.    delete[] Ind_;
  60.    Ind_ = new int[NNZ];
  61.    NNZ_ = NNZ;
  62.    return 0;
  63. }
  64.  
  65. int& SVector_d::Index( int NZI )
  66. {
  67.    #ifdef SML_BOUNDS_CHECK
  68.       assert( 0<=NZI && NZI<NNZ_ );
  69.    #endif
  70.    return Ind_[NZI];
  71. }
  72.  
  73. double& SVector_d::Value( int NZI )
  74. {
  75.    #ifdef SML_BOUNDS_CHECK
  76.       assert( 0<=NZI && NZI<NNZ_ );
  77.    #endif
  78.    return Val_[NZI];
  79. }
  80.  
  81. SVector_d& SVector_d::operator=( const SVector_d& V )
  82. {
  83.    if (this == & V) return *this;
  84.    SetNNZ( V.NNZ_ );
  85.    memcpy( Val_ , V.Val_ , sizeof(double)*NNZ_ );
  86.    memcpy( Ind_ , V.Ind_ , sizeof(int)*NNZ_ );
  87.    return *this; // General call
  88. }
  89.  
  90. int SMatrix_d::SetNVec( int NV )
  91. {
  92.    if(NV==NVec_) return 0;
  93.    delete[] Vec_;
  94.    Vec_ = new SVector_d[NV];
  95.    NVec_ = NV;
  96.    return 0;
  97. }
  98.  
  99. SVector_d& SMatrix_d::operator()( int Index )
  100. {
  101.    #ifdef SML_BOUNDS_CHECK
  102.       assert( 0<=Index && Index<NVec_ );
  103.    #endif
  104.    return Vec_[Index];
  105. }
  106.  
  107. int main() {
  108.    int Adim = 7000000;
  109.    int udim = 1000000;
  110.    printf("A dimension = %d\n",Adim);
  111.    printf("u dimension = %d\n",udim);
  112.    Vector_d u;
  113.    u.SetDim( udim );
  114.    SMatrix_d A;
  115.    A.SetNVec( Adim );
  116.    SVector_d SV;
  117.    SV.SetNNZ(5);
  118.    for(int i=0; i<5; i++) { SV.Value(i) = i; SV.Index(i)=i; };
  119.    for(int i=0; i<Adim; i++ )    A(i) = SV;
  120.    A.SetNVec( 20 ); // Error occurs at this place
  121.    A(0) = SV;
  122.    printf("Done!\n");
  123.    return 0;
  124. } // End of program
  125.  
The runtime error occurs when program releases memory that is allocated to "Vec_".
This program gets 1GB of memory approximately, while the 32bits systems can use at most 4GB of memory.
if I reduce the value of "Adim" or "udim", program will get less memory, and there will be no error.
However I need to set these large values to both "Adim" and "udim".
My compliler is Borland C++ 5.02.
I will appreciate, if you help me to overcome this problem.

Thanks.
Jul 29 '08 #1
6 2701
Banfa
9,065 Expert Mod 8TB
If is not clear to me why you have chose to implement you own vectors ignoring the STL <vector> class, additionally you seem to be using a compiler that is 8at least 8 years old, you may wish to use a newer one.

Finally you have not said what error you are getting you have just alluded to some mysterious error without every mentioning it.

However I believe you problem is that you have not implemented any copy constructors or assignment operators. In this case the compiler creates them for you and it does a member to member copy and that means that at line 119 of your code all the vectors in your matrix end up sharing the same allocated memory buffer and so when they are deleted in the call to SetNVec at line 120 after the first vector in the array the destructor is trying to delete memory blocks that are no longer valid.

So I would guess you are getting invalid memory block exceptions during the delete.
Jul 29 '08 #2
gpraghuram
1,275 Expert 1GB
Try running the same with Rational Purify to find if there are are any memory corruption......
That may help u to identify the issue.

raghu
Jul 29 '08 #3
I have made the code much simpler:

Expand|Select|Wrap|Line Numbers
  1. class SVector_d
  2. {
  3.    private:
  4.       double* V_;
  5.    public:
  6.       SVector_d() { V_ = 0; }; // Set V_ to NULL
  7.       ~SVector_d() { delete[] V_; };
  8.       void SetSize(int N) { delete[] V_; V_ = new double[N]; }
  9. };
  10.  
  11. int main() {
  12.    double* DPtr = new double[524281];
  13.    int SZ = 50000;
  14.     SVector_d* SVPtr = new SVector_d[SZ];
  15.    for(int i=0; i<SZ; i++) SVPtr[i].SetSize(1000);
  16.    delete[] SVPtr; // Error: Fault access violation...
  17.    delete[] DPtr;
  18.    return 0;
  19. }
  20.  
This code makes the same runtime error "Fault: access violation..." when the program frees memory allocated to "SVPtr" (at line 16).
What do i have to do to get out of this problem?
Declaring copy constuctor in class "SVector_d" does not help any more.
I reminde again that the program will run without any problem if i reduce dynamic memoy size. (Instance line 12 should be replaced by "double* DPtr = new double[500000];")

Thanks.
Jul 29 '08 #4
Savage
1,764 Expert 1GB
I don't get any access violation when I run this code nor on VC++ 2005 or Borland
C++ Builder 5. Try wrapping the allocation code inside try-catch block to see if any error happened during the allocation.
Jul 29 '08 #5
Banfa
9,065 Expert Mod 8TB
I agree with Savage, that 2nd set of code appears to be fine nothing wrong with it. It also runs for me in VS 2005 Express. It only failed when I set SZ high enough to cause the system to run out of virtual address space (About 2GB).

So my guess is that you are running into a limit of your platform (your platform being the compiler/OS combination you are using).

Also I was wrong wasn't I, your compiler is not 8 years old, that is C++ Builder 5, you compiler is 11 years old, and there appears to be anecdotal evidence that it is a 16bit compiler.

I seriously suggest you get something a little more modern, may be something released this century.

Your other options are

1. Try to work out what it is about your platform that is causing the error and find a work-around. Although I suspect it is hitting some hard-coded/hard-wired limit.

2. You could used memory mapped files, that is rather than trying to hold all your data in memory use a file to hold it and only load in part of it to operate on at a time. If you are able to use the WIN32 API then there are functions provided to enable you to do this fairly easily, look up MapViewOfFile on MSDN. Of course if your compiler is 16bit this may not be an option you may have to write the whole thing yourself.
Jul 30 '08 #6
You are right. I must get a new version of compiler. That is my answer. Thanks.
Jul 30 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Peter Hickman | last post by:
I have a program that requires x strings all of y length. x will be in the range of 100-10000 whereas the strings will all be < 200 each. This does not need to be grown once it has been created....
1
by: lwickland | last post by:
Summary: System.Net.ScatterGatherBuffers.MemoryChuck allocates inordinately large bytes when sending large post data. The following application consumes inordinate quantities of memory. My code...
8
by: TheB | last post by:
Ok, lets try this again. I have a program which searches all disk drives for certain file types. When it finds a file it writes a record to a Firebird DB. The program normally is using 40 - 44...
11
by: EDom | last post by:
Hi, I have aspnet_wp.exe with increasing on every postback and not every revisit to any page. Even if I clear session and close the browser it remains in the memory. I have only one connection...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
13
by: coosa | last post by:
Dear all, Using the conio implementation i wanted to create a dynamic string, whereby its size would be determined after each keyboard hit; in other words, i don't want to ask the user to...
8
by: Francine.Neary | last post by:
I'm experimenting with a program that manipulates a large matrix, i.e. a 2-deep array. I started by just allocating this in one go, but later it occurred to me that, as there are still some very...
29
by: marvinla | last post by:
Hello! I'm a beginner in C, and I'm having trouble with a pointer-to-pointer reallocation. This piece of code works well, but Valkyrie warns some parts (pointed below), and is breaking my real...
1
by: Jonathan Wilson | last post by:
I am working on some software which has to deal with data that could be as large as 500mb or so. Currently I am using new and delete to manage this memory but I find it is not ideal and sometimes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.