473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

why this program is crashing

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

Nov 14 '05 #1
6 1282
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

James wrote:
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

Nov 14 '05 #2
James wrote:
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
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #3
James wrote:
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
Nov 14 '05 #4
On Sun, 17 Apr 2005 07:20:18 -0700, James wrote:
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
Nov 14 '05 #5
On Sun, 17 Apr 2005 16:34:54 +0200, Daniel Etzold <de*****@gmx.de>
wrote:
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

James wrote:
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


<<Remove the del for email>>
Nov 14 '05 #6
On 17 Apr 2005 07:20:18 -0700, "James" <ja********@yahoo.com> wrote:
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>>
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daragoth | last post by:
Hi, I'm writing a program using Metrowerks CodeWarrior 4.0 that determines the best possible combination of a data set by checking every possible combination. I found there were about 250,000,000...
14
by: Java and Swing | last post by:
static PyObject *wrap_doStuff(PyObject *self, PyObject *args) { // this will store the result in a Python object PyObject *finalResult; // get arguments from Python char *result = 0; char *in=...
20
by: ghyott | last post by:
hello, In my opinion the following code should crash when run with *(argv+1)="1234567890" and *(argv+2)="1234567890" . int main(int argc,char **argv) { char buf1; char buf2; char buf3;...
5
by: Kuku | last post by:
My Program is crashing at strupr(e2.n); int main() { struct emp {
3
by: Shawn August | last post by:
Hello: I am converting a working VB6 program to C#. During testing of the C# version, I noticed the ReadFile API is crashing. The parameters going into the this function are identical to the...
17
by: UJ | last post by:
Is there any way for a windows service to start a windows program ? I have a service that will need to restart a windows app if it needs to. TIA - Jeff.
8
by: code break | last post by:
Can Any one tell me why this program is crashing . testFunc() { int a1, *ptr ; f=&a1; f=f+1; return 0; }
2
by: doug | last post by:
Hi all, Can anyone help me with the following dilema: I have been provided with a 3rd party threaded library (say libfoo.a) believed to be stable, it has been in use for a few years and noone...
41
by: z | last post by:
I use Visual C 2005 to develop my programs. One in particular is crashing in very specific and hard to replicate situations, made worse by the fact it only crashes when run -outside- the dev - as...
3
by: plbaldri | last post by:
I have written a very simple and basic program for an intro Comp Sci class and am having trouble with it crashing. The program needs to accept a filename on the command line at the prompt and has to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.