Static pointers

  • Thread starter prasoonthegreat
  • Start date
P

prasoonthegreat

Look at at following two functions min and min2

void min(int a[],int n)
{

static int *x=new int[n];
if(n==0)
{delete[] x;return;}
else
cout<<x<<" "<<n<<endl;
min(a,n-1);

}

void min2(int a[],int n)
{
static int *x;
x=new int[n];
if(n==0)
{delete[] x;return;}
else
cout<<x<<" "<<n<<endl;
min2(a,n-1);

}

what are difference between the static pointers in the two cases....

why do i get the same value of 'x' in the function min even though
control reaches the statement static int *x = new int[n], each time
the function is being called....

I get different values of 'x' in min2 even though the pointer is
static
(what is the significance of being static then)

tell me the difference between the following two

1) static int *p=new int[n];
2) static int *p;
p=new int[n];

I am waiting for responses.......
 
A

Alf P. Steinbach

* (e-mail address removed):
tell me the difference between the following two

1) static int *p=new int[n];
2) static int *p;
p=new int[n];

This looks like homework, but, ok.

In the first case you have a once-only initialization-in-declaration.

It's performed the first time execution passes through the declaration.

In the second case you have an assignment, which is performed every time
execution passes through the statement.

Check out your textbook.

I am waiting for responses.......

That seems counter-productive on Usenet. What would you do if you didn't get a
response within a few days? At least be sure to have on hand sufficient
foodstuff and water when you settle down to wait.


Cheers & hth.,

- Alf
 
P

prasoonthegreat

* (e-mail address removed):


tell me the difference between the following two
1) static int *p=new int[n];
2) static int *p;
    p=new int[n];

This looks like homework, but, ok.

In the first case you have a once-only initialization-in-declaration.

It's performed the first time execution passes through the declaration.

In the second case you have an assignment, which is performed every time
execution passes through the statement.

Check out your textbook.
 I am waiting for responses.......

That seems counter-productive on Usenet. What would you do if you didn't get a
response within a few days? At least be sure to have on hand sufficient
foodstuff and water when you settle down to wait.

Cheers & hth.,

- Alf



Well i was a bit confused that is why i asked this question....
I do my homework myself.......

Well one more thing....
What happens when the control reaches the statement static int *p= new
int [n] each time the function is being called ...although the pointer
has been initialized...I think just it reaches there to allocate
enough memory each time starting from the same address with which ii
was initialized...

Correct me if I am wrong
 
J

James Kanze

* (e-mail address removed):
tell me the difference between the following two
1) static int *p=new int[n];
2) static int *p;
p=new int[n];
This looks like homework, but, ok.

Homework or not, neither function will really work.
In the first case you have a once-only initialization-in-declaration.

And he had a delete when the recursion was finished. Since the
initialization won't occur the second time the function is
called from the outside, he'll end up using a pointer to deleted
memory.

In the other case, of course, he's allocating everytime he
enters the function (but only deleting once, at the end of the
recursion---a nice memory leak).
 
P

prasoonthegreat

* (e-mail address removed):
tell me the difference between the following two
1) static int *p=new int[n];
2) static int *p;
    p=new int[n];
This looks like homework, but, ok.

Homework or not, neither function will really work.
In the first case you have a once-only initialization-in-declaration.

And he had a delete when the recursion was finished.  Since the
initialization won't occur the second time the function is
called from the outside, he'll end up using a pointer to deleted
memory.

In the other case, of course, he's allocating everytime he
enters the function (but only deleting once, at the end of the
recursion---a nice memory leak).

--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

void min2(int a[],int n)
{
static int *x;
x=new int[n];
if(n==0)
return
else
cout<<x<<" "<<n<<endl;
min2(a,n-1);
delete[] x;
}

i hope this will not result to memory leak......
 
A

Alf P. Steinbach

* (e-mail address removed):
void min2(int a[],int n)
{
static int *x;
x=new int[n];
if(n==0)
return
else
cout<<x<<" "<<n<<endl;
min2(a,n-1);
delete[] x;
}

i hope this will not result to memory leak......

Define a function alloc() and a function release().

Replace your calls to new and delete with calls to alloc and release.

Add trace statements to alloc and release and check what's going on.


Cheers & hth.,

- Alf
 
P

prasoonthegreat

* (e-mail address removed):
tell me the difference between the following two
1) static int *p=new int[n];
2) static int *p;
    p=new int[n];
This looks like homework, but, ok.

Homework or not, neither function will really work.
In the first case you have a once-only initialization-in-declaration.

And he had a delete when the recursion was finished.  Since the
initialization won't occur the second time the function is
called from the outside, he'll end up using a pointer to deleted
memory.

In the other case, of course, he's allocating everytime he
enters the function (but only deleting once, at the end of the
recursion---a nice memory leak).

--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

What should I do in the second case to avoid memory leak......????
 
H

Helge Kruse

what is the bug in this code??
tell me if any

void min2(int a[],int n)
{
static int *x;
x=new int[n];
if(n==0)
return
else
cout<<x<<" "<<n<<endl;
min2(a,n-1);
delete[] x;
}

Bugs:
1. Memory leak
You allocate an int vector with "x=new int[n]" and return if n is zero.
The operator new returns a valid pointer to an array of size 0, that
must be deleted. You dont, if n==0.
Fix: call new after checking n==0

2. Probably corrupting heap
You allocate only one pointer x in the global variable aray, not on the
stack (see: static). If this function is called in more than one thread
and a thread switch occures between new and delete, this one variable
will be overwritten. When the first thread gets the CPU again, the value
of x is changed and points to an int array that has already been deleted.
Fix: remove the static keyword.

Helge
 
P

prasoonthegreat

one more thing

void min(int a[],int n)
{

static int *x=new int[n];
if(n==0)
{delete[] x;return;}
else
cout<<x<<" "<<n<<endl;
min(a,n-1);

}

In this the there is once -only initialization in declaration
but the control moves through the statement static int *x=new int[n]
each time a recursive call is made ...what does it do when the
function is recursively called the second time (since it has already
been initialized)????
 
P

prasoonthegreat

one more thing

void min(int a[],int n)
{

static int *x=new int[n];
if(n==0)
{delete[] x;return;}
else
cout<<x<<" "<<n<<endl;
min(a,n-1);

}

In the above there is once -only initialization in declaration
but the control moves through the statement static int *x=new int[n]
each time a recursive call is made ...what does it do when the
function is recursively called the second time (since it has already
been initialized)????
 
J

James Kanze

one more thing
void min(int a[],int n)
{
static int *x=new int[n];
if(n==0)
{delete[] x;return;}
else
cout<<x<<" "<<n<<endl;
min(a,n-1);
}
In the above there is once -only initialization in declaration
but the control moves through the statement static int *x=new int[n]
each time a recursive call is made ...what does it do when the
function is recursively called the second time (since it has already
been initialized)????

Nothing.

Note that this will also be true the second time that min is
called non-recursively as well. After you've deleted the memory
x points to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top