why does this happen?

From: Billy Patton (bpatton_at_ti.com)
Date: 03/29/05


Date: Tue, 29 Mar 2005 13:01:19 -0600

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>

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));
   return tp;
}

int main(void)
{
   test_p tp = tp_malloc();
   string s = "abcd";
   tp->path = s;
   return 0;
}

test_t is a c structure but sontaing the STL string.
In may data I cannot use new yet because of all these structures.
So malloc is creating the memory but it does not initialize the string path
because it was done by malloc instead of new.
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?

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