Solved

convert C->C++ : malloc

Posted on 2004-09-25
7
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

688 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