Let me state my problem.
This is my main.c -
-
#include <stdio.h>
-
#include "includes/first_order_local_matrix.h"
-
-
int main()
-
{
-
float eps = 1;
-
-
struct vector *x, *y, *rho, *q;
-
struct matrix *P;
-
-
initialize_vector(x,3);
-
initialize_vector(y,3);
-
initialize_vector(rho,3);
-
-
x->values[0] = 0;
-
x->values[1] = 2;
-
x->values[2] = 0;
-
y->values[0] = 0;
-
y->values[1] = 0;
-
y->values[2] = 1;
-
rho->values[0] = 3;
-
rho->values[1] = 3;
-
rho->values[2] = 3;
-
-
first_order_local_matrix(x,y,rho,eps,P,q);
-
-
}
-
-
and I have declared the structure vector as follows: -
-
struct vector
-
{
-
float *values;
-
int length;
-
};
-
-
and the definition for the initialize_vector() function is as follows: -
-
void initialize_vector(struct vector *vector_to_be_initialized, int length)
-
{
-
printf("Initializing Vector\n");
-
-
vector_to_be_initialized -> length= length;
-
vector_to_be_initialized -> values = malloc(length * sizeof(float));
-
-
printf("Initialized Vector\n");
-
}
-
-
and finally this is the code for the first_order_local_matrix() function: -
-
void first_order_local_matrix(struct vector *x, struct vector *y, struct vector *rho, float epsilon, struct matrix *P, struct vector *q)
-
{
-
-
struct vector *b, *c;
-
-
float area;
-
int ii,jj,dim;
-
-
dim = x->length;
-
-
initialize_vector(b, dim);
-
initialize_vector(c, dim);
-
initialize_vector(q, dim);
-
initialize_matrix(P, dim,dim);
-
-
area = triangle(x,y,b,c);
-
-
for(ii=0; ii<dim; ii++)
-
{
-
for(jj=0; jj<dim; jj++)
-
P->values[ii][jj] = epsilon*area*(b->values[ii]*b->values[jj] + c->values[ii]*c->values[jj]);
-
-
q->values[ii] = area*(2*rho->values[ii] + rho->values[mod(ii,3)+1] + rho->values[mod(mod(ii,3)+1,3)+1])/12;
-
}
-
}
-
-
The problem is that the first 3 calls for the initialize_vector() from main() goes well. But when the initalize_vector() is called from first_order_local_matrix() I get a segmentation fault.
Any clues on what I am doing wrong here?
Thanks in advance.
5 2235
Let me state my problem.
This is my main.c -
-
#include <stdio.h>
-
#include "includes/first_order_local_matrix.h"
-
-
int main()
-
{
-
float eps = 1;
-
-
struct vector *x, *y, *rho, *q;
-
struct matrix *P;
-
-
initialize_vector(x,3);
-
initialize_vector(y,3);
-
initialize_vector(rho,3);
-
-
x->values[0] = 0;
-
x->values[1] = 2;
-
x->values[2] = 0;
-
y->values[0] = 0;
-
y->values[1] = 0;
-
y->values[2] = 1;
-
rho->values[0] = 3;
-
rho->values[1] = 3;
-
rho->values[2] = 3;
-
-
first_order_local_matrix(x,y,rho,eps,P,q);
-
-
}
-
-
and I have declared the structure vector as follows: -
-
struct vector
-
{
-
float *values;
-
int length;
-
};
-
-
and the definition for the initialize_vector() function is as follows: -
-
void initialize_vector(struct vector *vector_to_be_initialized, int length)
-
{
-
printf("Initializing Vector\n");
-
-
vector_to_be_initialized -> length= length;
-
vector_to_be_initialized -> values = malloc(length * sizeof(float));
-
-
printf("Initialized Vector\n");
-
}
-
-
and finally this is the code for the first_order_local_matrix() function: -
-
void first_order_local_matrix(struct vector *x, struct vector *y, struct vector *rho, float epsilon, struct matrix *P, struct vector *q)
-
{
-
-
struct vector *b, *c;
-
-
float area;
-
int ii,jj,dim;
-
-
dim = x->length;
-
-
initialize_vector(b, dim);
-
initialize_vector(c, dim);
-
initialize_vector(q, dim);
-
initialize_matrix(P, dim,dim);
-
-
area = triangle(x,y,b,c);
-
-
for(ii=0; ii<dim; ii++)
-
{
-
for(jj=0; jj<dim; jj++)
-
P->values[ii][jj] = epsilon*area*(b->values[ii]*b->values[jj] + c->values[ii]*c->values[jj]);
-
-
q->values[ii] = area*(2*rho->values[ii] + rho->values[mod(ii,3)+1] + rho->values[mod(mod(ii,3)+1,3)+1])/12;
-
}
-
}
-
-
The problem is that the first 3 calls for the initialize_vector() from main() goes well. But when the initalize_vector() is called from first_order_local_matrix() I get a segmentation fault.
Any clues on what I am doing wrong here?
Thanks in advance.
Fault 1:
struct vector *x, *y, *rho, *q;
struct matrix *P;
initialize_vector(x,3);
initialize_vector(y,3);
initialize_vector(rho,3);
your not intializing pointer here.
use x = (struct vector *)malloc(sizeof(struct vector));
before using pointer x. this holds good for other pointers as well
Fault 2:
your are doing same mistake in function first_order_local_matrix() as well.
i don't know why its not crashing at first place itself. :(
if any one know do tell me.
Thanks for the reply budy. I fixed the problem. I was in Java for quite sometime and these subtle things in C are screwing me up grandly [:D]
I have one more question:
You said I should use x = (struct vector *)malloc(sizeof(struct vector));
But should it not be x = (struct vector *)malloc(sizeof(struct vector *)) since x is a pointer.
But both of them seem to work fine. So which one is technically correct? Plus why is it required to coerce it by typecasting as (struct vector *). Isn't it something redundant?
Thanks in advance.
Prathap
But should it not be x = (struct vector *)malloc(sizeof(struct vector *)) since x is a pointer.
you want enough memory for your structure not enough memory for a pointer to your structure
Plus why is it required to coerce it by typecasting as (struct vector *). Isn't it something redundant?
malloc just returns a void* to your new memory (is just a chunk and says nothing abut how it is to be used/divided up). malloc knows nothing about what the memory is for, only how big the chunk is. You use the typecast to define how that memory is used (how its shaped or divided up) - your saying that the memory is memory for your vector structure.
In that case, what is the differnce between -
x = (struct vector *)malloc(sizeof(struct vector *))
-
and -
x = (struct vector *)malloc(sizeof(struct vector))
-
i.e. under what circumstance would you want to allocate space corresponding to a pointer?
under what circumstance would you want to allocate space corresponding to a pointer?
I can't think of a time when I malloc'd space for a pointer. There may be a situation where this is desired, I just can't think of one.
If your still foggy on the subject, try this: -
printf( "sizeof struct vector: %d\n", sizeof( struct vector));
-
printf( "sizeof struct vector *: %d\n", sizeof( struct vector*));
The pointer is only large enough to hold an address. The structure is large enough to hold what it's made up of (an int, and a pointer for floats).
Sign in to post your reply or Sign up for a free account.
Similar topics
by: sivignon |
last post by:
Hi,
I'm writing a php script which deals with 3 ORACLE databases.
This script is launch by a script shell on an linux machine like this :
/../php/bin/php ./MySript.php (PHP 4.3.3)
My script...
|
by: diyanat |
last post by:
i am writing a cgi script in C using the CGIC library, the script
fails to run, i am using apache on linux
error report from apache : internal server error
Premature end of script headers:...
|
by: laberth |
last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why?
Here is what I use :
typedef struct noeud {
int val;
struct noeud *fgauche;
struct noeud *fdroit;
} *arbre; //for those...
|
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...
|
by: Fra-it |
last post by:
Hi everybody,
I'm trying to make the following code running properly, but I can't get
rid of the "SEGMENTATION FAULT" error message when executing.
Reading some messages posted earlier, I...
|
by: Digital Puer |
last post by:
Hi, I'm coming over from Java to C++, so please bear with me.
In C++, is there a way for me to use exceptions to catch
segmentation faults (e.g. when I access a location off
the end of an array)?...
|
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!
...
|
by: pycraze |
last post by:
I would like to ask a question. How do one handle the exception due to
Segmentation fault due to Python ? Our bit operations and arithmetic
manipulations are written in C and to some of our...
|
by: madunix |
last post by:
My Server is suffering bad lag (High Utlization)
I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2
Web visitors retrieve data from the web by php calls through oci
cobnnection...
|
by: DanielJohnson |
last post by:
int main()
{
printf("\n Hello World");
main;
return 0;
}
This program terminate just after one loop while the second program
goes on infinitely untill segmentation fault (core dumped) on...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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: 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...
| |