0

I have a .cc file which uses both iostream and malloc. How can I compile that? using g++, it says

 error: 'malloc' was not declared in this scope

using gcc, it says

 fatal error: iostream: No such file or directory

The source code is located at http://sequitur.info/sequitur_simple.cc

UPDATE

I changed malloc to new and chaned free to delete. Still I get a lot of errors. For example

 /usr/include/c++/4.6/new:103:14: error:   initializing argument 2 of âvoid* operator new(std::size_t, void*)â [-fpermissive]
mahmood
  • 23,197
  • 49
  • 147
  • 242

3 Answers3

5

Either include <stdlib.h> or include <cstdlib> and change malloc to std::malloc - compile with g++. Including <cstdlib> is the prefered way for new C++ code, "name.h" style is deprecated in C++.

While this will fix you problem, it might be a better idea to migrate to new/delete, to be more consistantly C++.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • @mahmood Just noticed that, it does not include stdlib however, and that is the issue here. – Karthik T Feb 22 '13 at 06:18
  • @JoshPetitt the question is how to compile, and this answers it. There is no reason to downvote, if you wish to advocate new/delete you are welcome to answer it as well.. – Karthik T Feb 22 '13 at 06:21
  • @KarthikT, it might answer it but it is not the "right" answer, please remove the .h include and only show the and I will remove the downvote. – Josh Petitt Feb 22 '13 at 06:23
  • @JoshPetitt it is a valid fix, and I would rather leave it in for completeness, especially since with the `using namespace std;` statement, both are all but identical. Unless you can give a concrete reason. I have already added guidelines to my answer. – Karthik T Feb 22 '13 at 06:26
  • @KarthikT I don't know if it matters with gcc, but MSVC++ it did matter and if you used both, there would be problems when linking to the standard lib. (probably bad example to use MSVC++ since it doesn't really support C that well) [why do some includes need the h and others do not](http://stackoverflow.com/questions/4404725/why-do-some-includes-need-the-h-and-others-not) – Josh Petitt Feb 22 '13 at 06:30
  • Yes. But please let me write a new question. The thread will be confusing – mahmood Feb 22 '13 at 06:34
0

Have you tried to include

#include <stdio.h>      
#include <stdlib.h>   

and use g++?

ChaosCakeCoder
  • 367
  • 1
  • 8
0

use new and delete in C++ code. Don't mix new and malloc. From the code you posted, there isn't any reason AFAIK you can't use new and delete

Josh Petitt
  • 9,371
  • 12
  • 56
  • 104
  • 2
    Yes to prefer `new`/`delete` in C++ code, but mixing `new` and `malloc` is perfectly valid, won't confuse memory managers, and in some edge cases may be desirable. – congusbongus Feb 22 '13 at 06:36