Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

System.StackOverflowException


  • Please log in to reply

#1
pmukund

pmukund

    New Member

  • Member
  • Pip
  • 8 posts
Hi
I am new to .NET programming.....I am trying to write a program that copies a large matrix into a certain format called HDF5 format. The program just has to do this. There are a large number of text files (140). The program just reads the files and converts the text data into integers and stores these in a matrix. So the matrix has 140*1091 rows as each file has 1091 integers. But in the beginning when I give the memory allocation as "const int FSPACE_DIM1 = 1091*100;" the program stops here itself and gives the error:
"An unhandled exception of type 'System.StackOverflowException' occurred in copytohdf5.exe".

I saw the earlier discussion where daniel had the same problem. Swandog46 had given a reply asking him to rewrite the program iteratively so that he could allocate his own memory. I am not able to figure out how to do this. This is my code (It builds and compiles):

// This is the main project file for VC++ application project
// generated using an Application Wizard.

// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include <string>

#include "stdafx.h"
#ifdef OLD_HEADER_FILENAME
#include <iostream.h>
#else
#include <iostream>
#endif
#include "H5Cpp.h"

#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif


const string FILE_NAME = "RamanHDF.h5";
const string DATASET_NAME( " RAMAN data" );
const int FSPACE_RANK = 2; // Dataset rank as it is stored in the file
const int FSPACE_DIM1 = 1091*100; // Dimension sizes of the dataset as it is...
const int FSPACE_DIM2 = 4; // ...stored in the file
//const int FSPACE_DIM3 = 2;

#include <iomanip>
#include <fstream>
using namespace std;

#using <mscorlib.dll>

using namespace System;

int _tmain()
{


//float matrix[FSPACE_DIM1][FSPACE_DIM2][FSPACE_DIM3];
float matrix[FSPACE_DIM1][FSPACE_DIM2];
float x=0;
int m=0;
int e=0;
string y;

char filename1[100] = "C:\\Documents and Settings\\ganesht-a\\Desktop\\moving to CPP\\directRAMANTOHDFALLXY\\raman-files\\wy2-136";
char filename3[15]=".txt";
char filename2[15];
char filename[100];



for(int j=1;j<=100;j++)
{
y.clear();
for(int k=0;k<=250;k++)
filename[k]='\0';
for(int k=0;k<=strlen(filename2);k++)
filename2[k]='\0';

sprintf(filename2, "%d", j);

for(int i=0;i<(strlen(filename1));i++)
filename[i]=filename1[i];

m=strlen(filename);

for(int i=0;filename2[i]!='\0';i++)
filename[m+i]=filename2[i];

m=strlen(filename);

for(int i=0;filename3[i]!='\0';i++)
filename[m+i]=filename3[i];

ifstream inFile;
inFile.open(filename);

if (!inFile)
{
cout << "Unable to open file";
exit(1); // terminate with error
}

while(getline(inFile,y))
if(y =="XYDATA")
{
for(int i=0;i<1091;i++)
for(int l=0;l<2;l++)
{
inFile >> x;
matrix[i+(1091*(j-1))][l] = x;
}
}
for(int i=0;i<j*1091;i++)
{
if(((i+1)%1091)==0)
e=(i+1)/1091;
else
e = (i+1)/1091 + 1;
if(e%20==0)
matrix[i][2]=20;
else
matrix[i][2]=e%20;
if(e%20==0)
matrix[i][3]=e/20;
else
matrix[i][3]=(e)/20+1;
}

for(int i=0;i<j*1091;i++)
{
cout<<matrix[i][0]<<"\t"<<matrix[i][1]<<endl;

}
inFile.close();

}


//This part is only after reading the file data into the matrix and must work fine once the matrix size problem is cleared..

H5File* file = new H5File( FILE_NAME, H5F_ACC_TRUNC );
hsize_t fdim[] = {FSPACE_DIM1, FSPACE_DIM2}; // dim sizes of ds (on disk)
DataSpace fspace( FSPACE_RANK, fdim );
DataSet* dataset = new DataSet( file->createDataSet( DATASET_NAME, PredType::NATIVE_FLOAT , fspace ));
dataset->write( matrix, PredType::NATIVE_FLOAT );
delete dataset;
delete file;

file = new H5File( FILE_NAME, H5F_ACC_RDONLY );


dataset = new DataSet( file->openDataSet( DATASET_NAME ));


dataset->read( matrix, PredType::NATIVE_FLOAT);


delete dataset;
delete file;
return 0;
}


I am not from a programming background and have been trying this. If Swandog46 could tell me how to allocate memory on my own I would greatly appreciate it.
  • 0

Advertisements


#2
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Hi pmukund,

I don't understand why your code would be crashing out on the line you said, so I've been trying to compile it on my machine. I'm having trouble getting anywhere without the "H5Cpp.h" file. Maybe you could attach it, or even better, your entire project folder in a zip file.

Another thing that seems a little strange to me is that you appear to have created a managed c++ project, but rather than take advantage of the .net runtime, you are using the c++ libraries. Is it a homework requirement to do this in managed c++? Managed c++ is kind of a beast. If possible, I'd strongly recommend either using straight c++ (unmanaged), or c#.

-Ricci
  • 0

#3
pmukund

pmukund

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
Hi Ricci
Thanks man. I will send you my project. The folder HDFView contains the setup for HDFview which is the program used to open the file created by the C++ code. The folder hdf5 contains all the include files necessary. The folder directRAMANTOHDFALLXYforall140 contains the code.

Thanks for the trouble. By the way I am trying to do the same thing in MATLAB and trying to implement it in a GUI.

The problem is the zipped file is 23.1 mb. So can you give me an email account where I can send it?

mukund
  • 0

#4
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Hey mukund,

That is a surprisingly large project folder. Let's try attacking this problem from another angle.

I was thinking more about your problem and the error it displays, and I'm guessing that the error does not occur on that line of code that it is indicating. What probably happens is that somewhere in your program the stack overflows, causing your instruction pointer to get messed up. This can send your program execution into never-never land, and by the time it detects the error, it happens to be on some random line of code...the one it is indicating.

The strange thing now is that your stack is overflowing. That should only happen with recursion, but I don't see where you are using recursion at all. Have you already modified your code to stop using recursion and iterate instead?

-Ricci
  • 0

#5
pmukund

pmukund

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
Hi Ricci
I am not using recursion at all. Only iterations. Actually the problem is in the line "float matrix[FSPACE_DIM1][FSPACE_DIM2];" where FSPACE_DIM1 = 1091*140. I comment out everything else in the main and just run the program with this one line. Still the program gives the StackOverflow exception. Moreover there was something interesting happening....if I run it with FSPACE_DIM1 = 1091*30 it wasnt supposed to work and I expected it to give the stack overflow exception. But when I first run it for FSPACE_DIM1 = 1091*25 and then FSPACE_DIM1 = 1091*30 and then FSPACE_DIM1 = 1091*35 it went all the way upto 55. Is it a problem with memory settings? I was busy for the last 2 days and couldnt see your post. If you sense the problem please tell me.

Thank you
Mukund
  • 0

#6
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Ok, this makes sense. When you declare an array in C++ like you are doing:
float matrix[FSPACE_DIM1][FSPACE_DIM2];
The compiler allocates all the memory on the stack. You are declaring a very large array, and so it is immediately overflowing the stack. You could try allocating it from the heap:
float* matrix = new float[FSPACE_DIM1][FSPACE_DIM2];
After you do this, you will need to make sure that matix != NULL. If it's null, the operating system couldn't find enough memory to allocate.

-Ricci
  • 0

#7
pmukund

pmukund

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
Hi Ricci
I am trying to declare the matrix as you said. First of all there is an error like
error C2440: 'initializing' : cannot convert from 'float (*)[2]' to 'float *'
when i try to declare it as
float* matrix = new float[FSPACE_DIM1][FSPACE_DIM2];
But when I change this to
float* matrix = new float[FSPACE_DIM1,FSPACE_DIM2]; it works
but i dont know how to call the (i,j)th element of the matrix.
Is it matrix[i][j] (this doesnt work-error C2109: subscript requires array or pointer type)
or
matrix[i,j] (this works but it doesnt store the value assigned to it but some junk value maybe an address)
or
*(matrix[i] +j) (error C2100: illegal indirection)
I have been trying to store values in an array of pointers using *(matrix[i] + j)

This is a small code:
int _tmain()
{
int m[4];
int a;
for (int j=1;j<5;j++)
m[j-1]=j;

int* matrix[] = {&m[0],&m[1],&m[2],&m[3]};
//int* matrix[] = {1,2,3,4};
//int matrix[3][3];

//cout<<matrix;
//cout<<*(matrix[0]);

//cout<<*(matrix);
for(int i=0;i<3;i++)
{
for( int j=0;j<4;j++)
{
*(matrix[i]+j)=i+2*j;
//cout<<*(matrix[i]+j)<<" ";
//a = *(matrix[0]);
}
cout<<endl;
}
cout<<(*(matrix[0]));
cout<<matrix[0,0];*/
The code stores the values i+2*j as the values at the addresses stored in the matrix. I am able to store the values but I am unable to print any of the values outside the loop.

So i am still trying to do this. Sorry for troubling you with everything but it would be great if you could figure this out.

Thanks
mukund
  • 0

#8
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Ok, that was bad code, sorry! :tazz:

The code I should have suggested looks like this:
float (*matrix)[FSPACE_DIM2] = new float[FSPACE_DIM1][FSPACE_DIM2];
This declares a pointer to a two dimensional array with FSPACE_DIM2 columns and allocates from the heap a two dimensional array with FSPACE_DIM1 rows and FSPACE_DIM2 columns. You access the elements of the array with
matrix[i][j];
Then, before your return statement in the main function, be sure to delete the array:
delete matrix;
Note that some might suggest that you have to say
delete matrix [];
but this isn't necessary as long as your array does not hold objects with a destructor. Floats are fine.

Good luck,
Ricci
  • 0

#9
pmukund

pmukund

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
Hi Ricci

Thanks a billion man...
this worked for us.
We were stuck at this thing for about 2 weeks now.. thanks to you it is solved and we can go to the next step:D:)
That was really grt!!! Plz give some tips on learning VC++. Is there anything we can refer to?
Thank you again,

Mukund
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP