0

I was wonder how malloc would allocate memory (can you tell me ?), so I tried something.

Is this a bad way to allocate memory ?

void* myMalloc(unsigned int size) {
    return (void*) new bool[size];
}
Niklas R
  • 16,299
  • 28
  • 108
  • 203
  • 5
    Are you aware that a bool occupies more space than a single byte? Or to be more accurate, there's nothing that says it *must* be one byte. The amount of actual memory this allocates may change on other architectures or compilers. This is very bad. – Chris Eberle Oct 06 '11 at 20:06
  • Doesn't answer the question, but usually, `new` calls `malloc` (or goes straight to whatever lower-level function the default `malloc` is a wrapper for), not the other way around. –  Oct 06 '11 at 20:07
  • 1
    So what's so bad about `malloc` anyway that makes you want to do this? – riwalk Oct 06 '11 at 20:08
  • @Stargazer712 I think the OP is under the assumption that `malloc` invokes `new`. – Chris Eberle Oct 06 '11 at 20:09
  • 2
    The `size` parameter should be of type `size_t`, and the `new` expression should be `new char[size]`. – Keith Thompson Oct 06 '11 at 20:12
  • 1
    your "Malloc" isn't a `malloc`, it's a `new`. – John Dibling Oct 06 '11 at 20:29

5 Answers5

6

The C++ standard states explicitely that the C malloc and free functions must not call operator new or operator delete (20.6.13/3 and 4 in the C++11 FDIS). This makes a red light blink in my mind...

Aside from that, your approach dumps all type-safety new could give you that malloc lacks. And your implementation will be too slow for what it does.

Conclusion: yes, this is a bad way to allocate memory.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • can you please quote the standard? – Luchian Grigore Oct 06 '11 at 20:21
  • 1
    -1 The standard says malloc and free do not **attempt** to call new or delete. That's quite different than "must not call". That wouldn't even make sense since they're built-in functions, so you couldn't possibly modify how they work. – Luchian Grigore Oct 06 '11 at 20:34
  • Luchian: I think you misunderstand my point. The C functions made available in C++ `malloc` and `free` will not attempt, ergo will not call C++'s `new` or `delete`. That's why I said it's probably a bad idea to define a user-made malloc that uses `new`. – rubenvb Oct 06 '11 at 20:40
  • I completely agree that it's a bad idea. I also understood your point but you can also make a point without miss-quoting the standard. – Luchian Grigore Oct 06 '11 at 20:45
  • Luchian: I could hardly just say "calling new from a malloc is bad" without any form of precedent or argumentation. I just used the first argument that came into my mind, which is quite compelling IMHO... – rubenvb Oct 06 '11 at 20:47
  • "The C++ standard states explicitely that the C malloc and free functions must not call operator new or operator delete" - this is not an argument - this is simply a FALSE statement, as the C++ standard states no such thing. Also "calling new from a malloc" is also wrong, since there is only ONE malloc. Please understand that I agree that it's wrong to do it, I don't advise it, however I am in disagreement with your argument for the sheer fact that it's misleading - which it is. – Luchian Grigore Oct 06 '11 at 20:53
  • Luchian: The community disagrees with your interpretation (see point 7 in the malloc/free half of the answer): http://stackoverflow.com/questions/240212/what-is-the-difference-between-new-delete-and-malloc-free/240308#240308 – rubenvb Oct 06 '11 at 21:25
4

The are some mistakes:

1) you're assuming sizeof(bool) == 1, which is not necessarily true. Change the

return (void*) new bool[size];

to

return (void*) new char[size];

2) how will you free the memory? You'd have to do something like:

char* x = myMalloc(unsigned int size);
delete[] x; //not free, since you actually use new and not malloc

3) malloc doesn't call new, it's probably the other way around (in VS20xx new will call malloc). malloc also doesn't call the constructors, so if this does work, it will only work for basic types.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • How do you know that `new` calls `malloc()`? – Keith Thompson Oct 06 '11 at 20:21
  • We [discussed this](http://stackoverflow.com/questions/7443782/does-dynamic-memory-allocation-differ-in-c-and-c-in-popular-implementations) a while ago, and it seems that all popular platforms implement `::operator new()` in terms of `malloc()`. – Kerrek SB Oct 06 '11 at 20:47
3

I hate to disagree with quite so many people recommending that you use new char[n], but I feel obliged to do so.

Since you want to allocate "raw" memory, not objects, you should really use ::operator new instead of new some_type[some_size]:

 void *myMalloc(size_t size) { 
     return ::operator new(size);
 }

Ultimately, new char[whatever] isn't particularly harmful, but (at least IMO) it's conceptually wrong, and I see no advantage over using ::operator new directly.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Thanks for your good answer. Can you tell me, When **::operator new** uses *malloc()*, what uses malloc then ? – Niklas R Oct 07 '11 at 12:46
  • `malloc` will normally use something supplied by the underlying OS such as `brk` (or `sbrk`) on Unix, `HeapAlloc` on Windows, etc. Also note that while *early* implementations of C++ often implemented `::operator new` by calling `malloc`, on a modern system, it'll probably go directly to the OS as well. – Jerry Coffin Oct 07 '11 at 15:24
0

malloc()/free() is not necessarily compatible with new and delete. new might very well invoke malloc() (at least by default), but you shouldn't count on it.

One major difference between malloc() and new() is that malloc() returns a void* that points to raw memory, whereas new returns a typed pointer and calls the constructor, if any (and delete calls the destructor).

If you're writing C++, there is very rarely a good reason to allocate raw untyped memory.

But if you really want to do so, you can write something like:

void *p = (void*)new char[size];
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

If you want to allocate a certain number of bytes with new[], use new char[n]. Unlike bool, sizeof(char) is guaranteed to be 1.

As to implementation of new and malloc, new is a higher-level construct (as it also calls constructors for types that have those), and therefore likely implemented in terms of malloc, not the other way round.

UncleBens
  • 40,819
  • 6
  • 57
  • 90