473,388 Members | 1,340 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,388 software developers and data experts.

dynamic memory alloc and recursion

Below is a function I wrote used to evaluate the determinant of a nxn matrix. arguments are an array of the matrix values in order going from left to right jumping to the next line then going from left to right again.

An error happens after the function recursively calls itself once and attempts to allocate dynamic memory for a sub-matrix.

I'm not sure why this happens? Can anyone explain why an error occurs and how to correct it? Thanks.

Expand|Select|Wrap|Line Numbers
  1. double    evaluate(double  *mat,int      dim)
  2. {
  3.     //smallest case
  4.     if(dim==2)
  5.     {
  6.         return mat[0]*mat[3]+mat[2]*mat[1];
  7.     }
  8.     //smallest case
  9.     int b;
  10.     double result = 0;
  11.     //iterate through columns
  12.     for(int j =0; j<dim; j++)
  13.     {
  14.         //calculate b
  15.         if((j+2)%2==0)
  16.         {
  17.             b=1;
  18.         }
  19.         else
  20.         {
  21.             b=-1;
  22.         }
  23.         //calculate b
  24.         //smaller matrix
  25.         double *smaller = new double [(dim-1)*(dim-1)];//error occurs here after one recursive call
  26.         int counter=0;
  27.         for (int p = 0; p<dim*dim; p++)
  28.         {
  29.             if(dim != j)
  30.             {
  31.                 smaller[counter]=mat[p];
  32.                 counter++;
  33.             }
  34.         }
  35.  
  36.         //smaller matrix
  37.         result+=b*mat[j]*evaluate(smaller, dim-1); //recursive call
  38.         delete [] smaller;
  39.     }
  40.     return result;
  41.  
  42.  
  43. }
  44.  
Oct 15 '07 #1
1 2136
RRick
463 Expert 256MB
One probem you is that after you create a smaller matrix of dim-1**2 size (line 25), you then fill it up with dim**2 entries.

Since these values are on the stack, overwritting the stack like this can cause havoc with your program.
Oct 16 '07 #2

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

Similar topics

4
by: hall | last post by:
Hi. I have some problems with a class i've written that allocates memory dynamicaly. I want to put these objects into a std::vector, but it does not work. My class looks (simplified) like this: ...
6
by: Fernando Barsoba | last post by:
Hi all, I have a simple question regarding dynamic memory allocation.. is there a way to create a variable for which we don't know its size using the 'stack' instead of the 'heap'? For instance,...
5
by: Janning Vygen | last post by:
Hi, tonight my database got corruppted. before it worked fine. since two days i do the following tasks every night psql -c 'CLUSTER;' $DBNAME psql -c 'VACUUM FULL ANALYZE;' $DBNAME ...
17
by: Joe Smith | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #define BUF_MAX 80 int main (void) { char buf;
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...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
11
by: mast2as | last post by:
This question has been posted to this forum before and I read the thread but found that the answers were perhaps imcomplete, so I am trying again. Whenever I am creating objects I would like to...
11
by: Grey Alien | last post by:
I am looking to write a very simple memory pool library to store only one data type at a time - i.e. to provide a contiguous block of memory to be alloc'd free'd by the calling program. I am I...
16
by: Steven D'Aprano | last post by:
On Tue, 09 Sep 2008 14:59:19 -0700, castironpi wrote: You've created a solution to a problem which (probably) only affects a very small number of people, at least judging by your use-cases....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.