tags:

views:

239

answers:

5

I have a function in which one of the function arguments is an integer. During function invocation I am passing an enumerated datatype to this function. After building using gcc, any access to the INTEGER variable inside the function causes a segmentation fault.

Sample code:

void somefun (unsigned int nState)
{
    switch (nState) // <-- Crashes on this line
    {
        //
        // functionality here ...
        //
    }
}

enum {

    UNDEFINED = -1,
    STATE_NICE,
    STATE_GREEDY
} E_STATE;

int main (int argc, char *argv [])
{
    somefun (STATE_NICE);
}
+1  A: 

First off, The enum is defined in main() and does not exist for somefun(). You should define the enum outside of main, although I cannot see how this is causing a crash.

After defining the enum outside of the main you should define somefun to be somefun( E_STATE nState ) and test again.

gbrandt
+1 those were my thoughts as well...
David Zaslavsky
Well, no; since the argument is declared unsigned int, the function doesn't care about the enum, and since enums in C are ints, the call is simply casting an int to a uint. no problem there.
Charlie Martin
exactly what I thought, and what I said. But it does not hurt to show the proper way to use an enum.
gbrandt
+1  A: 

Actually runs for me:

bash $ cat bar.c
#include <stdio.h>

void somefun (unsigned int nState)
{
    switch (nState) // <-- Crashes on this line
    {
        //
        // functionality here ...
        //
      default:
        printf("Hello?\n");
    }
}

int main (int argc, char *argv [])
{
    enum {

        UNDEFINED = -1,
        STATE_NICE,
        STATE_GREEDY
    } E_STATE;

    somefun (STATE_NICE);
    return 0;
}
bash $ gcc -Wall bar.c -o bar
bar.c: In function 'main':
bar.c:22: warning: unused variable 'E_STATE'
bash $ ./bar
Hello?
bash $

Made a couple of changes, but it ran without them. (1) added a tag in the switch just so it had something; (2) added the #include <stdio.h> and printf so I could tell that it had run; (3) added the return 0; to eliminate an uninteresting warning.

It did run successfully with none of the changes, it just didn't do anything visible.

So, what's the OS, what's the hardware architecture?

Update

The code changed while I was trying it, so here's a test of the updated version:

bash $ cat bar-prime.c
#include <stdio.h>

void somefun (unsigned int nState)
{
    switch (nState) // <-- Crashes on this line
    {
        //
        // functionality here ...
        //
      default:
        printf("Hello?\n");
    }
}

enum {

    UNDEFINED = -1,
    STATE_NICE,
    STATE_GREEDY
} E_STATE;


int main (int argc, char *argv [])
{

    somefun (STATE_NICE);
    return 0;
}
bash $ gcc -Wall bar-prime.c -o bar-prime && ./bar-prime
Hello?
bash $

Still works. Are you getting a core file in your version? Have you tried getting a stack trace?

Charlie Martin
Since nState is not used in the switch at all, is it optimized out. Anyway, gcc version 3.4.6-10 (Redhat), in a Pentium 4 PC.
Alphaneo
Sounds good. Code still works for me. Since nState is being optimised out but isn't being accessed, why would it crash?
Charlie Martin
+1  A: 

I compiled and ran that code exactly (cut & paste) on my computer, using gcc version 4.2.4, with no errors or segmentation fault. I believe the problem might be somewhere else.

Colin
A: 

Your situation is like specific to sun sparc hardware or similar. Please post uname -a and output of dmesg

vitaly.v.ch
A: 

From all your answers it seems that the code is logically correct, and I need to investigate the real reason for the crash. I will investigate it and post it soon.

Alphaneo