On Aug 20, 9:54 am, "Dhoom@Rock" <cdac.dha...@gmail.com> wrote:
> hello i get debug error when i going to overload [] operator..
> i compile the program in VC++ 6.0 and get error DEMAGE: aftr normal
> block(#51)..
> my program get run and also get output but at the end of clossing
> brecket i got this error...
>
> My Program:
> include <iostream.h>
> #include <iomanip.h>
> #include <stdlib.h>
> #include <string.h>
> #include <stdio.h>
> #include <conio.h>
All non-standard headers. Use <iostream>, <cstdlib> etc.
> #define LIST 10
const int LIST = 10; // prefer const over Macros
> class array
> {
> char *str[LIST]
> int a[LIST];
> int b;
Prefer vectors and strings over arrays and char*
//eg:
std::vector<std::string> str;
>
> public:
> array()
> {
> for(int i=0;i<LIST;i++)
> {
> str[i]=NULL;
> a[i]=0;
> }
>
> b=0;
// use member initailization list instead of assignment in
constructors
> }
> ~array()
> {
> for(int i=0;i<b;i++)
> delete str[i];
Observe: you are freeing memory using "delete"
> }
> int & operator [] (char * st)
> {
> b++;
> for(int i=0;i<b;i++)
> {
> if(str[i]!=NULL)
> if(strcmp(str[i],st)==0)return a[i];
>
> }
>
> if((str[b] = (char *)malloc(sizeof(st)))==NULL)
Observe: you are allocating memory using "malloc".
Malloc and delete never go together : http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#fa q-16.3
> b=a["dha"];
>
> printf("%d",b);
// Prefer C++ style IO
-N