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

I changed malloc to new and chaned free to delete, but I still 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]

6
  • 5
    You have to #include the appropriate file. Commented Feb 22, 2013 at 6:15
  • malloc is valid c++ Commented Feb 22, 2013 at 6:15
  • Can you build the source code? Commented Feb 22, 2013 at 6:16
  • 1
    @KarthikT, but its not a good idea to mix Commented Feb 22, 2013 at 6:18
  • yep #includeing <stdlib.h> helps, also go for <string.h> as you're using memset(). I compiled the code doing those mods. Commented Feb 22, 2013 at 6:20

3 Answers 3

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++.

Sign up to request clarification or add additional context in comments.

6 Comments

@mahmood Just noticed that, it does not include stdlib however, and that is the issue here.
@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..
@KarthikT, it might answer it but it is not the "right" answer, please remove the .h include and only show the <cstdlib> and I will remove the downvote.
@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.
@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
|
0

Have you tried to include

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

and use g++?

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.