![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Programmer
Join Date: Oct 2005
Posts: 48
Rep Power: 0
![]() |
Debug Assertion Failed
When the function below is being execute, a debug assertion Failed error occur where a microsoft Visual C++ Debug Library Dialog Box exist.
Quote:
void function1()
{
char *c = NULL;
c="Jay";
cout<<c<<endl;
if(c)
delete c;
} |
|
|
|
|
|
|
#2 | |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
Quote:
char *c = new char [size] |
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2005
Posts: 48
Rep Power: 0
![]() |
Hi hbe02,
Tried. Can not. :-( |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
I suggest you use C++ strings, like:
#include <iostream>
#include <string>
using namespace std;
void function1()
{
string c;
c = "Jay";
cout << c << endl;
}
int main()
{
function1();
return 0;
}#include <iostream>
#include <cstring>
using namespace std;
void function1()
{
char *c = NULL;
c = new char[strlen("jay") + 1];
c = "Jay";
cout << c << endl;
delete [] c;
}
int main()
{
function1();
return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 263
Rep Power: 4
![]() |
Then assertion failed because you are calling delete on a pointer that doesn't point to dynamically allocated memory.
Quote:
|
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Did you mean to use strcpy up there, Ruben?
A personal opinion about NULL. There's little use setting a declared pointer to NULL if the very next line is going to assign to it. I WOULD test it afterward, to make sure my request got fulfilled. At the OP. Strings, as suggested by Ruben, are a good idea. I would still suggest that you read the material on pointers that is referenced in my signature. It won't help you understand about not paying back memory you didn't borrow, though, as Cache pointed out. You'll have to get that elsewhere.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
![]() MY BAD, don't try that at home. :p
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|