S
Sid
Hi,
I was going through this reference code and playing around with it.
The code (shown below) shows a class that represents a stack of string
pointers
In the code below in particular in the pop function, the code works
when I set stack[index]=0 but when I say delete stack[index] the code
compiles but does not execute -I am only trying to "delete
stack[index]" after I have copied the relevant data pointed by it
into variable "rv", so why is this program not allowing me to free
stack[index] (which is nothing but a pointer to a string) ?.
Also, when I comment the stack[index] and the delete stack[index], the
code executes to completion but doesn't this actually create a memory
leak since stack[index] is never deallocated or freed and if so how is
the program executing to completion without any errors ?
I would greatly appreciate any feedback.
#include <string>
#include <iostream>
using namespace std;
class StringStack {
static const int size = 100;
const string* stack[size];
int index;
public:
StringStack();
void push(const string* s);
const string* pop();
};
StringStack::StringStack() : index(0) {
memset(stack, 0, size * sizeof(string*));
}
void StringStack:
ush(const string* s) {
if(index < size)
stack[index++] = s;
}
const string* StringStack:
op() {
if(index > 0) {
const string* rv = stack[--index];
stack[index] = 0; // this works
//delete stack[index]; // doesn't work
return rv;
}
return 0;
}
string iceCream[] = {
"pralines & cream",
"fudge ripple",
"jamocha almond fudge",
"wild mountain blackberry",
"raspberry sorbet",
"lemon swirl",
"rocky road",
"deep chocolate fudge"
};
const int iCsz =
sizeof iceCream / sizeof *iceCream;
int main() {
cout << "size of iceCream="<< sizeof iceCream << endl;
cout << "size of *iceCream="<< sizeof *iceCream << endl;
StringStack ss;
for(int i = 0; i < iCsz; i++)
ss.push(&iceCream);
const string* cp;
while((cp = ss.pop()) != 0)
cout << *cp << endl;
} ///:~
I was going through this reference code and playing around with it.
The code (shown below) shows a class that represents a stack of string
pointers
In the code below in particular in the pop function, the code works
when I set stack[index]=0 but when I say delete stack[index] the code
compiles but does not execute -I am only trying to "delete
stack[index]" after I have copied the relevant data pointed by it
into variable "rv", so why is this program not allowing me to free
stack[index] (which is nothing but a pointer to a string) ?.
Also, when I comment the stack[index] and the delete stack[index], the
code executes to completion but doesn't this actually create a memory
leak since stack[index] is never deallocated or freed and if so how is
the program executing to completion without any errors ?
I would greatly appreciate any feedback.
#include <string>
#include <iostream>
using namespace std;
class StringStack {
static const int size = 100;
const string* stack[size];
int index;
public:
StringStack();
void push(const string* s);
const string* pop();
};
StringStack::StringStack() : index(0) {
memset(stack, 0, size * sizeof(string*));
}
void StringStack:
if(index < size)
stack[index++] = s;
}
const string* StringStack:
if(index > 0) {
const string* rv = stack[--index];
stack[index] = 0; // this works
//delete stack[index]; // doesn't work
return rv;
}
return 0;
}
string iceCream[] = {
"pralines & cream",
"fudge ripple",
"jamocha almond fudge",
"wild mountain blackberry",
"raspberry sorbet",
"lemon swirl",
"rocky road",
"deep chocolate fudge"
};
const int iCsz =
sizeof iceCream / sizeof *iceCream;
int main() {
cout << "size of iceCream="<< sizeof iceCream << endl;
cout << "size of *iceCream="<< sizeof *iceCream << endl;
StringStack ss;
for(int i = 0; i < iCsz; i++)
ss.push(&iceCream);
const string* cp;
while((cp = ss.pop()) != 0)
cout << *cp << endl;
} ///:~