views:

257

answers:

3

I'm normally programming in c++, but are using some clibrary functions for my char*. Some of the manpages like for 'getline', says that input should be a malloced array.

Is it ok, to use 'new' instead?

I can see for my small sample that it works, but could this at some point result in some strange undefined behavior?

I know that a 'new' should match a 'delete', and a 'malloc' with a 'free'.

I'm also not using std::string. And this is intentional.

Thanks

+2  A: 

Yes, it's OK to use a pointer allocated with new when a "malloced" one is expected.

By the way, getline isn't ISO C. There's a getline in standard C++ library, but that one expects a std::string. For standard C file reading you should use fgets. The following works (simplified code assuming existence of infile and doesn't check fgets return value - which you probably should in real code):

// infile is some open FILE* object
int mylen = 100;
char* line = new char[mylen];
fgets(line, mylen, infile)

However, a mandatory disclaimer: it's much better to use std::string and getline if you're using C++.

Eli Bendersky
I know the original C standard came out of ANSI but that's a national body and ISO is now responsible for the standard. Please try to use "ISO C" if you can.
paxdiablo
@paxdiablo: fixed to "ISO C". using ANSI is just a habit from times past :-)
Eli Bendersky
It is not always ok to use new(ed) memory where malloc(ed) is expected. If it just uses the memory as raw memory fine. But if memory management routines are used on the pointers then you can not make this assumption. getline() may potentially call realloc() on the pointer passed. This is equivalent to calling free() on the pointer i.e. it is undefined behavior.
Martin York
@Martin: getline doesn't really accept a char* - do you mean another routine? fgets will not call realloc
Eli Bendersky
+1  A: 

Hi,

It is perfectly fine to have a 'new'ed array instead of 'malloc'ed array for the purpose.

The differences between the two are (to list some of them) :

  • new/delete call the constructor/destructor of the associated object unlike malloc/free. The latter can not perform initialisation/deinitialisation stuffs.
  • new returns a pointer of proper type but the pointer what malloc returns has to be typecasted (C++)
  • new/delete does not have realloc alternative unlike new/delete

These things are not going to make much of difference in your program; so as stated above, it is perfectly fine to go for 'new'

Your code will not fail, unless new or malloc fails, in which case, 'new' throws an exception, and malloc return a NULL pointer.

EDIT: For the purpose, the array must be 'malloc'ed. I guess I was wrong back then! Thanks Martin! :)

Srivatsan Iyer
@Srivatsan Iyer: Read this for more differences: http://stackoverflow.com/questions/240212/what-is-the-difference-between-new-delete-and-malloc-free
Prasoon Saurav
Thanks! Actually I just tried to put in *some* of the points; not most of them! :P
Srivatsan Iyer
__FAIL__ : This is incorrect the lines MUST be malloced. See below.
Martin York
-1, Martin is correct. A `new[]`ed array may cause strange behavior in this particular case.
greyfade
+13  A: 

The buffer passed to getline() MUST be malloced.

The reason is that getline() may call realloc() on the buffer if more space is required.

realloc() like free() should only be used with memory allocated by malloc(). This is because malloc() and new allocate memory from different storage areas:

See: http://stackoverflow.com/questions/240212/what-is-the-difference-between-new-delete-and-malloc-free/240308#240308

Basically new uses "The "Free Store" while malloc uses "The Heap". Both of these areas are part of the "application Heap" (Though the standard does not actually require an application heap as that is an implementation detail). Though they are both on the "Application Heap" these areas need not overlap. Whether they do is a detail of the implementation.

The man page for getline():

Notice this line:

Alternatively, before calling getline(), *lineptr can contain a pointer to a malloc()-allocated buffer *n bytes in size. If the buffer is not large enough to hold the line, getline() resizes it with realloc(), updating *lineptr and *n as necessary.

Martin York
@Martin: you're pointing to a GCC-extension function "getline". I don't think it's too great to recommend using this non-portable function, especially to newbies
Eli Bendersky
@Eli: the question specifically mentions `getline`.
Pavel Minaev
@Eli: I would recommend against any C functions where there is a usable C++ alternative. But the question is specifically asking about the C getline() functionality. And when the documentation specifically mentions using malloc(ed) memory there is usually a very good reason to-do so.
Martin York
@Martin: Thanks a ton! I guess I have been so ignorant about this. :)
Srivatsan Iyer