is there a difference between malloced arrays and newed arrays
I'm normally programming in c++, but are using some clibrary functions fo开发者_开发百科r 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
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: What is the difference between new/delete and malloc/free?
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():
- http://linux.die.net/man/3/getline
- http://www.kernel.org/doc/man-pages/online/pages/man3/getline.3.html
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.
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++.
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! :)
精彩评论