Re: why does this happen?
From: Thomas Matthews (Thomas_MatthewsSpamBotsSuck_at_sbcglobal.net)
Date: 03/29/05
- Next message: hk_mp5kpdw: "Re: why does this happen?"
- Previous message: Billy Patton: "why does this happen?"
- In reply to: Billy Patton: "why does this happen?"
- Next in thread: Barry Schwarz: "Re: why does this happen?"
- Reply: Barry Schwarz: "Re: why does this happen?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: hk_mp5kpdw: "Re: why does this happen?"
- Previous message: Billy Patton: "why does this happen?"
- In reply to: Billy Patton: "why does this happen?"
- Next in thread: Barry Schwarz: "Re: why does this happen?"
- Reply: Barry Schwarz: "Re: why does this happen?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|