new...delete not work in MFC ? Why ?

Hi,


Check out the following code
  char* t=new char[1000];
  t="sdfsdf";
  delete t;


at delete t;
statement , MSVC 6.0  MFC application generates Abort: Unhandled
exception 


Why? 
Help ! 


sandSpiderX

0
7/5/2005 10:09:16 AM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
533 Views

Similar Articles

[PageSpeed] 40

> Hi,
>
>
> Check out the following code
>  char* t=new char[1000];
>  t="sdfsdf";
>  delete t;
>
>

The rule is simple, if you use "new [...] then you must use delete []" and 
if you use new ... then simply call delete ...

In your case you need to do delete [] t;

....but that's not the end of your problems.

"char * t" is a pointer, and by saying 't="sdfsdf";' you are giving the 
pointer a new value, (not what it is pointing to).

So you need to do "strcpy( t, "sdfsdf" );"

You will see that the value of the pointer itself has not changed, because 
it is still pointing to the 1000 char, but what is in that memory space has 
changed to "sdfsdf", (more specifically the first 7 chars were changed, the 
remaining 993 are waiting to be used and their current value is undefined, 
(whatever was there before).

Simon 


0
7/5/2005 10:22:36 AM
It's even simpler than that! He reassigned the pointer then did delete on a pointer that
was not allocated by new!
			joe

On Tue, 5 Jul 2005 11:22:36 +0100, "Simon" <spambucket@example.com> wrote:

>
>> Hi,
>>
>>
>> Check out the following code
>>  char* t=new char[1000];
>>  t="sdfsdf";
>>  delete t;
>>
>>
>
>The rule is simple, if you use "new [...] then you must use delete []" and 
>if you use new ... then simply call delete ...
>
>In your case you need to do delete [] t;
>
>...but that's not the end of your problems.
>
>"char * t" is a pointer, and by saying 't="sdfsdf";' you are giving the 
>pointer a new value, (not what it is pointing to).
>
>So you need to do "strcpy( t, "sdfsdf" );"
>
>You will see that the value of the pointer itself has not changed, because 
>it is still pointing to the 1000 char, but what is in that memory space has 
>changed to "sdfsdf", (more specifically the first 7 chars were changed, the 
>remaining 993 are waiting to be used and their current value is undefined, 
>(whatever was there before).
>
>Simon 
>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15974)
7/5/2005 2:55:34 PM
>>
>>The rule is simple, if you use "new [...] then you must use delete []" and
>>if you use new ... then simply call delete ...
>>
>>In your case you need to do delete [] t;
>>
>>...but that's not the end of your problems.
>>
>>"char * t" is a pointer, and by saying 't="sdfsdf";' you are giving the
>>pointer a new value, (not what it is pointing to).
>>
>>So you need to do "strcpy( t, "sdfsdf" );"
>>
>>You will see that the value of the pointer itself has not changed, because
>>it is still pointing to the 1000 char, but what is in that memory space 
>>has
>>changed to "sdfsdf", (more specifically the first 7 chars were changed, 
>>the
>>remaining 993 are waiting to be used and their current value is undefined,
>>(whatever was there before).
>>
>>Simon
>>


"Joseph M. Newcomer" <newcomer@flounder.com> wrote in message 
news:fq7lc1dfh8fo37oreci9rt728jpod2kdmu@4ax.com...
> It's even simpler than that! He reassigned the pointer then did delete on 
> a pointer that
> was not allocated by new!
> joe

Read my post again please.

I was explaining the delete vs delete [], (as per his OP), and then 
explaining what the real problem was.

Thanks.

Simon


0
7/5/2005 3:18:38 PM
Reply:

Similar Artilces:

Delete table values automatically
Hi All, I want to create a routine that will clean all records from a specific table automatically once a week (if possible) or each time that the MDB opens up. Any thoughts? Thanks a lot in advance Hi, the code to empty a table is simple Public Sub EmptyTable() Dim strSQL as String Dim db as DAO.Database Set db = dbengine(0)(0) strSQL = "DELETE FROM TableName" db.Execute strSQL, dbFailOnError Set db = Nothing Exit Sub the above code goes in a standard module when your database opens, you call the routine EmptyTable Jeanette Cunningham "Lp12" <Lp12@discussion...

Bill Pay functions (new to Money)
I'm just setting up and learning my Microsoft Money. I already pay my bills (some automatically, some manually) through my local bank checking account. I have added that account to Money. If I set up Money's bill pay, are my bills going to get paid TWICE? "ConnieVH" <ConnieVH@discussions.microsoft.com> wrote in message news:8DF37BC1-380A-40F9-9F27-0A8AA0A028C2@microsoft.com... > I'm just setting up and learning my Microsoft Money. > I already pay my bills (some automatically, some manually) through my > local > bank checking account. I have added t...

how do i see the numbers of pages at the bottom of my work sheet?
i used to see the page numbers at the bottom of the screen when i worked on a multi-page document, so i could easily move from page to page. now i have to go to edit- then go to page- which is very inconvenient. what happened maggiekins wrote: > i used to see the page numbers at the bottom of the screen when i > worked on a multi-page document, so i could easily move from page to > page. now i have to go to edit- then go to page- which is very > inconvenient. what happened ============================= View / Status Bar. -- ******John Inzer******** **MS Picture It! MVP** ...

Quotes in money 2007 stopped working
Anyone else having problems with Money 2007 downloading quotes? Mine stopped about 10 am. JC Same here JC. -Kevin "JC" <handccomputers@hotmail.com> wrote in message news:1DC73822-FBAB-4E77-BC7B-F5B7556A4876@microsoft.com... > Anyone else having problems with Money 2007 downloading quotes? Mine > stopped about 10 am. > > JC ...

Create New Default PST
How do you go about creating a new PST and then setting it as your default in OL2002? Thanks for the help! JohnH <anonymous@discussions.microsoft.com> wrote: > How do you go about creating a new PST and then setting > it as your default in OL2002? To create it, click File>Open>Outlook Data File. Browse to the folder where you want it, give it a name, and click OK. Then to select it for your default delivery, click Tools>E-mail accounts>View or change existing e-mail accounts>Next. Click the drop-down next to "Delivery new e-mail to the following location...

How can I remove the previous lock cells and keep the new ones.
Could you please help me?... I locked and protected the cells A1:H20 in the sheet 1 two months ago. However, yesterday I wanted to unlock and unprotect these cells and locked and protected the cells A21:H40 instead. However, for some reason the cells A1:H20 are still lock and protect. How can I remove the previous lock cells (A1:H20) and keep the new cells (A21`:H40) only. Thanks. Maperalia In EXCEL 2007 take the following actions to unlock cells A1 to H20:- 1. Home / Cells group / Format / Unprotect Sheet / enter the password to Unprotect Sheet / OK / highlight cells A1 t...

New mail notification #12
I have become more adventurous in my email setup, as a consequence, not much mail goes to my default inbox. I found the 'new mail' notification very handy. I am disappointed that I cannot see a way to set it so that it notifies me if any folder gets new mail. Is there anyway to set this up? I don't mind modifying the registry directly if necessary. Cheers, Guy ** Stress - the condition brought about by having to ** resist the temptation to beat the living daylights ** out of someone who richly deserves it. What version of Outlook are you using? -- -Ben- Ben M. Schorr, MVP...

The new updates aren't available from autoupdate are they yet?
Right? Normally, they are out on "patch Tuesday" like the Windows updates and usually available via autoupdate the next day. Any word as to when they should start showing up? They usually lag a bit. Remember to quit all Microsoft applications including the Microsoft Database daemon. Best way: Under Help select "check for updates." When AutoUpdater shows up in the Dock, select to "keep in dock" Log out/in and hold Shift key down to disable all startup items. Select the AutoUpdater in the Dock and download the updater. Run the updater and restart. -- Diane ...

Safe to delete the default address lists?
I'd like to replace the default address lists (All Contacts, All Groups & All Users) with customized address lists. Is it safe to delete the default address lists or can they be hidden? The existence of both the default & custom lists is confusing the users. Thanks, David. Yes, they can be deleted. You can use LDIFDE to export them first - in case you need to recreate them the ldf dump will make it a snap. -- Bharat Suneja MVP - Exchange www.zenprise.com NEW blog location: www.exchangepedia.com/blog ---------------------------------------------- "David Agosta" &...

Adding data from multiple cell and linking to a new worksheet
I have data in a sheet with names and amounts due. Like A 500 B 300 A 600 B 300 C 400 C 200 There is a second sheet named summary. I want the totals of each person to appear in the summary sheet. These should be linked cells and any change in data in sheet 1 should be updtaed automatically Create a PivotTable http://www.ozgrid.com/Excel/excel-pivot-tables.htm -- Regards Dave Hawley www.ozgrid.com "Rashmi" <Rashmi@discussions.microsoft.com> wrote in message news:56DC0F3A-8D60-4FE2-B778-3A07790EFE8A@microsoft.com... >I have data in a sheet wi...

When typing charactures next to the cursor are deleted
When typing charactures next to the cursor are deleted ? how do I stop this Turn off Overtype mode, for example by pressing the Insert key (if you are using Word 2007, Insert key cannot be used for this purpose though). -- Stefan Blom Microsoft Word MVP "Battersea Dave-Pudsey" <Battersea Dave-Pudsey@discussions.microsoft.com> wrote in message news:548402A2-20F2-4016-B33B-35BE7A820863@microsoft.com... > When typing charactures next to the cursor are deleted ? how do I stop > this Battersea Dave-Pudsey, Hit the Insert key on your keyboard, or d...

Is this going to work
Hi, I have a dedicated T1 which connects my LAN to the WWW. My exchange box uses this T1 as well. I just got an additional DSL as backup. So basically I now have 2 routers as gateway. Can I connect the DLS router to the same switch as my lan and exchange, then give exchange a second gateway and create a new MX record that maps to the DSL public IP? Thanks No. "Chris" <Chris@discussions.microsoft.com> wrote in message news:AB769181-B634-47B7-AFD4-AA3419C1548B@microsoft.com... > Hi, > I have a dedicated T1 which connects my LAN to the WWW. My exchange box > uses...

Retrieving delete email
How do I retrieve a deleted email after the window of time has passed that the email is still able to be restored through Tools->Recover deleted item? I am trying to recover an email that was SENT from my email address about 3 weeks ago. Thanks for all your help ...

inbox auto deletes messages
when I open a message it deletes itself. How do I turn off this feature. What did I turn on to make this happen? Mike Check your view. It might be set to unread messages. View, Current View. -- Patricia Cardoza Outlook MVP ***Please post all replies to the newsgroups*** "mike" <hr@mrrefund.conm> wrote in message news:0cbc01c362a3$14579020$a001280a@phx.gbl... > when I open a message it deletes itself. How do I turn > off this feature. What did I turn on to make this happen? > > Mike ...

New Email does not display
When new email arrives you don't know it. You don't know you have new mail until you click on an existing email in your inbox. Any ideas? It's a setting. But... 1. You don't tell us the program you're using. 2. We can't guess if it's Outlook or Outlook Express. 3. This is the Excel newsgroup; you're in the wrong place. :) ******************* ~Anne Troy www.OfficeArticles.com "Travis" <Travis@discussions.microsoft.com> wrote in message news:D864BC01-7D98-48DA-A132-614D02E45963@microsoft.com... > When new email arrives you don't know ...

Receive mail stops working
After clicking send/receive download of email begins, after 10 have downloaded receive message "TCP/IP connection unexpectedly terminated by server. Account: mail account, POP3 server error number oxs00cccof" ...

Inserting new rows, which contain the formula in the above the new
I am not very technical when it comes to excel, and am creating a sheet. I have various information in each column, and in two of them I have formula eg - (on row 1) A1, B1 has personal info . C1, D1 have formula inserted, which work I have copied the formula/ data down to say Row 18. when I insert a new row inbetween rows 1 & 18 the formula is not copied down/ up eg - (on row 2) A2, B2 I want to remain blank, so I can input the data C2, D2 I want the formula automatically copied. How do I do this? Please help, and please try and keep it simple I'm not very compu...

NEW MESSAGE
On the untitled message form on top and all the way to the right is a search window. I did a search but now the words are still in the search window everytime I want to send an email. How do I delete it? Thank you!!! It should be gone when you restart Outlook. -- Robert Sparnaaij [MVP-Outlook] Coauthor, Configuring Microsoft Outlook 2003 http://www.howto-outlook.com/ Outlook FAQ, HowTo, Downloads, Add-Ins and more http://www.msoutlook.info/ Real World Questions, Real World Answers ----- "lynsuann" <lynsuann@discussions.microsoft.com> wrote in message ...

My cropping function in Publisher does not work as described!Why?
The croppping function in my Microsoft Office Small Business Edition 2003 Publisher program does not cut off an inserted picture properly. The size of the crop is accurate but the resulting picture inside is expanded and the result is not as I want it to be. What is wrong? Al O Crop your photo with photo-editing software then insert it in your Publisher document. -- JoAnn Paules MVP Microsoft [Publisher] "Al O" <Al O@discussions.microsoft.com> wrote in message news:8E65B108-E28F-4CBB-9525-571E7687D0E9@microsoft.com... > The croppping function in my Microsoft ...

Stuck email in my out box (15 MB)...cannot delete becuase it is "already transmitting."
Help!!! How do I delete it...I cannot delete a 15MB email in my out box becuase it said it is "transmitting." It's been transmiting for three days! Check: File | Work Offline and in: View | Layout: Uncheck the Preview Pane. Can you delete it now? -- Bruce Hagen MS-MVP [Mail] Imperial Beach, CA "THP" <teresahparker@sbcglobal.net> wrote in message news:3EDD2EEF-6FFE-4FF7-982E-D20FB1D53B91@microsoft.com... > Help!!! How do I delete it...I cannot delete a 15MB email in my out > box becuase it said it is &quo...

Deleting columns
Has anyone had this problem: I have a spreadsheet with multiple tabs. On the first tab I can selec a group of cells within a column, lets say B12 -- B45 and right click o them, get the option 'Delete...' and it asks if I want to shift left up, entire row, or entire column. Now, on the second tab I select th same group of cells except now when I right click it will only let m delete rows. Any ideas as to what I am missing here -- surg4u197 ----------------------------------------------------------------------- surg4u1975's Profile: http://www.excelforum.com/member.php?action...

Outlook Express
I'm in OE 6.0 with XP, I deleted an identity by mistake. is there a way I can recover the identity - or at least the inbox and sent folders? It was not passwork protected? Thanks This newsgroup is for support of Outlook 97, 98, 2000 & 2002 from the Office family for Intel PCs. For Outlook Express (OE) support try posting in one of these newsgroups: microsoft.public.inetexplorer.ie4.outlookexpress for OE 4.x microsoft.public.windows.inetexplorer.ie5.outlookexpress for OE 5.x microsoft.public.windows.inetexplorer.ie55.outlookexpress for OE 5.5x microsoft.public.windows.inetexplorer....

sql query won't work
I have two tables, Districts (as in school districts) and in the district table are the names of all the school districts in the state in order. The second table is Districts With Projects (I called it DWP) and in it are the district names and the projects those districts have completed. So there is one column in the first table and two columns in the second table. The are 256 records in the first (districts) table and 183 records in the second table. All I want to do is have a query that reports all 256 district names and beside the district names either the project or a blank (if ther...

Customize Outlook Today not working #2
I am having the same problem bbelt's posting on 11/17/03. I have just installed my Office 2000 and have completed all sp-3 + updates and my Customize button does not work. What do you have to do to get it turned on/working? Thank you bill@springprop.com OL2000: You Cannot Customize Outlook Today After You Install Critical Update 813489 for Internet Explorer: http://support.microsoft.com/default.aspx?scid=kb;EN-US;820575 -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Coauthor, OneNote 2003 for Windows (Visual QuickStart Guide) Outlook Tips: http://ww...

Error when creating new contact
Hi! When creating a new contact directly from the contact "module" everything works just fine. but when we try to create a new contact directly from an account (so the values from the account get adopted to the contact), I get an error message that gives me two options; "try again" or "Cancel" Further on, the error message just says "Something went wron, please contact your system administrator". is there any place I can read a detailed log, to get information about what is wrong? Do you have any ideas what this can be? Thanks! Activate the deb...