473,467 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

segmentation fault, it's big problem to me

1 New Member
hello, I'm trying to write a source in c, but I've a big problem. when i write my code
like this:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include "lfixo.h"
  3.  
  4. extern long int a,m;
  5. extern float nad[7];
  6.  
  7. float Tgl0 = 580;
  8. float Tr0 = 590;
  9.  
  10. void ci(float **Tgl,float **Ts,float **Tr)
  11. {
  12. Tgl = Alocar(a,m);Ts = Alocar(a,m);Tr = Alocar(a,m);
  13. long int i,j,k;
  14.  
  15. for(j=0;j<m;j++)
  16. {
  17. // inicialização das temperaturas
  18.  
  19. Tgl[0][j]=Tgl0;
  20.  
  21. //condições iniciais de temperatura na fase fluida
  22. Ts[0][j]=Tgl0; //condições iniciais de temperatura na fase sólida
  23. Tr[0][j]=Tr0; //condições iniciais de temperatura na fluido refrigerante
  24.  
  25.  
  26. Liberar(a,m,Tgl);Liberar(a,m,Ts);Liberar(a,m,Tr);}
  27.  
  28.  
  29.  
where is Alocar and Liberar are function to alocate and release memory for may 2d matrix.
and the main function are:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include "lfixo.h"
  3. long int a=2400,m=6;
  4. float nad[7],dt;
  5.  
  6. void main(void)
  7.  
  8. {
  9. float **Tgl, **Ts, **Tr;
  10.  
  11. ci(Tgl,Ts,Tr);
  12. }
  13.  
thus, my program works. but if put any change(considering the same main function) as the follow(The line 30 is the difference between 2 codes ):
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include "lfixo.h"
  4.  
  5. extern long int a,m;
  6. extern float nad[7];
  7.  
  8. float Tgl0 = 580;
  9. float Tr0 = 590;
  10.  
  11. void ci(float **Tgl,float **Ts,float **Tr)
  12. {
  13. Tgl = Alocar(a,m);Ts = Alocar(a,m);Tr = Alocar(a,m);
  14. long int i,j,k;
  15.  
  16.  
  17. for(j=0;j<m;j++)
  18. {
  19. // inicialização das temperaturas
  20.  
  21. Tgl[0][j]=Tgl0;
  22.  
  23. //condições iniciais de temperatura na fase fluida
  24. Ts[0][j]=Tgl0; //condições iniciais de temperatura na fase sólida
  25. Tr[0][j]=Tr0; //condições iniciais de temperatura na fluido refrigerante
  26. }
  27. for(k=0;k<a;k++){
  28. for (j=1;j<=m-1;j++) {
  29. //   nós internos
  30. // the line below is cause of my problem       Tgl[k+1][j]=Tgl[k][j]*(1-2*nad[1]-nad[2]-nad[3])+Tgl[k][j-1]*(nad[0]+nad[1])+Tgl[k][j+1]*(-nad[0]+nad[1])+nad[2]*Ts[k][j]+nad[3]*Tr[k][j];
  31.  
  32.   }
  33. }
  34.  
  35. Liberar(a,m,Tgl);Liberar(a,m,Ts);Liberar(a,m,Tr);}
  36.  
my code doesn't work well, for example, if set a=240(in main function), the loop goes up to a=226. then if set a=226 the loop run until a=200(or something like this). and now what I have to do?what or where is the problem?
Nov 8 '07 #1
1 1150
weaknessforcats
9,208 Recognized Expert Moderator Expert
void ci(float **Tgl,float **Ts,float **Tr) <<<<<<<<<<<<<<<<<<<<<
{
Tgl = Alocar(a,m);Ts = Alocar(a,m);Tr = Alocar(a,m);
long int i,j,k;

void main(void)

{
float **Tgl, **Ts, **Tr;

ci(Tgl,Ts,Tr); <<<<<<<<<<<<<<<<<<<<<<<
}
I'm not sure what you are doing here. ci() takes float** argiments. Your main() passes float** variables. The problem is that the Tgl, Ts and Tr in the function are copies of the Tgl, Ts and Tr variables used on the call. At the end of ci(), these copies will be destroyed. The net effect is nothing comes out of your function and the variables in main() never do get initialized. If main() continues, you will crash shortly.

Normally in a case like this, ou declare pointers in main() and pass the addresses to the function. The function now had the address of the pointer so it can dereference this address to update the address in the pointer used on the call.
Expand|Select|Wrap|Line Numbers
  1. void MyFunction(float** arg)
  2. {
  3.     *arg = malloc(sizeof(float));
  4. }
  5.  
  6. int main()
  7. {
  8.     float* result;
  9.     MyFunction(&result);
  10. }
  11.  
Maybe you can clear this up before I look at your other main().
Nov 8 '07 #2

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

Similar topics

3
by: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will...
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! ...
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...
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
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,...
1
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,...
0
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.