Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Char* Question

Reply
Thread Tools

Char* Question

 
 
tryptik@gmail.com
Guest
Posts: n/a
 
      02-12-2007
I am implimenting an IO function that will write numeric types (int,
long, double, etc) in binary format. I am casting them to a char* in
order to implement byte-swapping from big-endian to little-endian
before writing them out. To that end, I am using re-interperet-cast
and assigning the result to a char *. I tried to clean up the code
with a delete statement, but this seems to cause an exception.

My question is, will the memory assigned to buf be reclaimed when the
function goes out of scope? If I pass a long, buf should be pointing
to an array of size 4 - why does the delete statement cause a fault?

template <class T>
void put(T& t, std:stream& s)
{
size_t size = sizeof(T);

char* buf = reinterpret_cast<char *>(&t); // Isn't buf pointing to a
char array?

//byteswap buf

s.write(buf, size);

delete [] buf; //This causes an exception - why?
}

Thanks for your help.

__tryptik

 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      02-12-2007
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> I am implimenting an IO function that will write numeric types (int,
> long, double, etc) in binary format. I am casting them to a char* in
> order to implement byte-swapping from big-endian to little-endian
> before writing them out. To that end, I am using re-interperet-cast
> and assigning the result to a char *. I tried to clean up the code
> with a delete statement, but this seems to cause an exception.
>
> My question is, will the memory assigned to buf be reclaimed when the
> function goes out of scope? If I pass a long, buf should be pointing
> to an array of size 4 - why does the delete statement cause a fault?


No memory is really assigned to 'buf' - it's just a pointer that you're
setting to refer back to the original item ('t') that was passed to the
function. That means when you try to delete buf, it's trying to delete
whatever was passed into the function.

For example, if I wrote something like:

int x;

put(x, cout);

it would cause a major problem: put is going to try to delete x -- but x
wasn't allocated with new, so the best you can hope for is that your
program dies quickly with a big error message.

Use of 'delete' should match up exactly with use of 'new' and anything
that wasn't allocated with new should NOT be delete'd -- ever. Use of
new and delete should match up not only in the number of times they're
called, but also logically. For example, a typical use of new is in a
constructor for a class. The matching use of delete would be in the
destructor for the same class. A function like 'put' that has nothing to
do with allocating objects shouldn't be involved with deleting them
either.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments