why this program is crashing

J

James

hello,
The program given below is giving segmentation fault. By debugging I
inferred that the problem is in the statement t1.f(), but I am not sure
that what is the problem.

#include <stdio.h>

struct type {
int i;
void (*f)(void);
};

void funct(void)
{
printf("this is funct\n");
return;
}

int construct(struct type t1,int i,void (*f)(void))
{
t1.i=i;
t1.f=f;
return 0;
}

int main(void)
{
struct type t1;
printf("This is main\n");
construct(t1,2,&funct);
t1.f(); // Most probably the problem is in this line.
return 0;
}

Can anybody please tell me that what is the real problem?

Thanks
 
D

Daniel Etzold

This works:

[...]

int construct(struct type* t1,int i,void (*f)(void))
{
t1->i=i;
t1->f=f;
return 0;
}

int main(void)
{
struct type t1;
printf("This is main\n");
construct(&t1,2,&funct);
t1.f(); // Most probably the problem is in this line.
return 0;
}

You have to call "construct" with the address of the struct.

Regards,
Daniel
 
A

Artie Gold

James said:
hello,
The program given below is giving segmentation fault. By debugging I
inferred that the problem is in the statement t1.f(), but I am not sure
that what is the problem.

#include <stdio.h>

struct type {
int i;
void (*f)(void);
};

void funct(void)
{
printf("this is funct\n");
return;
}

int construct(struct type t1,int i,void (*f)(void))
You realize, of course, that `t1' is being passed by value? Any change
you make to `t1' only affects the local copy.
{
t1.i=i;
t1.f=f;
return 0;
}

Make that:

int construct(struct type * t1, int i, void (*f)(void))
/* why are you bothering to return anything here? */
{
t1->i = 1;
t1->f = f;
return 0;
}
int main(void)
{
struct type t1;
printf("This is main\n");
construct(t1,2,&funct);
You should have looked at the value of t1 *here*. ;-(
construct(&t1, 2, funct);
t1.f(); // Most probably the problem is in this line.
return 0;
}

Can anybody please tell me that what is the real problem?
HTH,
--ag
 
M

Martin Ambuhl

James said:
hello,
The program given below is giving segmentation fault. By debugging I
inferred that the problem is in the statement t1.f(), but I am not sure
that what is the problem.

[OP's code replaced]

#include <stdio.h>

struct type
{
int i;
void (*f) (void);
};

void funct(void)
{
printf("this is funct\n");
return;
}

int OPs_construct(struct type t1, int i, void (*f) (void))
{
t1.i = i;
t1.f = f;
return 0;
}


int corrected_construct(struct type *t1, int i, void (*f) (void))
{
t1->i = i;
t1->f = f;
return 0;
}

int main(void)
{
struct type t1 = { 0, 0 };
printf("t1.i starts as %d.\n"
"It should be 2 after \"construct\"\n", t1.i);
OPs_construct(t1, 2, funct);
printf("After calling OP's construct function, \n"
" t1.i = %d\n", t1.i);
printf("Since the OP's construct function doesn't work,\n"
" we won't bother with 't1.f();'\n\n");

corrected_construct(&t1, 2, funct);
printf("After calling the corrected construct function, \n"
" t1.i = %d\n", t1.i);
printf("Calling t1.f:\n");
t1.f();
return 0;
}



[output]
t1.i starts as 0.
It should be 2 after "construct"
After calling OP's construct function,
t1.i = 0
Since the OP's construct function doesn't work,
we won't bother with 't1.f();'

After calling the corrected construct function,
t1.i = 2
Calling t1.f:
this is funct
 
R

Robert Gamble

hello,
The program given below is giving segmentation fault. By debugging I
inferred that the problem is in the statement t1.f(), but I am not sure
that what is the problem.

#include <stdio.h>

struct type {
int i;
void (*f)(void);
};

void funct(void)
{
printf("this is funct\n");
return;
}

int construct(struct type t1,int i,void (*f)(void))
{
t1.i=i;
t1.f=f;
return 0;
}

int main(void)
{
struct type t1;
printf("This is main\n");
construct(t1,2,&funct);
t1.f(); // Most probably the problem is in this line.
return 0;
}

Can anybody please tell me that what is the real problem?

Thanks

When you call your "construct" function you are passing a copy of t1 on
which the function operates, the original struct is unchanged, this is
because arguments are passed by-value in C as opposed to by-reference.
When your program calls t1.f(), it is trying to execute whatever happens
to be at the (uninitialized) address stored in t1.f causing your
segmentation fault. You have a couple of options, you can return a struct
from your "construct" function assigning the new value to your old struct,
or you can pass your function a pointer to the struct and have the
function operate on the original struct.

Example 1 (Return struct):

/* Change return type */
struct type construct(struct type t1, int i, void (*f)(void))
{
t1.i=i;
t1.f=f;
return t1; /* Return the modified copy */
}

int main(void)
{
struct type t1;
printf("This is main\n");
t1 = construct(t1, 2, &funct); /* Store the return value in t1 */
t1.f();
return 0;
}

Example 2 (Pass pointer to struct):

/* Change first parameter to be pointer to struct */
int construct(struct type * t1, int i, void (*f)(void))
{
t1->i=i; /* Operate on original struct */
t1->f=f;
return 0;
}

int main(void)
{
struct type t1;
printf("This is main\n");
construct(&t1, 2, &funct); /* Pass address of t1 */
t1.f();
return 0;
}


Rob Gamble
 
B

Barry Schwarz

This works:

[...]

int construct(struct type* t1,int i,void (*f)(void))
{
t1->i=i;
t1->f=f;
return 0;
}

int main(void)
{
struct type t1;
printf("This is main\n");
construct(&t1,2,&funct);
t1.f(); // Most probably the problem is in this line.
return 0;
}

You have to call "construct" with the address of the struct.

Or return the updated copy of the struct back to the calling function.
Regards,
Daniel



<<Remove the del for email>>
 
B

Barry Schwarz

hello,
The program given below is giving segmentation fault. By debugging I
inferred that the problem is in the statement t1.f(), but I am not sure
that what is the problem.

#include <stdio.h>

struct type {
int i;
void (*f)(void);
};

void funct(void)
{
printf("this is funct\n");
return;
}

int construct(struct type t1,int i,void (*f)(void))

struct type construct(...
{
t1.i=i;
t1.f=f;
return 0;

return t1;
}

int main(void)
{
struct type t1;
printf("This is main\n");
construct(t1,2,&funct);

t1 = construct(...
t1.f(); // Most probably the problem is in this line.
return 0;
}

Can anybody please tell me that what is the real problem?

Thanks



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top