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

Relevant Pages

  • Re: why does this happen?
    ... > typedef struct ... you should avoid using malloc in C++ programs. ... > test_t is a c structure but sontaing the STL string. ...
  • Re: System.IO.Directory.Delete - access denied exception
    ... System.Console.WriteLine("is on.", username, host); ... at System.IO.Directory.DeleteHelper(String fullPath, String ...
  • Re: DLookup Challenge
    ... The OutputFile parameter of the OutputTo method requires a full path as a ... string value, either as a full string path or a string variable represented ... trailing quotes in a field's value. ...
  • Re: Deleting folder
    ... Put spaces after "My" just as I did when I wrote the response, the space is a valid part of the string path and you are missing it. ... Don't attach the RD command to the end of the CD string, do it as a separate step. ... You want to be sure you are in the right directory (the "My Pictures" folder in your user profile) before you run the RD command, as the results are not reversible. ...
  • Re: has anyone seen this before?
    ... >> The following message may help in diagnosing the problem: The string ... Parameter name: configPath at ... >> webLevel, ConfigurationFileMap fileMap, String path, String site, ... >> TextBox userName, ListControl roles, CheckBox userRadio, CheckBox ...