C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 22nd, 2006, 08:45 AM
Esam Esam is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 1 Esam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 58 sec
Reputation Power: 0
Constructor Initialization Problem

I would like to use a vector of vectors to store a 2D array. I played around with it in a small test file which I have included below. The problem is how I create my vector of vectors in the initialization list I think. The program crashes in my destructor when I attempt to delete my dynamically allocated vectors. It works if I use 0-6 as the size of my dynamically allocated vector, but as soon as I make it 7 it crashes...

Code:
class temp
{
public:
temp();
~temp();

void print();

private:
vector< vector < int > > Vector2D;
};

temp::temp() : Vector2D(3, *(new vector < int > (7,2)))
{
   cout << "Hello!" << endl;
}

temp::~temp()
{
   for (int i=0; i < Vector2D.size(); i++)
   {
      cout << i << endl;
      if (&(Vector2D[i]))
      {
         delete &(Vector2D[i]);
      }
      //Program crashes before FINAL done is printed!
      cout << i << " Done " << endl;
   }
}

void temp::print()
{
  for (int i=0; i < Vector2D.size(); i++)
  {
     for (int j=0; j< Vector2D[i].size(); j++)
     {
        cout << Vector2D[i][j] << " ";
     }
     cout << endl;
  }
}

int main(int argc, char *argv[])
{
  temp t;
  t.print();
  
return 0;
}

Reply With Quote
  #2  
Old March 22nd, 2006, 03:24 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Code:
temp::temp() : Vector2D(3, *(new vector < int > (7,2)))
{
   cout << "Hello!" << endl;
}


Clarification: What does "Vector2D(3, *(new vector < int > (7,2)))
" mean ???

Is this statement trying to create a 2 D vector as a matrix of size
7 by 2 ?????

Probably, when deallocating memory you have to first free memory from cells first and then the array of cells.

Just as you free memory from a double pointer?

Example: Let we have to createa 2D char array of size 10 by 5
i.e if char **p = new malloc( 10) ; //allocates memory to single row of ten cells.

and //for ( i = 0 ; i < 5 ; i++)
// *(p + i) = malloc( 5) ; //etc.

Then for deallocation we reverse the loops.Just try to apply that and see if it helps you.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Constructor Initialization Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT