Re: why does this happen?

From: Thomas Matthews (Thomas_MatthewsSpamBotsSuck_at_sbcglobal.net)
Date: 03/29/05


Date: Tue, 29 Mar 2005 20:00:31 GMT

Billy Patton wrote:

> First I'm trying to convert K lines of c code to c++, in baby steps.
>
> I've written a small example of something I'm trying to do.
> #include <string>
For using malloc(), also:
   #include <stdlib.h>
That way, you won't have to cast the output.
{You can also include <cstdlib> }

> using namespace std;
>
> typedef struct
> {
> string path;
> int val;
> } test_t;
> typedef test_t *test_p;
>
> test_p tp_malloc(void)
> {
> test_p tp = (test_p) malloc(sizeof(test_t));
Remove the cast:
     test_p tp = malloc(sizeof(test_t));

> return tp;
> }
The above could be simplified as:
test_p tp_malloc(void)
{
   return malloc(sizeof(test_t));
}
This allows the compiler to implement Return Value
Optimization, if it wants to. It is less typing
for you. :-)

> int main(void)
> {
> test_p tp = tp_malloc();
Note that malloc() will return NULL if there were
any problems allocating memory. Remember this
for below.

> string s = "abcd";
> tp->path = s;
If the pointer "tp" is null, you will be deferencing
a null pointer, which is undefined (and unacceptable)
behavior. Anything could happen, such as firing
off rockets unexpectedly.

> return 0;
Before your program leaves, it should be nice
and free up the allocated memory. See free().

> }
>
> test_t is a c structure but sontaing the STL string.
> In may data I cannot use new yet because of all these structures.
Why?
Have you tried:
   struct test_t
   {
     string path;
     int val;
   };
   typedef test_t * test_p;

//...
   test_p tp;
   tp = new test_t;
   tp->path = s;
   delete tp;
   return 0;

> So malloc is creating the memory but it does not initialize the string
> path because it was done by malloc instead of new.
WTF? The operator new will call the constructor when creating a new
instance of a type. The malloc() function does not. The malloc()
function will not initialize the memory it allocates; the program
must do that.

> I know if I changed path to string* path, then in the tp_malloc do a tp
> = new string, ith should probably work ok. But I would kile to avoid
> this to have the destructors work properly.
>
> Is there any way around this?

To have constructors and destructors work properly, use operator new
and operator delete. You must also code your contructors and
destructors to work properly too.

>
>
> ___ _ ____ ___ __ __
> / _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
> / _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
> /____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
> /___/ Texas Instruments ASIC Circuit Design Methodology Group
> Dallas, Texas, 214-480-4455, b-patton@ti.com

-- 
Thomas Matthews
C++ newsgroup welcome message:
          http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq:   http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
          http://www.comeaucomputing.com/learn/faq/
Other sites:
     http://www.josuttis.com  -- C++ STL Library book
     http://www.sgi.com/tech/stl -- Standard Template Library


Relevant Pages

  • Re: Linked List Library
    ... I don't recommend casting the return value of malloc: ... The cast is not required in ANSI C. ... When calling malloc(), I recommend using the sizeof operator on ... typedef struct ADCLIST_el ...
    (comp.lang.c)
  • Re: Pointers, structs and memory allocations
    ... > lots of pointers and pointers to pointers which makes me confused. ... Hiding structs behind typedefs is only a good idea if the struct needs ... Casting malloc is a no no, ... for lists that are pretty short. ...
    (comp.lang.c)
  • Re: How does free() knows?
    ... See the FAQ section 7.7. ... > implicit casting is better than explict casting? ... malloc() returns a void pointer and that gets automatically ... An explicit cast will keep the compiler from complaining (even ...
    (comp.lang.c)
  • Malloc statistics patch
    ... Now that the UMA changes to use critical sections instead of per-cpu mutexes ... memory allocation patch previous posted. ... - Introduce per-cpu malloc type statistics, ... - If we're willing to abandon 5.x backport, we can clean up 'struct ...
    (freebsd-performance)
  • Re: Looking for malloc() help
    ... struct item *stuff; ... Never ever cast the result from malloc() except you like to go into ... with mallocto avoid memory leaks. ...
    (comp.lang.c)