Allocate Struct Memory

Pages: 12
I have just been reading Prata on STL, i.e., vectors, etc, but I really know nothing about them yet.

And I'm still not clear on the contructor issue. And I still don't understand why new is safter than malloc(). Will malloc() not make sure the ctors are run? Don't you have to remember to delete[] a new object just like you have to free() a malloc() object?

And so on.

And as was suggested earlier, _strdup() will work with resizing strings, but if I have a big block of memory I certainly cannot use new.

For example, I have one routine where I combine files. The user may choose to combine 2 files or 200 files. His RAM is the limit.

I have to use realloc() on each pass. I see no way to do this using STL or other means. Of course, as I said, I don't really know anything about STL worth mentioning, but at least with my routines I'm working close to the hardware and I know what's going on. From the little I've seen so far, I'm not sure that's the case with STL and other of the newer C++ classes/operators/routines, etc.
1
2
3
4
5
struct MYSTRUCT
{
    std::string str1;
    std::string str2;
};

In this example using malloc() will leave the std::string objects un-initialized because their constructor's will not be run.

You do have to delete, when you new, which is why you should avoid using new if you do not have to. That is why you should use a std::string rather than a char* and a std::vector<> rather than an array.
Oh, okay. I know about unitialize pointers, but I didn't know that's what you were referring to with the ctor.

In any case, I'm just going to have to read up on STL and get at least a basic handle on STL before I start using it. I've never seen a need for it (maybe because I don't know what it can or cannot do), but I doubt that STL can do anything I can't already do with standard C/C++.

My way may take a little more coding, but at least I'm managing every aspect of the program instead of entrusting it to more OOP.

As I said, I have a number of cases where I have to reallocate memory. If there's nothing in STL or what-have-you that allows me to do that, then I see no reason whatsoever to change so that part of my code is straight C/C++ and part of it is STL and part of it is... and so on.

Thanks for clearing that up for me.

EDIT: I meant to add, this effectively initializes my pointers to zero --

1
2
3
4
5
6
struct MYSTRUCT {
     char* str1;
     char* str2;
};

MYSTRUCT ms = { 0 };

Last edited on
The benefit of using std::string over a char* is that the std::string will do all the things that you would have to do yourself. So you are right in that it will save you having to write allocation and dealocation code. This is also much safer because it is easy to loose track of pointers.

Ultimately that is the power of OO. Your objects manage themselves so you don't have to write the code to manage them every time you use them.

So everything you will have to do with your char* will already be available in the std::string class. Basically a std::string object is a char* combined with all the code necessary to manage it.
Last edited on
Well, this code won't work in a Windows app...

1
2
3
std::string str1;
str1 = "Hello World";
MessageBox(hwnd,str1,szAppName,MB_OK);


I have typecast it and it still won't work. Granted, I'm sure I can figure out how to get it done with a little effort, but the fact that MessageBox() has problems with it is very telling.

It also makes me wonder how many other troubles I would run into in a Windows app by using these newer conventions along with STL, etc.

Because the only programs I write are in Windows.

EDIT: Just in case somebody wanted to show me how to do it, I just looked it up so that this DOES work...

1
2
3
std::string str1;
str1 = "Hello World";
MessageBox(hwnd,str1.c_str(),szAppName,MB_OK);


But I'd have to do a lot of that kind of stuff in a normal Win32 app, and I don't see any benefit in it personally. I'm sure others find it most beneficial, but I don't... at least not yet.
Last edited on
I'm not a WinAPI expert but you can try:
1
2
3
std::string str1;
str1 = "Hello World";
MessageBox(hwnd,str1.c_str(),szAppName,MB_OK);


The std::string::c_str() function returns a const char* version of your std::string.

EDIT: Whoops, too late ;o)
Last edited on
You must have posted right after my edit above. -:)

In any case, you have made me pull out the book. I've forgotten a lot of important things that I need to go over again anyway, but you have cause me to start reading up on more OOP and STL and see what it has to offer. There's no use turning down tools that may be of great help, and I suspect that the majority of programmers use these "advanced" tools because they're helpful.

Thanks for the clarifications.
Topic archived. No new replies allowed.
Pages: 12