LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-30-2009, 04:37 AM   #16
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18

@lemon09
thank you so much its working now.
thanks []

Last edited by ibabhelix; 09-30-2009 at 04:42 AM.
 
Old 09-30-2009, 04:43 AM   #17
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 285
Blog Entries: 1

Rep: Reputation: 37
Thumbs up

all the best............

have a happy journey...
 
Old 10-08-2009, 03:05 AM   #18
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
hi guys.
in my program when i compiled i got this warning:


warning: function returns address of local variable

the program is running succesfully but i want to know how to correct this warning.

please help me
 
Old 10-08-2009, 03:25 AM   #19
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by ibabhelix View Post
warning: function returns address of local variable
Which function? Can you post the problem code? Does the function in question return a pointer? If so, you shouldn't return a pointer to a variable that's declared on the stack in that function. The reason is that when the function returns, these variables are removed from the stack and so that memory should no longer be used by your program.
 
Old 10-08-2009, 03:37 AM   #20
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by ibabhelix View Post
hi guys.
in my program when i compiled i got this warning:


warning: function returns address of local variable

the program is running succesfully but i want to know how to correct this warning.

please help me
You don't want to return the address of a local variable, because that variable only exists within the function. This is a recipe for trouble.

Code:
please help me
Sorry, my crystal ball is cloudy right now ... try again later ... when you post your code.
 
Old 10-08-2009, 12:50 PM   #21
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by ibabhelix View Post
hi guys.
in my program when i compiled i got this warning:


warning: function returns address of local variable

the program is running succesfully but i want to know how to correct this warning.

please help me
its probably not good
thats one of those warnings you actually have to worry about
try decalring the variables that its returning as static
 
Old 10-08-2009, 01:15 PM   #22
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Or better yet, pass a pointer to the variable to the function and declare it out of the function. Or, if it's a simple enough type, just make the function return that data type, and call it as value=func(...);

But, as said, no code = no concrete answer.
 
Old 10-09-2009, 12:41 AM   #23
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
here is the code

Quote:
char* TorsionCheck()
{
int i,j;
float r1[3][1],r2[3][1],r3[3][1],n1[3][1],n2[3][1];
float mn1,mn2;
float a1,a2,a3;
float b1,b2,b3;
float k,l;
float n1n2,teta;
float n1t[1][3];
float s[3][1],sign;
char returnValue[80];

//gettin the vectors

for(i=0;i<3;i++)
{
for(j=0;j<1;j++)
{
r1[i][j]=a[i][j]-b[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<1;j++)
{
r2[i][j]=c[i][j]-b[i][j];
}
}

//gettin the normal
//printf("the normal n1=");

n1[0][0]=r1[1][0]*r2[2][0]-r1[2][0]*r2[1][0];
n1[0][1]=r1[2][0]*r2[0][0]-r1[0][0]*r2[2][0];
n1[0][2]=r1[0][0]*r2[1][0]-r1[1][0]*r2[0][0];

a1=n1[0][0]*n1[0][0];
a2=n1[0][1]*n1[0][1];
a3=n1[0][2]*n1[0][2];
mn1=sqrt(a1+a2+a3);

//printf("magnitude of n1=%f",mn1);
//printf("the vector r3=\n");
for(i=0;i<3;i++)
{
for(j=0;j<1;j++)
{
r3[i][j]=b[i][j]-d[i][j];
}
}

//gettin the normal
//printf("the normal n2=");

n2[0][0]=r2[1][0]*r3[2][0]-r2[2][0]*r3[1][0];
n2[0][1]=r2[2][0]*r3[0][0]-r2[0][0]*r3[2][0];
n2[0][2]=r2[0][0]*r3[1][0]-r2[1][0]*r3[0][0];

b1=(n2[0][0])*(n2[0][0]);
b2=(n2[0][1])*(n2[0][1]);
b3=(n2[0][2])*(n2[0][2]);
mn2=sqrt(b1+b2+b3);
//printf("magnitude of n2=%f",mn2);

//dot product for obtaining the angle //

n1n2=n1[0][0]*n2[0][0]+n1[1][0]*n2[1][0]+n1[2][0]*n2[2][0];
//printf("\n%f",n1n2);
teta=acos(n1n2/(mn1*mn2))*57.29578;
// printf("\nteta=%f",teta);

//scalar triplet product

s[0][0]=n1[1][0]*n2[2][0]-n1[2][0]*n2[1][0];
s[0][1]=n1[2][0]*n2[0][0]-n1[0][0]*n2[2][0];
s[0][2]=n1[0][0]*n2[1][0]-n1[1][0]*n2[0][0];

sign=s[0][0]*r2[0][0]+s[1][0]*r2[1][0]+s[2][0]*r2[2][0];

//printf("\n%f",sign);
sprintf(returnValue, "Theta Value = %s %f. \n", ((sign>0) ? " " : "-"), teta);
// sprintf(returnValue, "Theta Value = %f", teta);

/* if(sign>0)
printf("\nThe torsional angle is positive");
else
printf("\nThe torsional angle is positive");*/
return returnValue;
}

Last edited by ibabhelix; 10-09-2009 at 12:43 AM.
 
Old 10-09-2009, 12:42 AM   #24
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
here is the code

Quote:
char* TorsionCheck()
{
int i,j;
float r1[3][1],r2[3][1],r3[3][1],n1[3][1],n2[3][1];
float mn1,mn2;
float a1,a2,a3;
float b1,b2,b3;
float k,l;
float n1n2,teta;
float n1t[1][3];
float s[3][1],sign;
char returnValue[80];

//gettin the vectors

for(i=0;i<3;i++)
{
for(j=0;j<1;j++)
{
r1[i][j]=a[i][j]-b[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<1;j++)
{
r2[i][j]=c[i][j]-b[i][j];
}
}

//gettin the normal
//printf("the normal n1=");

n1[0][0]=r1[1][0]*r2[2][0]-r1[2][0]*r2[1][0];
n1[0][1]=r1[2][0]*r2[0][0]-r1[0][0]*r2[2][0];
n1[0][2]=r1[0][0]*r2[1][0]-r1[1][0]*r2[0][0];

a1=n1[0][0]*n1[0][0];
a2=n1[0][1]*n1[0][1];
a3=n1[0][2]*n1[0][2];
mn1=sqrt(a1+a2+a3);

//printf("magnitude of n1=%f",mn1);
//printf("the vector r3=\n");
for(i=0;i<3;i++)
{
for(j=0;j<1;j++)
{
r3[i][j]=b[i][j]-d[i][j];
}
}

//gettin the normal
//printf("the normal n2=");

n2[0][0]=r2[1][0]*r3[2][0]-r2[2][0]*r3[1][0];
n2[0][1]=r2[2][0]*r3[0][0]-r2[0][0]*r3[2][0];
n2[0][2]=r2[0][0]*r3[1][0]-r2[1][0]*r3[0][0];

b1=(n2[0][0])*(n2[0][0]);
b2=(n2[0][1])*(n2[0][1]);
b3=(n2[0][2])*(n2[0][2]);
mn2=sqrt(b1+b2+b3);
//printf("magnitude of n2=%f",mn2);

//dot product for obtaining the angle //

n1n2=n1[0][0]*n2[0][0]+n1[1][0]*n2[1][0]+n1[2][0]*n2[2][0];
//printf("\n%f",n1n2);
teta=acos(n1n2/(mn1*mn2))*57.29578;
// printf("\nteta=%f",teta);

//scalar triplet product

s[0][0]=n1[1][0]*n2[2][0]-n1[2][0]*n2[1][0];
s[0][1]=n1[2][0]*n2[0][0]-n1[0][0]*n2[2][0];
s[0][2]=n1[0][0]*n2[1][0]-n1[1][0]*n2[0][0];

sign=s[0][0]*r2[0][0]+s[1][0]*r2[1][0]+s[2][0]*r2[2][0];

//printf("\n%f",sign);
sprintf(returnValue, "Theta Value = %s %f. \n", ((sign>0) ? " " : "-"), teta);
// sprintf(returnValue, "Theta Value = %f", teta);

/* if(sign>0)
printf("\nThe torsional angle is positive");
else
printf("\nThe torsional angle is positive");*/
return returnValue;
}
 
Old 10-09-2009, 12:54 AM   #25
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Please use [code] tags in future, rather than [quote] tags. This will allow you to preserve indentation when posting code.

Your problem seems to be as I described earlier. i92guboj posted a solution, so please try it.

Edit:

float r1[3][1]

If you're declaring arrays of length 3, why do you have another "dimension" of length 1? Surely that second index is superfluous?

Last edited by Nylex; 10-09-2009 at 01:04 AM.
 
Old 10-09-2009, 07:51 AM   #26
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 10-09-2009, 08:15 AM   #27
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by ibabhelix View Post
here is the code

char* TorsionCheck()
{
. . .
char returnValue[80];
. . .
return returnValue;
}
First, do you understand why the above is wrong?

Arrays in C are passed and returned by address, not by value. So when you return returnValue, you are returning just a pointer to the 80 char array, not a copy of it.

That 80 char array goes out of scope when the function exits, so its memory may be reused by any later function that is called.

Because TorsionCheck has so much local storage, your program apparently works despite this serious bug. The compiler happened to allocate returnValue far enough down the stack and you then called only functions that use less stack, so the value did not get clobbered before you used it. Any of that could be affected by minor changes almost anywhere in your program. So this bug could suddenly make your program fail after some change that may not appear to relate in any way to the actual bug.

Once you understand why the code is wrong, then you should fix it. Depending on the rest of your program, there are several different approaches to fixing this bug. Some of them are:

1) Use malloc: Instead of
char returnValue[80];
use
char* returnValue = (char*)malloc(80);

If you do that, then freeing the allocated memory becomes the responsibility of the calling code.

If TorsionCheck is called only once per run of your program and/or the resulting char array is needed until nearly the end of the program, it is common and OK to just forget about freeing the memory, because when the program exits it makes no difference whether such allocations were internally freed.

But in other situations, it is important for the calling code to know that it "owns" the returned memory and must use the free() function to release it when it is no longer needed.

2) Declare the char[80] in the calling code and pass the address into TorsionCheck, rather than out of it.

3) Make the return value static: If TorsionCheck is called only once, or if the caller of TorsionCheck is always done using the result before it calls TorsionCheck again, you can change
char returnValue[80];
to
static char returnValue[80];

Last edited by johnsfine; 10-09-2009 at 08:19 AM.
 
Old 10-09-2009, 10:08 AM   #28
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ibabhelix View Post
is there a solution for the above problem ?
please help me.
Yes, there is - learn "C" - your problem has nothing to do with Linux or any other OS in particular.

...

Regarding your code

Code:
float r1[3][1],r2[3][1],r3[3][1],n1[3][1],n2[3][1];
- what is the exact reason for the second index to be 1 ?
 
Old 10-12-2009, 01:15 AM   #29
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
thank u so much
m still a learner i'm learning slowly.
thank u johnsfine and Sergei Steshenko for helping me.
 
Old 10-12-2009, 01:32 AM   #30
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by johnsfine View Post
First, do you understand why the above is
If TorsionCheck is called only once per run of your program and/or the resulting char array is needed until nearly the end of the program, it is common and OK to just forget about freeing the memory, because when the program exits it makes no difference whether such allocations were internally freed.
WTF? that is called a MEMORY LEAK and is very bad programming practice and telling a newbie that its is ok is just an outright stupid thing to do.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cross Platform Programming Ace Blackwell Programming 17 01-26-2009 09:51 AM
Cross-platform programming drbongo Programming 3 07-28-2007 10:09 PM
Cross-Platform Programming JMJ_coder Programming 7 03-11-2007 12:59 PM
Linux Cross-Platform "C" Programming tux_addicted Programming 10 04-28-2006 03:50 AM
Multi-Platform Development/Programming al042077 Linux - Software 1 04-05-2004 05:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration