473,287 Members | 1,643 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,287 software developers and data experts.

Segmentation Fault

Let me state my problem.

This is my main.c

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include "includes/first_order_local_matrix.h"
  4.  
  5. int main()
  6. {
  7.         float eps = 1;
  8.  
  9.         struct vector *x, *y, *rho, *q;
  10.         struct matrix *P;
  11.  
  12.         initialize_vector(x,3);
  13.         initialize_vector(y,3);
  14.         initialize_vector(rho,3);
  15.  
  16.         x->values[0] = 0;
  17.         x->values[1] = 2;
  18.         x->values[2] = 0;
  19.         y->values[0] = 0;
  20.         y->values[1] = 0;
  21.         y->values[2] = 1;
  22.         rho->values[0] = 3;
  23.         rho->values[1] = 3;
  24.         rho->values[2] = 3;
  25.  
  26.         first_order_local_matrix(x,y,rho,eps,P,q);
  27.  
  28. }
  29.  
  30.  
and I have declared the structure vector as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. struct vector
  3. {
  4.         float *values;
  5.         int length;
  6. };
  7.  
  8.  
and the definition for the initialize_vector() function is as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void initialize_vector(struct vector *vector_to_be_initialized, int length)
  3. {
  4.         printf("Initializing Vector\n");
  5.  
  6.         vector_to_be_initialized -> length= length;
  7.         vector_to_be_initialized -> values = malloc(length * sizeof(float));
  8.  
  9.         printf("Initialized Vector\n");
  10.  
  11.  
and finally this is the code for the first_order_local_matrix() function:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void first_order_local_matrix(struct vector *x, struct vector *y, struct vector *rho, float epsilon, struct matrix *P, struct vector *q)
  3. {
  4.  
  5.         struct vector *b, *c;
  6.  
  7.         float area;
  8.         int ii,jj,dim;
  9.  
  10.         dim = x->length;
  11.  
  12.         initialize_vector(b, dim);
  13.         initialize_vector(c, dim);
  14.         initialize_vector(q, dim);
  15.         initialize_matrix(P, dim,dim);
  16.  
  17.         area = triangle(x,y,b,c);
  18.  
  19.         for(ii=0; ii<dim; ii++)
  20.         {
  21.                 for(jj=0; jj<dim; jj++)
  22.                         P->values[ii][jj] = epsilon*area*(b->values[ii]*b->values[jj] + c->values[ii]*c->values[jj]);
  23.  
  24.                 q->values[ii] = area*(2*rho->values[ii] + rho->values[mod(ii,3)+1] + rho->values[mod(mod(ii,3)+1,3)+1])/12;
  25.         }
  26. }
  27.  
  28.  
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.
Apr 27 '07 #1
5 2227
svlsr2000
181 Expert 100+
Let me state my problem.

This is my main.c

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include "includes/first_order_local_matrix.h"
  4.  
  5. int main()
  6. {
  7.         float eps = 1;
  8.  
  9.         struct vector *x, *y, *rho, *q;
  10.         struct matrix *P;
  11.  
  12.         initialize_vector(x,3);
  13.         initialize_vector(y,3);
  14.         initialize_vector(rho,3);
  15.  
  16.         x->values[0] = 0;
  17.         x->values[1] = 2;
  18.         x->values[2] = 0;
  19.         y->values[0] = 0;
  20.         y->values[1] = 0;
  21.         y->values[2] = 1;
  22.         rho->values[0] = 3;
  23.         rho->values[1] = 3;
  24.         rho->values[2] = 3;
  25.  
  26.         first_order_local_matrix(x,y,rho,eps,P,q);
  27.  
  28. }
  29.  
  30.  
and I have declared the structure vector as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. struct vector
  3. {
  4.         float *values;
  5.         int length;
  6. };
  7.  
  8.  
and the definition for the initialize_vector() function is as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void initialize_vector(struct vector *vector_to_be_initialized, int length)
  3. {
  4.         printf("Initializing Vector\n");
  5.  
  6.         vector_to_be_initialized -> length= length;
  7.         vector_to_be_initialized -> values = malloc(length * sizeof(float));
  8.  
  9.         printf("Initialized Vector\n");
  10.  
  11.  
and finally this is the code for the first_order_local_matrix() function:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void first_order_local_matrix(struct vector *x, struct vector *y, struct vector *rho, float epsilon, struct matrix *P, struct vector *q)
  3. {
  4.  
  5.         struct vector *b, *c;
  6.  
  7.         float area;
  8.         int ii,jj,dim;
  9.  
  10.         dim = x->length;
  11.  
  12.         initialize_vector(b, dim);
  13.         initialize_vector(c, dim);
  14.         initialize_vector(q, dim);
  15.         initialize_matrix(P, dim,dim);
  16.  
  17.         area = triangle(x,y,b,c);
  18.  
  19.         for(ii=0; ii<dim; ii++)
  20.         {
  21.                 for(jj=0; jj<dim; jj++)
  22.                         P->values[ii][jj] = epsilon*area*(b->values[ii]*b->values[jj] + c->values[ii]*c->values[jj]);
  23.  
  24.                 q->values[ii] = area*(2*rho->values[ii] + rho->values[mod(ii,3)+1] + rho->values[mod(mod(ii,3)+1,3)+1])/12;
  25.         }
  26. }
  27.  
  28.  
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.
Apr 27 '07 #2
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
Apr 27 '07 #3
mac11
256 100+
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.
Apr 27 '07 #4
In that case, what is the differnce between

Expand|Select|Wrap|Line Numbers
  1. x = (struct vector *)malloc(sizeof(struct vector *))
  2.  
and

Expand|Select|Wrap|Line Numbers
  1. x = (struct vector *)malloc(sizeof(struct vector))
  2.  
i.e. under what circumstance would you want to allocate space corresponding to a pointer?
Apr 27 '07 #5
mac11
256 100+
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:
Expand|Select|Wrap|Line Numbers
  1. printf( "sizeof struct vector: %d\n", sizeof( struct vector));
  2. 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).
May 3 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
3
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:...
16
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...
3
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...
5
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...
18
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)?...
27
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! ...
7
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...
3
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...
6
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.