Re: malloc vs new for POD types

From: Paul (paul_at_paul.com)
Date: 10/31/04


Date: Sun, 31 Oct 2004 08:04:14 -0500


"Method Man" <a@b.c> wrote in message
news:hHUgd.552$14.136@read1.cgocable.net...
>
> I don't see the problem with copying objects bit-by-bit. It's ok for
structs
> isn't it? Can someone shed some light?
>
"Method Man" <a@b.c> wrote in message > It's not quite what I was after. But
I found "[16.4] Can I use realloc() on
> pointers allocated via new?" interesting to read. Here's the partial
answer
> from the FAQ:
>
> I don't see the problem with copying objects bit-by-bit. It's ok for
structs
> isn't it? Can someone shed some light?
>
>

Read the rest of the FAQ. For example, what if the object is
reference-counted? You don't update the reference count if you blindly copy
the object by using realloc(). Some implementations use reference counted
std::string's. If you bypass the copy-assign operators, you will
undoubtedly render the string object(s) unstable.

In general, that's why there is such a thing as a user-defined copy
constructor and assignment operator. They are there for a purpose, and that
purpose is to do something important if the object is copied or assigned.

-Paul



Relevant Pages

  • Re: malloc vs new for POD types
    ... I found "Can I use realloc() on ... You don't update the reference count if you blindly copy ... constructor and assignment operator. ... purpose is to do something important if the object is copied or assigned. ...
    (comp.lang.cpp)
  • Re: Structs vs. Classes
    ... Classes are created on heap while structs are created on stack. ... different from "Bob Smith, customer #1234". ... reference to that customer in your entire program see that change. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Naming structs with a variable
    ... Structs exist for the purpose of being more lightweight than ... I would agree if you'd just said that structs are lighter weight ... a literal reference from an authoritative source: ... *know* causes confusion on the grounds that people should learn more ...
    (microsoft.public.dotnet.languages.csharp)
  • Beware CS1612 when dealing with "Point" (its not really a structure like most of us think of)
    ... modify a Point, which I had made have a property. ... like 'int' or 'nullable' types, and does not really have 'member ... allow you to return a reference to a value type. ... The only issue I have with the way that structs are implemented ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Reference to a struct inside its definition
    ... But the more general issue here is that you've got a data type that you expect to be stored as a reference on a regular basis, making it more appropriate as a reference type than a value type. ... Generally speaking, classes are reference types, while structs and primitive types are value types. ... If you assign a value type to a variable that's of type object, or pass it as a parameter of type object, etc. the compiler will generate code that creates an object instance that contains the value type you've used. ... If you box the same value instance multiple times, you get multiple instances, each with a new copy of the value type. ...
    (microsoft.public.dotnet.languages.csharp)

Loading