(long*)malloc(STACKSIZE);

Status
Not open for further replies.

Ajar

Ars Tribunus Angusticlavius
9,701
Subscriptor++
I want to use setjmp and longjmp to context switch between child processes and between child processes and the parent process, but I'm having some major trouble setting up my stack space.

...probably because I've never done this before. :)

Here's what I'm doing:

code:<HR>
Code:
#define STACKSIZE 16384;<P>
int main(){<P>
pid_t CurrentID = getpid();<P>
//create first child with a fork
if(CurrentID = fork() == 0){
	
   //set up stack for jump buffer
   long *stack;
   stack = (long*)malloc(STACKSIZE); <----------//IT B0RKS ON THIS LINE...<P>
   while(1){
      //do stuff, then setjmp and pause()
      }<P>}<P>
//...more parent stuff...<P>
return 0;
}

gcc gives me a error while compiling, saying "parse error before ')'" in the line (long*)malloc(STACKSIZE);, and I have no idea why. It doesn't help that that line of code came from my professor...

Anyone know?

...and can anyone point me to a decent overview of implementing context switches with setjmp and longjmp? I've read the man pages, and they're very helpful, but I'd like an alternate resource as well...

Thanks again :)
 

Ajar

Ars Tribunus Angusticlavius
9,701
Subscriptor++
Dear god, I'm clearly some sort of idiot... :)

Guess I've been going for too long today :p

But dammit, it's fun, and I'm learning SO much...

...I want to set the stack pointer now. Isn't that a low-level call? Do I need to include anything special to do it? The professor has said set_sp() is defined in set.s, but I don't know where that is or what to do with it it...
 

Ajar

Ars Tribunus Angusticlavius
9,701
Subscriptor++
If I knew how to do that... :)

He wouldn't be annoyed if I did it differently, I don't think... but right now I'm just looking for some means of moving the stack pointer, and some sort of decent implementation documentation.

What I have for documentation right now is this.

For someone who knows next to nothing about assembler, and only marginally more about UNIX, it really isn't that helpful *sigh*
 

DrPizza

Well-known member
93,870
Originally posted by Stephan T. Lavavej:
In ANSI C:
1) Declare main as either int main(void) or int main(int argc, char *argv[]).
2) Do not cast the return value of malloc.
Unless he's using a C++ compiler, in which case an empty parameter list is identical to <span style="font-family: 'Lucida Console', monospace;">void</span> and the return value of <span style="font-family: 'Lucida Console', monospace;">malloc()</span> should be cast from <span style="font-family: 'Lucida Console', monospace;">void*</span> to whatever the appropriate type is, because to fail to do so generates an error.
 
Originally posted by Stephan T. Lavavej:
C++ is not C. Note how I didn't say, "In ANSI C++".
Notice, I didn't say he needed to be writing C++, he could simply be using a C++ compiler (to benefit from stricter warnings).

Either way -- why is casting the return value of <span style="font-family: 'Lucida Console', monospace;">malloc()</span> be wrong?
 

limbs

Ars Scholae Palatinae
1,058
PeterB:
Either way -- why is casting the return value of malloc() be wrong?
Because it can hide an error. ANSI C guarantees that voids are automatically casted up to the appropriate type. If you specifically cast the return of malloc (or realloc, calloc etc. for that matter) you can cause the compiler to miss a warning.

Try writing a C program using malloc and cast the return value. Now "forget" to have the #include <stdlib.h> line in your code (or the library that holds the malloc definition. See how it compiles OK. See how it crashes horribly....

Simple answer (apart from don't be stupid and forget to include stdlib ;) ) ....
Never, ever cast the return from malloc .... ever. Assuming you are using ANSI C.
 
Originally posted by limbs:
Quote: "PeterB:
Either way -- why is casting the return value of malloc() be wrong?"Because it can hide an error. ANSI C guarantees that voids are automatically casted up to the appropriate type. If you specifically cast the return of malloc (or realloc, calloc etc. for that matter) you can cause the compiler to miss a warning.

Try writing a C program using malloc and cast the return value. Now "forget" to have the #include <stdlib.h> line in your code (or the library that holds the malloc definition. See how it compiles OK. See how it crashes horribly....
Well that's the problem. It doesn't crash horribly. The only thing casting the return value is to remove a warning. The memory allocation succeeds either way. The executable links correctly, and has a loadtime dynamic link to the function in question. (gcc --ansi 2.95.4 on Linux, gcc --ansi 2.95.3-5 on Cygwin, MSC/C++ compiler v.13, MSC/C++ compiler v.12). None of the compilers I have access to will crash if I omit the header; all will correctly allocate the memory.
 
Status
Not open for further replies.