473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Char* Question

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::ostream& 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

Feb 12 '07 #1
1 1390
In article <11*********************@k78g2000cwa.googlegroups. com>,
tr*****@gmail.com 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.
Feb 12 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ekim | last post by:
my question is as follows: I've got a DLL in which I have a method GetBuffer (this one is extern, exported, is called from outside this program) which shall pass a char-buffer to the...
19
by: Jasper Dozer | last post by:
Is this a healthy way to get a pointer to point ? char *p = "longenough"; regards, jasper
1
by: b83503104 | last post by:
When are they not consistent?
42
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
9
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.