473,508 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segment fault with pointer to method

I have a program that compiles without errors, but when I run it I get a
segment fault.

I have managed to trace where it happens, but since im new to C I don't know
how to fix this or how I did it wrong.

In main lets say I have 2 variables var1 and var2 that has values that I
want to pass to a method for calculations with them. But I want to work with
them later from main so I thought I'd make them pointers.

So I called them to a method so: method(&var1, &var2)

and the method receives them so: void method(int *var1, *var2)

All this works..but when I try to get the numbers that are supposed to be in
var1 and var2 I get some long crazy numbers (like -1218520428) and I am
guessing these are the address to the int I want?

And the segment error happens when I use these in an array
(array[var1][var2]) because it gets extremely out of bounds.

But how do I get the values and not the address?

I tried grabbing them from the pointer and placing it in a new variable like
this: int var = (int)var1

But the new variable also has the huge address value

So my question is, what am I doing wrong? How to I access the actual value
thats in main from the other method and not the address?

I need the number to calculate with, and the starting number has to be
created in main.
Hope someone can help.

-Juggernaut

P.S. I can post the actual code, but I thought It wouldn't be necessary
since I know whats wrong, but just not how I do it right.

Nov 14 '05 #1
3 1304
Juggernaut wrote:
I have a program that compiles without errors, but when I run it I get a
segment fault.

I have managed to trace where it happens, but since im new to C I don't know
how to fix this or how I did it wrong.

In main lets say I have 2 variables var1 and var2 that has values that I
want to pass to a method for calculations with them. But I want to work with
them later from main so I thought I'd make them pointers.

So I called them to a method so: method(&var1, &var2)

and the method receives them so: void method(int *var1, *var2)

All this works..but when I try to get the numbers that are supposed to be in
var1 and var2 I get some long crazy numbers (like -1218520428) and I am
guessing these are the address to the int I want?

<snip>
So my question is, what am I doing wrong? How to I access the actual value
thats in main from the other method and not the address?


Since you're passing the addresses to method() from main, you need to
dereference the pointers in method().

i.e. method() should operate with/on *var1, *var2 to get the values
from main. As you correctly state, var1 and var2 themselves are the
addresses of var1, var2 in main's scope.

Don
Nov 14 '05 #2
"Juggernaut" <ju************@hotmail.com> writes:
I have a program that compiles without errors, but when I run it I get a
segment fault.

I have managed to trace where it happens, but since im new to C I don't know
how to fix this or how I did it wrong.

In main lets say I have 2 variables var1 and var2 that has values that I
want to pass to a method for calculations with them. But I want to work with
them later from main so I thought I'd make them pointers.
C doesn't have "methods"; you mean you want to pass them to a function.
So I called them to a method so: method(&var1, &var2)

and the method receives them so: void method(int *var1, *var2)


void method(int *var1, int *var2)
{
printf("var1 = %p, *var1 = %d\n", (void*)var1, *var1);
printf("var2 = %p, *var2 = %d\n", (void*)var2, *var2);
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
Thanks for the help
"Juggernaut" <ju************@hotmail.com> wrote in message
news:a3*********************@news000.worldonline.d k...
I have a program that compiles without errors, but when I run it I get a
segment fault.

I have managed to trace where it happens, but since im new to C I don't know how to fix this or how I did it wrong.

In main lets say I have 2 variables var1 and var2 that has values that I
want to pass to a method for calculations with them. But I want to work with them later from main so I thought I'd make them pointers.

So I called them to a method so: method(&var1, &var2)

and the method receives them so: void method(int *var1, *var2)

All this works..but when I try to get the numbers that are supposed to be in var1 and var2 I get some long crazy numbers (like -1218520428) and I am
guessing these are the address to the int I want?

And the segment error happens when I use these in an array
(array[var1][var2]) because it gets extremely out of bounds.

But how do I get the values and not the address?

I tried grabbing them from the pointer and placing it in a new variable like this: int var = (int)var1

But the new variable also has the huge address value

So my question is, what am I doing wrong? How to I access the actual value
thats in main from the other method and not the address?

I need the number to calculate with, and the starting number has to be
created in main.
Hope someone can help.

-Juggernaut

P.S. I can post the actual code, but I thought It wouldn't be necessary
since I know whats wrong, but just not how I do it right.

Nov 14 '05 #4

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

Similar topics

2
1592
by: linq936 | last post by:
Hi, I am using gcc3.2.3 as the compiler for my c++ code on Red Hat Linux Enterprise 3. I find that even an exception is caught, "Segment Fault" is still thrown out and program crashes. Here is...
3
11383
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
65
12509
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
27
3318
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
1
2425
by: nass | last post by:
hello everyone.. i am not sure what is wrong - never liked strings anyway! my problem is that i am trying to save a QCstring in a shared memory segment and i get a segmentation fault.. here is...
1
1340
by: pascal | last post by:
hi friends i am new to C on Linux env. My prob is why do we not get a segment fault when we try to find the value at 0 memory location where as a segment fault is shown if attempt is made to...
5
2662
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
16
2492
by: rahul | last post by:
void foo(void) { /* blah blah */ } char buf = {....}; memcpy( (void *)&foo, buf, sizeof buf); This thing is giving segmentation fault on RHEL 5/gcc 4.1. Is the text segment protected? I am...
10
12464
by: H.S. | last post by:
Hello, I have class in which I am allocating space for a double array in the constructor. I use the double array twice in one of the methods and then delete that array in the class's destructor....
0
7224
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
7120
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
7494
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
5626
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,...
1
5050
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...
0
4706
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...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
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...

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.