Solved

convert C->C++ : malloc

Posted on 2004-09-25
7
237 Views
Last Modified: 2012-05-05
Hi, I have a question:
I want to know how malloc should be "converted to" C++ in the following case:

°°°°°°°header°°°°°°
typedef struct A_STRING
{
      int16*                  buffer;
      unsigned long      length;
}_STRING, *P_STRING;
°°°°°°°°°°°°°°°°°°°

°°°°°°°cpp°°°°°°°°°
P_STRING pStr = new _STRING;
int a ;//used to find how much elements there are in
pStr->buffer = (int16 *) malloc(a);
°°°°°°°°°°°°°°°°°°°

DEEGBAL

0
Comment
Question by:DEEGBAL
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12150646
You still can use 'malloc()'  in C++, but if you want, you can switch to 'new' here also:

P_STRING pStr = new _STRING;
int a ;//used to find how much elements there are in
pStr->buffer = new int16 [ a];
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 75 total points
ID: 12150761
>>> You still can use 'malloc()'  in C++

... but you shouldn't.


class String
{
private:
     int16*               buffer;
     unsigned long    length;
public:
     String() : buffer(NULL), length(0) { }
     String(int a) : buffer(new int16[a]), length(a) { }
    ~String() { delete [] buffer; }
};

typedef String* StringPtr;

void main()
{
        int a = getAFromeSomeWhere();
       StringPtr pStr = new String(a);
}

Regards, Alex
0
 
LVL 86

Expert Comment

by:jkr
ID: 12150800
>>... but you shouldn't.

Why not for POD? ANd how have I been wrong so that code needed some correction?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 15

Expert Comment

by:efn
ID: 12152564
>> Why not for POD?

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.3

An additional consideration is that avoiding malloc will make it easier for future young punk upstart whippersnappers who didn't learn C before C++ to maintain your code.  More generally, the fewer different ways you do things, the simpler, the better.

On the other hand, if you expect your code to be maintained in the future by crusty old C programmers, maybe you should avoid the new operator.

--efn
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 12153312
Why you shouldn't mix them is because it is error prone...

For example, the following snippets both have a serious error:

char *x = (char *)malloc(15);
delete x;  /* Can't mix  malloc and delete : undefined behavior */

char * y = new char[15];
free(y); /* Can't mix new and free : undefined behavior */


This is most important when writing functions whose result is dynamically
allocated, and the caller needs to manage the memory.

There is a great risk that the programmer will mistakenly use the wrong convention
for releasing the memory.

To retain your development team's sanity, it is best to choose one of the conventions
and stick to it:  Either always use 'new' or always use 'malloc()', but don't mix them if
it can be avoided.

(One case where it cannot be avoided is if you need to resize the memory area...
there isn't an equivalent to C++'s realloc() provided for memory allocated with the
new[] operator)

new is often best to use, because that is what most C++ programmers and the C++ libraries
are using

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12154299
>>>> ANd how have I been wrong so that code needed some correction?

jkr, what did you make come to the assumption that my code was a correction of your's. If i make corrections i post the wrong code and the corrected one. I thought there is a common accordance between C++ programmers not to mix C++ and C style, especially if there isn't any need for that. As your first statement about malloc and C++ was technically correct, but in contradiction to that or at least misunderstandable, i made an objection and posted C++ code to demonstrate the C++ way how to solve these kind of tasks.

BTW, your code wasn't quite correct as the variable "a" had not been initialized and the questioner made "malloc(a)"  while your code made "new int16[a]", what most likely is better but not the same.

Regards, Alex

0
 

Author Comment

by:DEEGBAL
ID: 12154327
Hi,
first of all, thanks all for your help and your comments.
Since I copied-and-pasted the code of itsmeandnobodyelse, and everything directly worked fine, I'll give him the points.

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question