Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 23rd, 2006, 4:06 AM   #1
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
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:
Debug Assertion Failed!

Program: C:\test\Debug\a.exe
Filedbgdel.cpp
Line:47

Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
 void function1()
{
	char *c = NULL;

	c="Jay";

	cout<<c<<endl;

	if(c)
	 delete c;
}
myName is offline   Reply With Quote
Old May 23rd, 2006, 4:36 AM   #2
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
Quote:
char *c = NULL;
try this:
char *c = new char [size]
size is an integer .. here you are saying ... create c , which is a pointer to an array of characters of size[size]..
hbe02 is offline   Reply With Quote
Old May 23rd, 2006, 4:49 AM   #3
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
Hi hbe02,

Tried. Can not. :-(
myName is offline   Reply With Quote
Old May 23rd, 2006, 5:08 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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;
}
But here's C strings for you anyway:
#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
nnxion is offline   Reply With Quote
Old May 23rd, 2006, 8:00 AM   #5
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Then assertion failed because you are calling delete on a pointer that doesn't point to dynamically allocated memory.

Quote:
Originally Posted by myName
 void function1()
{
	char *c = NULL;

	c="Jay";

	cout<<c<<endl;

	if(c)	// Note: c != 0 == true.
	 delete c;  // <--- Oops!
}
Cache is offline   Reply With Quote
Old May 23rd, 2006, 8:06 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 23rd, 2006, 1:20 PM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by DaWei
Did you mean to use strcpy up there, Ruben?
Whoops, yeah, I was just trying to show what he was trying, I didn't actually think about what the code was doing.
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
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC