Detect direction of stack growth

From: Adam Warner (usenet_at_consulting.net.nz)
Date: 12/27/04


Date: Mon, 27 Dec 2004 14:47:24 +1300

Hi all,

Is this a (C99) portable way to detect whether the C stack grows upwards
(1) or downwards (-1)?

#include <stdio.h>

int stack_direction=0;

void detect_stack_direction(void * stack_start) {
  void * stack_current;
  stack_direction=-1;
  if (&stack_start<&stack_current) stack_direction=1;
}

int main() {
  void * stack_start;
  detect_stack_direction(stack_start);

  printf("Stack grows: %i\n", stack_direction);
  return 0;
}

Thanks,
Adam



Relevant Pages

  • [PATCH] leds-ams-delta: Fix wrong loop direction on removal
    ... We want to go upwards, not downwards. ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)
  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • [PATCH] get rid if __cpuinit and __cpuexit
    ... unsigned long action, void *hcpu) ... unsigned int cpu = hcpu; ... -static int __cpuinit ... __cpu_up(unsigned int cpu) ...
    (Linux-Kernel)
  • [PATCH,RFC 2.6.14 09/15] KGDB: SuperH-specific changes
    ... This adds basic support for KGDB on SuperH as well as adding some architecture ... -static int kgdb_uart_getchar ... -static void kgdb_uart_putchar ... * The command-line option can include a serial port specification ...
    (Linux-Kernel)
  • problem in java swings
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)

Loading