So, I wrote this:

Code:
#include <stdio.h>
void main2()
{
   main();
}
main()
{
   main2();
}


It causes the cmd to crash.
Why does it crash? I mean, it's probably because it's an infinite loop thing, but how does it work? Does the computer stop the cmd when it detects something like this?
I think this is a silly question, but I'm curious

Also, is it possible to set another funtion as a function's parameter? I don't know why I'd do that, I'm really just wondering. If it's possible, how do you do that
Is your question : how does the CMD know it has to crash because of an infinite loop ? If yes, that's a good question and I'd also like to hear the answer. If no, then, well, pfrt.

It's possible to pass functions as parameters using function pointers. Example :


Code:

int foo(char bar)
{
    // do stuff
}

int passTheThing(int (*func)(char), char leParameter)
{
    return (func)(leParameter);
}

int main(void)
{
    return passTheThing(foo);
}
In linux (and probably elsewhere), your stack is given an address space defined by the OS. This address space is mapped to physical memory. When you call functions, the return address, certain arguments, and possible saved registers are pushed (for typical ABIs). When you endlessly recurse, you fill up that address space. When you pass the defined region, you trigger a page fault. This notifies the OS that you are (iirc) faulting/writing outside of the allowed pages in memory, which sends appropriate signals and usually ends with the termination of the program.

(Someone correct me if I get parts wrong, quickly typing this)
AHelper wrote:
In linux (and probably elsewhere), your stack is given an address space defined by the OS. This address space is mapped to physical memory. When you call functions, the return address, certain arguments, and possible saved registers are pushed (for typical ABIs). When you endlessly recurse, you fill up that address space. When you pass the defined region, you trigger a page fault. This notifies the OS that you are (iirc) faulting/writing outside of the allowed pages in memory, which sends appropriate signals and usually ends with the termination of the program.

(Someone correct me if I get parts wrong, quickly typing this)


That makes sense. What is a stack?

matrefeytontias wrote:
Is your question : how does the CMD know it has to crash because of an infinite loop ? If yes, that's a good question and I'd also like to hear the answer. If no, then, well, pfrt.

It's possible to pass functions as parameters using function pointers. Example :


Code:

int foo(char bar)
{
    // do stuff
}

int passTheThing(int (*func)(char), char leParameter)
{
    return (func)(leParameter);
}

int main(void)
{
    return passTheThing(foo);
}


What does the (char) after (*func) do?
So, *func is an int type pointer?

Code:
int (*func)(char)

This means : the parameter "func" is a pointer on a function that takes one char as parameter and returns an int.
matrefeytontias wrote:

Code:
int (*func)(char)

This means : the parameter "func" is a pointer on a function that takes one char as parameter and returns an int.


Shouldn't it be "int *func( char x)" then? Or it can be written both ways?
I must admit I never tried any other way since this one works Razz
Oh, alright

thanks

Code:
int *func(char)
int (*func)(char)
These are not equivalent. The first is a pointer to a function (char) returning int pointer, the second is a pointer to a function (char) returning int.
I'm pretty sure the first one isn't a function pointer at all, it's a declaration of a function that takes a char and returns int*, and should throw an error if placed in any other context besides a forward declaration.
elfprince13 wrote:
I'm pretty sure the first one isn't a function pointer at all, it's a declaration of a function that takes a char and returns int*, and should throw an error if placed in any other context besides a forward declaration.

Oops, read that too fast. Yes, first is a function, seconds is pointer to function.

Dropping cdecl.org here, it works well at explaining types.

BBCode fixed by elfprince
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement