473,654 Members | 3,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(struc t 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 1292
This works:

[...]

int construct(struc t 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(struc t 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(struc t 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(struc t 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(s truct type t1, int i, void (*f) (void))
{
t1.i = i;
t1.f = f;
return 0;
}
int corrected_const ruct(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(t 1, 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_const ruct(&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(struc t 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(struc t 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(struc t 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(struc t 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(struc t 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********@yah oo.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(struc t 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
2195
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 possibilities and decided to insert a cout in the loop that gave the percentage that had been completed. The program worked, but I soon realized it would take almost a month of execution time for it to finish. I figured the cout was slowing the...
14
2724
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= 0; char *aString = 0; char *bString = 0; MY_NUM *a;
20
1506
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; strncpy(buf2,*(argv+1),sizeof(buf2));
5
1983
by: Kuku | last post by:
My Program is crashing at strupr(e2.n); int main() { struct emp {
3
3241
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 working VB6 version. I have tried changing the returned buffer datatype from string to object. The program does not crash (no work) if I remove the ref argument on the 2nd argument within the ReadFile API ('psBuffer'). A quick API sniffer revealed:...
17
6429
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
2037
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
1454
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 has complained about it crashing. The program seems to run fine.
41
2444
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 an exe, not from the Debug option. If I try to launch the debug on the crashing program, it'll close before the debugger opens. Any suggestions as to how I should proceed? Thanks in advance.
3
2499
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 verify that the filename has a .txt extension. The code below works great as long as a '.' is entered regardless of the extension, but if no '.' is entered into the file name the program crashes. I'm sure it has to do with the fact the program is...
0
8376
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8489
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.