Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Memory Allocation

Status
Not open for further replies.

aaadetos

Programmer
Nov 21, 2004
54
US
Must I dynamically allocate memory to variables that are to be user determined at run-time? For instance, I have this code...
Code:
void unbound(double *unbd, int nSize)
{
	enum{nSize = 100};
	double A[nSize],B[nSize],k0[nSize],k1[nSize],s[nSize],expi[nSize],a[nSize],b[nSize];
	
	void bessk0(double *x,double *k0,int nSize);
	void bessk1(double *x,double *k1, int nSize);
	void ei(double *x, double *expi, int nSize);
	
	double x = 0;	
	//trial: K0(x) --> x = rw*sqrt(s), as per A-32
	s[0] = 1;
	for (int t=1;t<nSize;t++)
	{

		CKuchukDlg MyClas;	
		MyClas.m_dWellRad = x;//do i set this dynamically? how??

		s[t] = 2*t*PI;
		a[t] = x*sqrt(s[t]);
		b[t] = 2*sqrt(s[t]);

		bessk0(a,k0,nSize);
		bessk1(a,k1,nSize);
		ei(b,expi,nSize);

		A[t] = k0[t]/(x*sqrt(s[t])*k1[t]);
		B[t] = (1-exp(-2*sqrt(s[t])))/(2*sqrt(s[t]));
	
		unbd[t]=0.5*(A[t]-B[t]-expi[t]);
	}
}
On running the whole project, [ctrl+F5], it crashes, even tho' it comiples without errors. On debugging, I found that the user-defined variable x, initialized to 0 at run-time, becomes this bogus number:
-9.2559631349318e+061, which i certainly didn't put in!! the array, s[t] behaves in an equally unlikey manner:
-->s[0] = 1.0000//fine! initialized so.
-->s[1] = 6.2831853071796//also fine:2*1*PI
-->s[2] = -9.2559631349318e+061//huh?!!!!!!!!!!

I'd appreciate any tips on this...thanks a lot!
 
I don't think this is a problem of dynamic memory allocation actually. The problem is to do with the fact that the user-defined variable at run-time is not being used. How do I change that.
Code:
UpdateData(TRUE);
in the change or update events doesn't do this.
 
1. Local variables are allocated authomatically on the stack, they don't need any additional allocation.
2. What is value of t at the point you are checking value of s[2]? Are you not in the break on the first iteration of the loop?

 
3. Wow!:
Code:
void unbound(double *unbd, int nSize)
{
    enum{nSize = 100};
<...>

What have you meant here?
nSize from enum is probably in global scope.
Locally nSize is taken from actual parameter of the function call - what is value of nSize in call of unbound()?
 
The only value recorded as t in the debug window is "1"; from the code, t should be "2", tho', at s[2]. I set my break point at the last statement in the code:
Code:
unbd[t]=0.5*(A[t]-B[t]-expi[t]);

Since t = 1, is recorded by the debugger, I guess i am in the 1st iteration of the loop; but the debugger records up to s[99],a[99],b[99] and so forth.
 
nSize is always set as 100; unbound is called by other functions that use nSize as 100. Finally, the output file calls nSize 1 more time, and its still 100.
 
And what happens in second iteration, when program stops next time after continue (F5 may be)?

> but the debugger records up to s[99],a[99],b[99] and so forth.

Do you mean, you can see or set values of arrays in debuger mode? Of course you can - arrays are allready allocated. You probably can even set values of s[100], s[101], ..., but this will destroy your stack - program return addresses or at least values of neighboring arrays.

I'm surprised - my compiler claims about dynamical boundaries of local arrays (after removing of enum{nSize = 100};). It seems, that for dynamical arrays only operator new could be used.
 
Yeah; but what could be the cause of this? if i remove enum and use it another way, say
Code:
const int nSize = 100
i suspect that won't solve this problem.
 
What problem?
If you want to be arrays allocated dynamically, use

Code:
double *s=NULL;

   s = new double[nSize];
   if(s)
   {
      <...>
      
      delete [] s;
   }

instead of

Code:
double s[nSize];

      <...>
 
I mean the problem of the compiler not using the value of the variables input at run time. Why is it using the bogus value I earlier mentioned? -9.255...e+061? instead of the user's value?
 
Because you are breaked in the very first iteration of the loop and there is only second element of the array assigned yet at the moment. Continue execution to the next iterations.
 
I placed the breakpoint at the very end of the file; t = 100 in the debugger. Now i get the following:
a[0] to a[99] = 0: meaning the initial value of x is used: (x= 0). it is still not using my value for x.s[0] to s[99] takes on realistic values, for t=0 to t=99.

For your convenience, here is the code again:
Code:
void unbound(double *unbd, int nSize)
{
	enum{nSize = 100};
	double A[nSize],B[nSize],k0[nSize],k1[nSize],s[nSize],expi[nSize],a[nSize],b[nSize];
	
	void bessk0(double *x,double *k0,int nSize);
	void bessk1(double *x,double *k1, int nSize);
	void ei(double *x, double *expi, int nSize);
	
	double x = 0;
	
	CKuchukDlg MyClas;	
	MyClas.m_dWellRad = x;

	s[0] = 1;
	a[0] = x*sqrt(s[0]);
	b[0] = 2*sqrt(s[0]);

	bessk0(a,k0,nSize);
	bessk1(a,k1,nSize);
	ei(b,expi,nSize);

	A[0] = k0[0]/(x*sqrt(s[0])*k1[0]);
	B[0] = (1-exp(-2*sqrt(s[0])))/(2*sqrt(s[0]));
	
	unbd[0]=0.5*(A[0]-B[0]-expi[0]);

	//trial: K0(x) --> x = rw*sqrt(s), as per A-32
	for (int t=1;t<nSize;t++)
	{
		s[t] = 2*t*PI;
		a[t] = x*sqrt(s[t]);
		b[t] = 2*sqrt(s[t]);

		bessk0(a,k0,nSize);
		bessk1(a,k1,nSize);
		ei(b,expi,nSize);

		A[t] = k0[t]/(x*sqrt(s[t])*k1[t]);
		B[t] = (1-exp(-2*sqrt(s[t])))/(2*sqrt(s[t]));
	
		unbd[t]=0.5*(A[t]-B[t]-expi[t]);
	}
}
Why is the compiler not using the user-defined 'x'?
 
Code:
#include <math.h>

#define PI 3.1415926535

// void bessk0(double *x,double *k0,int nSize);
// void bessk1(double *x,double *k1, int nSize);
// void ei(double *x, double *expi, int nSize);
    
enum{nSize = 100};


void unbound(double *unbd, int /* nSize */)
{
    double A[nSize],B[nSize],k0[nSize],k1[nSize],s[nSize],expi[nSize],a[nSize],b[nSize];
    
    double x = 0;
    
//  CKuchukDlg MyClas;    
//  MyClas.m_dWellRad = x;

    s[0] = 1;
    a[0] = x*sqrt(s[0]);
    b[0] = 2*sqrt(s[0]);

//  bessk0(a,k0,nSize);
//  bessk1(a,k1,nSize);
//  ei(b,expi,nSize);

    A[0] = k0[0]/(x*sqrt(s[0])*k1[0]);
    B[0] = (1-exp(-2*sqrt(s[0])))/(2*sqrt(s[0]));
    
    unbd[0]=0.5*(A[0]-B[0]-expi[0]);

    //trial: K0(x) --> x = rw*sqrt(s), as per A-32
    for (int t=1;t<nSize;t++)
    {
        s[t] = 2*t*PI;
        a[t] = x*sqrt(s[t]);
        b[t] = 2*sqrt(s[t]);

//      bessk0(a,k0,nSize);
//      bessk1(a,k1,nSize);
//      ei(b,expi,nSize);

        A[t] = k0[t]/(x*sqrt(s[t])*k1[t]);
        B[t] = (1-exp(-2*sqrt(s[t])))/(2*sqrt(s[t]));
    
        unbd[t]=0.5*(A[t]-B[t]-expi[t]);
    }

x=123.;
}

int main()
{
double unb_arr[nSize];   

   unbound(unb_arr, nSize);

return(0);
}

When i stop execution on line x=123.; (without such line there is nowhere to put a breakpoint), values of s[] seem to bee correct.
 
I put my break point on line ** to get reasonable values for s[]:
Code:
void unbound(double *unbd, int nSize)
{
    enum{nSize = 100};
    double A[nSize],B[nSize],k0[nSize],k1[nSize],s[nSize],expi[nSize],a[nSize],b[nSize];
    
    void bessk0(double *x,double *k0,int nSize);
    void bessk1(double *x,double *k1, int nSize);
    void ei(double *x, double *expi, int nSize);
    
    double x = 0;
    
    CKuchukDlg MyClas;    
    MyClas.m_dWellRad = x;

    s[0] = 1;
    a[0] = x*sqrt(s[0]);
    b[0] = 2*sqrt(s[0]);

    bessk0(a,k0,nSize);
    bessk1(a,k1,nSize);
    ei(b,expi,nSize);

    A[0] = k0[0]/(x*sqrt(s[0])*k1[0]);
    B[0] = (1-exp(-2*sqrt(s[0])))/(2*sqrt(s[0]));
    
    unbd[0]=0.5*(A[0]-B[0]-expi[0]);

    //trial: K0(x) --> x = rw*sqrt(s), as per A-32
    for (int t=1;t<nSize;t++)
    {
        s[t] = 2*t*PI;
        a[t] = x*sqrt(s[t]);
        b[t] = 2*sqrt(s[t]);

        bessk0(a,k0,nSize);
        bessk1(a,k1,nSize);
        ei(b,expi,nSize);

        A[t] = k0[t]/(x*sqrt(s[t])*k1[t]);
        B[t] = (1-exp(-2*sqrt(s[t])))/(2*sqrt(s[t]));
    
        unbd[t]=0.5*(A[t]-B[t]-expi[t]);
    }
}//** I put the breakpoint here; to go thro all the iterations of t.
 
Dont know about VC, but in Watcom at that point local variables are allready destroyed and debugger don't even take a possibility to inspect theyr values.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top