C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 1st, 2006, 07:16 PM
kedarnath kedarnath is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 66 kedarnath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 27 m 38 sec
Reputation Power: 5
2D dynamic array

Hello Everyone,

I am trying to read the data from a file and store into a 2D array(the file contains 540000 values), but i m facing memory issues. Then my friend suggested to create a dynamic 2D array, i thought thats simple but when i searched for the code on web, i was in vain and hardly implement those stuff.

Can anyone please tell me how do i declare,intialize and access a 2D array dynamically.

The code which i tried to implement is below, but i am ending up in errors

Code:
#include <stdio.h>
#include <stdlib.h>
int  main()
	
{
FILE *fp; //file pointer for reading a file
double k;
char word[80];
double **DataLat=NULL;
DataLat=new double[360][1502]; // 2-D array for holding all the file values
int row=0, col=0;    // row column  and column declaration, used in 2-d array

fp = fopen("lat.txt", "r");

    if ( fp == NULL )
   {
     puts("Unable to open the file");
     exit(1);      //terminate program 
   }

  while(fscanf(fp,"%s",word)!=EOF)
   {
     k=atof(word);
     DataLat[row][col]=k;
     col++;
         if(col==1502)
		{
		  row++;
        	  col=0;
		}
		if(row==359) exit(1);
   }
   delete []DataLat;
fclose( fp );
}


thanks,
Kedar

Reply With Quote
  #2  
Old March 1st, 2006, 09:04 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,644 Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 6 h 15 m 12 sec
Reputation Power: 92
Code:
#include <iostream>
#include <fstream>

int main()
{
   const int ROWS = 360, COLS = 1502;
   // Allocate 2D array.
   double **data = new double*[ROWS];
   for ( std::size_t r = 0; r < 360; ++r )
   {
      data[r] = new double[COLS];
   }
   // Read data into array.
   std::ifstream file("file.txt");
   if ( file )
   {
      for ( int r = 0; r < ROWS; ++r )
      {
         for ( int c = 0; c < COLS; ++c )
         {
            file >> data[r][c];
         }
      }
      file.close();
   }
   // Do stuff...
   // De-allocate 2D array.
   for ( std::size_t r = 0; r < 360; ++r )
   {
      delete[] data[r];
   }
   delete[] data;

   return 0;
}
__________________
Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.

Reply With Quote
  #3  
Old March 1st, 2006, 10:30 PM
kedarnath kedarnath is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 66 kedarnath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 27 m 38 sec
Reputation Power: 5
Thanks Dave,

That was a quick reply, I am trying to retreive the values that are stored in the array, but i guess, i m doing something wrong.

Here is what i did, let me know if if do something wrong.

Code:

#include <iostream>
#include <fstream>

int main()
{
   const int ROWS = 360, COLS = 1502;
   // Allocate 2D array.
   double **data = new double*[ROWS];
   for ( std::size_t r = 0; r < 360; ++r )
   {
      data[r] = new double[COLS];
   }
   // Read data into array.
   std::ifstream file("lon.txt");
   if ( file )
   {
      for ( int r = 0; r < ROWS; ++r )
      {
         for ( int c = 0; c < COLS; ++c )
         {
            file >> data[r][c];
         }
      }
      file.close();
   }

    for ( int r = 0; r < ROWS; ++r )
      {
         for ( int c = 0; c < COLS; ++c )
         {
            printf("%f\n", data[r][c]);
         }
      }
     

   // Do stuff...
   // De-allocate 2D array.
   for ( std::size_t r = 0; r < 360; ++r )
   {
      delete[] data[r];
   }
   delete[] data;

   return 0;
}


Reply With Quote
  #4  
Old March 2nd, 2006, 10:42 AM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,644 Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 6 h 15 m 12 sec
Reputation Power: 92
I don't see it straight away, but shortly after I posted I thought about editing my post to add in better error checking. (I also now notice a couple places I forgot to change 360 to ROWS.)

How about something like this?
Code:
#include <iostream>
#include <fstream>

double **my_alloc(int rows, int cols);               // Allocate 2D array.
void     my_cleanup(double **data, int rows);        // De-allocate 2D array.

int my_read(double **data, int rows, int cols)
{
   int count = 0;
   std::ifstream file("lon.txt");
   if ( file )
   {
      for ( int r = 0; r < rows; ++r )
      {
         for ( int c = 0; c < cols; ++c )
         {
            if ( !(file >> data[r][c]) )
            {
               return count;
            }
            ++count;
         }
      }
   }
   return count;
}

void do_stuff(double **data, int rows, int cols, int count)
{
   for ( int r = 0; r < rows; ++r )
   {
      for ( int c = 0; c < cols; ++c )
      {
         std::cout << "data[" << r << "][" << c << "] = " << data[r][c] << '\n';
         if ( --count == 0 )
         {
            return;
         }
      }
   }
}

int main()
{
   const int ROWS = 360, COLS = 1502;
   double **data = my_alloc(ROWS, COLS);
   int count = my_read(data, ROWS, COLS);
   do_stuff(data, ROWS, COLS, count);
   my_cleanup(data, ROWS);
   return 0;
}

double **my_alloc(int rows, int cols)
{
   double **data = new double*[rows];
   for ( std::size_t r = 0; r < rows; ++r )
   {
      data[r] = new double[cols];
   }
   return data;
}

void my_cleanup(double **data, int rows)
{
   for ( std::size_t r = 0; r < rows; ++r )
   {
      delete[] data[r];
   }
   delete[] data;
}

Reply With Quote
  #5  
Old March 2nd, 2006, 11:35 AM
David_B David_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 46 David_B User rank is Sergeant (500 - 2000 Reputation Level)David_B User rank is Sergeant (500 - 2000 Reputation Level)David_B User rank is Sergeant (500 - 2000 Reputation Level)David_B User rank is Sergeant (500 - 2000 Reputation Level)David_B User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 14 h 10 m 48 sec
Reputation Power: 9
Eye-catching post!

I am presently trying to improve my own programs, which use much dynamic memory, so this thread is timely. In reading through the rest of this thread, several questions came to mind:

1) The OP indicated he was having memory problems. So how does using dynamic memory solve that problem? If you explictly define the array to be a fixed size [360][1502] or dynamically allocate it to be 360X1502, you're still allocating the same size of memory aren't you? Am I making a wrong assumption?

2) Dave, what about error checking if the memory allocation fails? Shouldn't there be a check to see if "new" fails? What is the best way of checking and dealing with this possibility?

3) Dave, I notice you did not use <vector>. I haven't used it myself, but was planning to go that way. In my recent surfing/research, it seems quite well-regarded. Is there a reason you did not use it? If it is, indeed, a good way to go, could you please post an example using <vector>. (My programs include several dynamic 2D arrays, as well as several dynamic 1D arrays. Doing all this memory management gets quite onerous; I am hoping that using <vector> would provide me a more concise option).

Regards,


David

Reply With Quote
  #6  
Old March 2nd, 2006, 11:47 AM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,644 Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dave Sinkula User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 6 h 15 m 12 sec
Reputation Power: 92
Quote:
Originally Posted by David_B
1) The OP indicated he was having memory problems. So how does using dynamic memory solve that problem? If you explictly define the array to be a fixed size [360][1502] or dynamically allocate it to be 360X1502, you're still allocating the same size of memory aren't you? Am I making a wrong assumption?
If sizeof(double) equals 8, then the total space requested is 4325760 bytes. As an automatic (local) variable, this might be a tall request for "the stack". If it were "global", it might have a better chance of working. But dynamic allocation is generally used to ask for large chunks of memory from "the heap".

Quote:
Originally Posted by David_B
2) Dave, what about error checking if the memory allocation fails? Shouldn't there be a check to see if "new" fails? What is the best way of checking and dealing with this possibility?
If I knew exception handling better, I'd put that in. But to cover my ignorance, and for simplicity, I left that out. Besides, if you mean a check for NULL, that's out of date advice.

http://www.parashift.com/c++-faq-li...t.html#faq-16.6

Quote:
Originally Posted by David_B
3) Dave, I notice you did not use <vector>. I haven't used it myself, but was planning to go that way. In my recent surfing/research, it seems quite well-regarded. Is there a reason you did not use it?
No. Just answering the OP's post as asked. Vectors do indeed make life easier in a lot of instances.

Quote:
Originally Posted by David_B
If it is, indeed, a good way to go, could you please post an example using <vector>. (My programs include several dynamic 2D arrays, as well as several dynamic 1D arrays. Doing all this memory management gets quite onerous; I am hoping that using <vector> would provide me a more concise option).
Sure. Give me a while.

[edit]One possible version.
Code:
#include <iostream>
#include <fstream>
#include <vector>

int main()
{
   std::ifstream file("lon.txt");
   if ( file )
   {
      const int ROWS = 360, COLS = 1502;
      std::vector< std::vector< double > > data;
      for ( std::size_t r = 0; r < ROWS; ++r )
      {
         std::vector< double > col;
         for ( std::size_t c = 0; c < COLS; ++c )
         {
            double value;
            if ( file >> value )
            {
               col.push_back(value);
            }
         }
         data.push_back(col);
      }
      for ( std::size_t r = 0, rows = data.size(); r < rows; ++r )
      {
         for ( std::size_t c = 0, cols = data[r].size(); c < cols; ++c )
         {
            std::cout << "data[" << r << "][" << c << "] = " << data[r][c] << '\n';
         }
      }
   }
   return 0;
}

Last edited by Dave Sinkula : March 2nd, 2006 at 12:00 PM.

Reply With Quote
  #7  
Old March 2nd, 2006, 01:13 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 8,100 Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 8 h 54 m 48 sec
Reputation Power: 2059
>> what about error checking if the memory allocation fails? Shouldn't there be a check to see if "new" fails? What is the best way of checking and dealing with this possibility?
There are two ways to handle this. The C++ standard actually specifies two forms of the new keyword. By default, if new cannot allocate the memory, it throws the bad_alloc exception. If you don't have an exception handler, the default handler will stop the program (which is what 99% of C programs that check for malloc() failure also do!)

Before the official standard though, some C++ compilers used to return NULL if the new failed. This allowed you to say:
Code:
int *p = new int[20];
if (!p) {
   cerr << "Failed alloc";
   exit(0);
}

very much like C's malloc() function. In order to preserve this behavior for backward compatibility, the C++ standard overloads the new operator for nothrow. To use this, you need to #include new
Code:
#include <new> // Need this for std::nothrow
int *p = new(std::nothrow) int[20];
if (!p) {
   // do something here
}

This form will not throw an exception if the allocation fails. Error handling is similar to C in this case.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month

Reply With Quote
  #8  
Old March 2nd, 2006, 04:00 PM
kedarnath kedarnath is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 66 kedarnath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 27 m 38 sec
Reputation Power: 5
Thanks a lot Dave, that was awesome.

Reply With Quote
  #9  
Old March 8th, 2006, 03:56 PM
David_B David_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 46 David_B User rank is Sergeant (500 - 2000 Reputation Level)David_B User rank is Sergeant (500 - 2000 Reputation Level)David_B User rank is Sergeant (500 - 2000 Reputation Level)David_B User rank is Sergeant (500 - 2000 Reputation Level)David_B User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 14 h 10 m 48 sec
Reputation Power: 9
Hello again, Dave and Scorpions4ever.

Thanks for your comments. I actually do use the check for NULL approach. You are correct: it IS a very onerous process. I have been using Microsoft’s Visual C++ Version 6 for a number of years, somebody told me that approach would work (it does), so I have just been using the same approach for the several numerical mathematics programs I have written.

I am now planning to clean up my source code and post it online, so in the near future, I intend to master some better techniques (one reason for joining this forum).

In fact, over the past few days, I have been working on a webpage on which I have posted a C++ program that calculates the eigenvalues of a matrix. The Intro is here:

Intro

and the source code itself is here:

Source Code Page

I am calling this version 0.

Version 1 will probably use the try-catch approach (I assume it is a more standards-compliant approach). Based on what I have read so far, I should be able to just wrap lines 101 through 136 in try braces, and write a catch routine further down. (Is this assumption correct?)

Version 2 will show the most basic use of the <vector> class. I have read about some very fancy things that can be done with class definitions, but I think they start to hide the algorithm. I actually want to show the algorithm, so I don’t plan to do anything too sophisticated.

Constructive comments regarding my memory management, or any other comments that you can offer that would make my code better, are always welcome.

Regards,


David

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > 2D dynamic array


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.

 
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.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
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.

 
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.

 

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





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