c++ - Debug Assertion Failure even when using delete[] -


i beginner @ programming , wrote code

#include<iostream> using namespace std;   void removeallcharacters(char * , char * ); void printarray(char*);  void deletearrays(char*,char*);  int main() {             char * source,*remove;             source=new char[18];             remove=new char[10];              source="hello how you";             remove="hi me";              removeallcharacters(source,remove);             printarray(source);              deletearrays(source,remove);             system("pause");             return 0; }  void removeallcharacters(char * source, char * remove) {             int n1=strlen(source)+1;             int n2=strlen(remove)+1;              bool arr[128] = {false};              for(int i=0;i<n2-1;i++)             {                 arr[remove[i]]=1;             }              char *newsource=new char[n1];              for(int i=0,j=0;i<n1-1;i++)             {                 if(arr[source[i]]==0)                 {                     newsource[j++]=source[i];                 }             }              delete  [] source;             source=newsource;             newsource=0; }  void printarray(char* arr) {             int n=strlen(arr)+1;              for(int i=0;i<n;i++)             {                 cout << arr[i];             }              cout << endl << endl; }  void deletearrays(char* arr1,char*arr2) {             delete [] arr1;             arr1=0;             delete[]arr2;             arr2=0; } 

i have tried debugging , found the error occurs @ delete[] source; in removecharacters function.i tried looking solution online couldn't find it. why error occurring? error because have allocated dynamic memory main deleting function?

the return value of new[] pointer value. no matter happens, when issue call delete[] same exact pointer value must used.

obviously violate rule above:

    source=new char[18];     remove=new char[10];      source="hello how you";     remove="hi me"; 

you're allocating new[], replacing returned value value of string literal. in other words, pointer value required call delete[] gone.

here example of should have done:

    char str1[] = "hello how you";     char str2[] = "hi me";     source=new char[sizeof(str1)];     remove=new char[sizeof(str2)];     strcpy(source, str1);     strcpy(remove, str2); 

note created 2 buffers initialized data. allocate enough storage characters (plus terminating null), instead of counting characters , possibly wrong.

then strcpy function copies characters on buffer.

you have error:

source=newsource 

you see on return source not have pointer assigned. reason source value parameter, local function -- assigning doesn't actual value passed function, assignment goes in puff of smoke when function returns.

also, you're beginner @ c++ programming, have started using std::string , not char* string data. creating strings using new[] dinosaur approach in c++. use std::string instead. should strive not business of creating strings this:

 char *newsource=new char[n1]; 

this leads memory leaks, , spaghetti-like logic in keeping track of allocated memory delete[] functions properly. in addition, most, if not of these issues mentioned disappear. assignments work, no memory leaks, etc.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -