0

I'm trying to execute a very simple program that runs "ls" command Im working under Mac OS 10.7, with XCode and C++ This is the code:

#include <iostream>
using namespace std;

int main(void)
{
    cout << "Hello world" << endl;
    execl("/bin/ls","ls",NULL);
    return 0;
}

It crashes after following output

Hello world

No memory available to program now: unsafe to call malloc

I tried to google it but no luck, any ideas on what I might be doing wrong?

Community
  • 1
  • 1
Dabrorius
  • 1,039
  • 1
  • 8
  • 17
  • That code won't compile. Can you post a complete program? – Robᵩ Mar 28 '12 at 15:02
  • there you go, #include and using namespace std were missing – Dabrorius Mar 28 '12 at 15:16
  • It still won't compile. I can only assume that you really *aren't* running this program, but some other program that looks sort of like it. If you won't show us the actual program that breaks, there isn't much that we can do. – Robᵩ Mar 28 '12 at 15:18
  • it is exactly the program I'm trying to run (copy/paste), I don't know why you can't compile it. Anyway Francesco figured it out so it works now :) Thanks a lot everybody. – Dabrorius Mar 28 '12 at 15:37

3 Answers3

2

This is just "my opinion"

From man page:

The exec family of functions replaces the current process image with a new process image.

It could be that it tries to replace the debugger process and so it crashes (the app is run from Xcode..). If you execute the app from command line it works...

Francesco
  • 1,840
  • 19
  • 24
  • You sir are 100% right :) I also figured out that if you fork the process first it works fine even from xcode. Thanks a lot. – Dabrorius Mar 28 '12 at 15:35
0

Seems to work fine:

http://ideone.com/8AoZ3

But seems like on your platform some sort of weird recursion is taking place. Can you change your call to:

execl("/bin/ls","/bin/ls",0);
Sid
  • 7,511
  • 2
  • 28
  • 41
  • I still get the same error. Also it's not just ls, but every other program too. :( And when it crashes I can see in Xcode that only one thread is running so I guess it's probably not a recursion? – Dabrorius Mar 28 '12 at 15:03
  • I've never used xcode but obviously this doesn't seem related to C/C++. Did you restart xCode? Maybe try the debugger and step line by line and see what happens. Sorry, just random ideas. Try increasing the stack size of your system too for example. – Sid Mar 28 '12 at 15:17
  • Well it get's stuck in bunch of assembly code (at least I think its assembly) Here is a screenshot http://www.deviantpics.com/?v=vdx39.png. I tried restarting XCode and OS X but it didn't help. I tried increasing stack size as explained here http://stackoverflow.com/questions/2092495/increase-stack-size-with-xcode but it didin't work either. – Dabrorius Mar 28 '12 at 15:26
0

I know this may not be exactly what you want to do, but the following SO question is using execv to execute echo:

how-to-create-a-process-on-mac-os-using-fork-and-exec

Community
  • 1
  • 1
Peter M
  • 7,309
  • 3
  • 50
  • 91