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

Pointer Usage

Before I am flamed, I did search the FAQ for an answer to the question
that I am about to post. Even with a search of the newsgroup archive,
there are so many subjects about pointers I could search for a long
time and not find an answer to my question.

I am currently integrating the Moller-Trumbore ray/triangle
intersection algorithm
(http://www.ce.chalmers.se/old/staff/...aytri/raytri.c) into an
existing raytracer. I have basically plagiarized the original code
with some minor modifications to variable names in order integrate it
into the existing raytracer. Below is the source code to the portion
of the source code that I am modifying. The program compiled under
Cygwin using gcc 3.3.3-2. When I run the program, I receive a
"Segmentation Segmentation fault (core dumped)" error. After
recompiling the program using the -ggdb compile option, I executed the
program within gdb in order to find out where the problem is
occurring. At this point, it was determined that the Segmentation
fault occurs at the line that reads:

*u = DOT(tvec, pvec);

Does anyone know if this statement is legal? Moller uses this exact
same code and I am assuming that he has successfully run it. The
reason why I question whether or not this satement is legal is because
I am dereferencing the pointer and setting it value to DOT(tvec,
pvec), since this value is calculated on the fly it has no memory
assigned to it and therefore no address. Am I off base? Is the
original Moller-Trumbore code valid?

Marcus


triangleintersect(o, r) /* compute intersection with polygonal face
*/
OBJREC *o;
register RAY *r;
{
double rdot; /* direction . normal */
double t; /* distance to intersection */
FVECT pisect; /* intersection point */
register FACE *f; /* face record */
register int i;
double *u, *v, *t;
double det, d, inv_det;
double tvec[3], pvec[3], qvec[3], edge1[3], edge2[3];
VSUB(edge1, VERTEX(f,1), VERTEX(f,0));
VSUB(edge2, VERTEX(f,2), VERTEX(f,0));

/* begin calculating determinant - also used to calculate U
parameter */
VCROSS(pvec, r->rdir, edge2);
/* if determinant is near zero, ray lies in plane of triangle */
det = DOT(edge1, pvec);
if (det < FTINY)
return 0;

/* calculate distance from vert0 to ray origin */
VSUB(tvec, r->rorg, VERTEX(f,0));
/* calculate U parameter and test bounds */
*u = DOT(tvec, pvec);
if (*u < 0.0 || *u > det)
return 0;
/* prepare to test V parameter */
VCROSS(qvec, tvec, edge1);
/* calculate V parameter and test bounds */
*v = DOT(r->rdir, qvec) ;
if (*v < 0.0 || *u + *v > det)
return 0;
*t = DOT(edge2,qvec);
inv_det = 1.0/det;
*t *= inv_det;
rdot = -DOT(r->rdir, f->norm);
if (rdot <= FTINY && rdot >= -FTINY) /* ray parallels plane */
t = FHUGE;
//else
// t = (DOT(r->rorg, f->norm) - f->offset) / rdot;
if (t <= FTINY || t >= r->rot) /* not good enough */
return(0);
/* compute intersection */
for (i = 0; i < 3; i++)
pisect[i] = r->rorg[i] + r->rdir[i]* t;
r->rot = t;
VCOPY(r->rop, pisect);
r->rod = rdot;
r->ro = o;
VCOPY(r->ron, f->norm);
r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
r->uv[0] = r->uv[1] = 0.0;
r->rox = NULL;
return(1); /* hit */
Nov 14 '05 #1
7 1513
On 29 Sep 2004 20:05:03 -0700, ma*******@hotmail.com (Marcus Jacobs)
wrote in comp.lang.c:
Before I am flamed, I did search the FAQ for an answer to the question
that I am about to post. Even with a search of the newsgroup archive,
there are so many subjects about pointers I could search for a long
time and not find an answer to my question.

I am currently integrating the Moller-Trumbore ray/triangle
intersection algorithm
(http://www.ce.chalmers.se/old/staff/...aytri/raytri.c) into an
existing raytracer. I have basically plagiarized the original code
with some minor modifications to variable names in order integrate it
into the existing raytracer. Below is the source code to the portion
of the source code that I am modifying. The program compiled under
Cygwin using gcc 3.3.3-2. When I run the program, I receive a
"Segmentation Segmentation fault (core dumped)" error. After
recompiling the program using the -ggdb compile option, I executed the
program within gdb in order to find out where the problem is
occurring. At this point, it was determined that the Segmentation
fault occurs at the line that reads:

*u = DOT(tvec, pvec);

Does anyone know if this statement is legal? Moller uses this exact
same code and I am assuming that he has successfully run it. The
reason why I question whether or not this satement is legal is because
I am dereferencing the pointer and setting it value to DOT(tvec,
pvec), since this value is calculated on the fly it has no memory
assigned to it and therefore no address. Am I off base? Is the
original Moller-Trumbore code valid?

Marcus


triangleintersect(o, r) /* compute intersection with polygonal face
*/
Prototype style function definitions were added to C 15 years ago.
Why are you still using obsolete code like this?

Also, this code could not have possible compiled. Always copy the
real code from your text editor and paste it as text into the body of
your post.
OBJREC *o;
register RAY *r;
{
double rdot; /* direction . normal */
double t; /* distance to intersection */
Here you define 't' as a double.
FVECT pisect; /* intersection point */
register FACE *f; /* face record */
register int i;
double *u, *v, *t;
Here you define another object with the name 't' in the same scope as
the one above. This can't possibly compile.

'u' is defined as a pointer to double in the line above. Since it is
an automatic variable and not initialized, its value is indeterminate.
It can point to a double, it does not do so now.
double det, d, inv_det;
double tvec[3], pvec[3], qvec[3], edge1[3], edge2[3];
VSUB(edge1, VERTEX(f,1), VERTEX(f,0));
VSUB(edge2, VERTEX(f,2), VERTEX(f,0));

/* begin calculating determinant - also used to calculate U
parameter */
VCROSS(pvec, r->rdir, edge2);
/* if determinant is near zero, ray lies in plane of triangle */
det = DOT(edge1, pvec);
if (det < FTINY)
return 0;

/* calculate distance from vert0 to ray origin */
VSUB(tvec, r->rorg, VERTEX(f,0));
/* calculate U parameter and test bounds */
*u = DOT(tvec, pvec);
Here is the line that you say is causing your problem. You have an
indeterminate pointer to double. It does not point to a double, it is
an uninitialized pointer. Attempting to dereference the pointer to
read or write memory is undefined behavior. There is every reason for
your program to crash. '*u' is not a double and most likely not
memory that you have a right to access.
if (*u < 0.0 || *u > det)
return 0;
/* prepare to test V parameter */
VCROSS(qvec, tvec, edge1);
/* calculate V parameter and test bounds */
*v = DOT(r->rdir, qvec) ;
You'll have the same problem here it you get this far.
if (*v < 0.0 || *u + *v > det)
return 0;
*t = DOT(edge2,qvec);
....and here.
inv_det = 1.0/det;
*t *= inv_det;
rdot = -DOT(r->rdir, f->norm);
if (rdot <= FTINY && rdot >= -FTINY) /* ray parallels plane */
t = FHUGE;
//else
// t = (DOT(r->rorg, f->norm) - f->offset) / rdot;
if (t <= FTINY || t >= r->rot) /* not good enough */
Suddenly, in the two lines above (even though one is commented out),
you are suddenly using 't' like it was a double, not a pointer.
return(0);
/* compute intersection */
for (i = 0; i < 3; i++)
pisect[i] = r->rorg[i] + r->rdir[i]* t;
r->rot = t;
VCOPY(r->rop, pisect);
r->rod = rdot;
r->ro = o;
VCOPY(r->ron, f->norm);
r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
r->uv[0] = r->uv[1] = 0.0;
r->rox = NULL;
return(1); /* hit */


Several places you assign the value of the function/macro DOT to
actual doubles, like 'rdot' and 'det'. Other places you try to assign
the result of whatever twilight zone specified by an indeterminate
pointer, and that's a definite no-no.

Why are 'u', 'v', and 't' defined as pointers to double, and not just
doubles?

This code is frankly unreadable and unmaintainable. Single character
variable names some upper case, pre-ANSI function definitions, and
poor structure make it a nightmare.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:k1********************************@4ax.com...
On 29 Sep 2004 20:05:03 -0700, ma*******@hotmail.com (Marcus Jacobs)
wrote in comp.lang.c:
Before I am flamed, I did search the FAQ for an answer to the question
that I am about to post. Even with a search of the newsgroup archive,
there are so many subjects about pointers I could search for a long
time and not find an answer to my question.

I am currently integrating the Moller-Trumbore ray/triangle
intersection algorithm
(http://www.ce.chalmers.se/old/staff/...aytri/raytri.c) into an
existing raytracer. I have basically plagiarized the original code
with some minor modifications to variable names in order integrate it
into the existing raytracer. Below is the source code to the portion
of the source code that I am modifying. The program compiled under
Cygwin using gcc 3.3.3-2. When I run the program, I receive a
"Segmentation Segmentation fault (core dumped)" error. After
recompiling the program using the -ggdb compile option, I executed the
program within gdb in order to find out where the problem is
occurring. At this point, it was determined that the Segmentation
fault occurs at the line that reads:

*u = DOT(tvec, pvec);

Does anyone know if this statement is legal? Moller uses this exact
same code and I am assuming that he has successfully run it. The
reason why I question whether or not this satement is legal is because
I am dereferencing the pointer and setting it value to DOT(tvec,
pvec), since this value is calculated on the fly it has no memory
assigned to it and therefore no address. Am I off base? Is the
original Moller-Trumbore code valid?

Marcus


triangleintersect(o, r) /* compute intersection with polygonal face
*/
Prototype style function definitions were added to C 15 years ago.
Why are you still using obsolete code like this?

Also, this code could not have possible compiled. Always copy the
real code from your text editor and paste it as text into the body of
your post.
OBJREC *o;
register RAY *r;
{
double rdot; /* direction . normal */
double t; /* distance to intersection */


Here you define 't' as a double.
FVECT pisect; /* intersection point */
register FACE *f; /* face record */
register int i;
double *u, *v, *t;


Here you define another object with the name 't' in the same scope as
the one above. This can't possibly compile.

'u' is defined as a pointer to double in the line above. Since it is
an automatic variable and not initialized, its value is indeterminate.
It can point to a double, it does not do so now.
double det, d, inv_det;
double tvec[3], pvec[3], qvec[3], edge1[3], edge2[3];
VSUB(edge1, VERTEX(f,1), VERTEX(f,0));
VSUB(edge2, VERTEX(f,2), VERTEX(f,0));

/* begin calculating determinant - also used to calculate U
parameter */
VCROSS(pvec, r->rdir, edge2);
/* if determinant is near zero, ray lies in plane of triangle */
det = DOT(edge1, pvec);
if (det < FTINY)
return 0;

/* calculate distance from vert0 to ray origin */
VSUB(tvec, r->rorg, VERTEX(f,0));
/* calculate U parameter and test bounds */
*u = DOT(tvec, pvec);


Here is the line that you say is causing your problem. You have an
indeterminate pointer to double. It does not point to a double, it is
an uninitialized pointer. Attempting to dereference the pointer to
read or write memory is undefined behavior. There is every reason for
your program to crash. '*u' is not a double and most likely not
memory that you have a right to access.
if (*u < 0.0 || *u > det)
return 0;
/* prepare to test V parameter */
VCROSS(qvec, tvec, edge1);
/* calculate V parameter and test bounds */
*v = DOT(r->rdir, qvec) ;


You'll have the same problem here it you get this far.
if (*v < 0.0 || *u + *v > det)
return 0;
*t = DOT(edge2,qvec);


...and here.
inv_det = 1.0/det;
*t *= inv_det;
rdot = -DOT(r->rdir, f->norm);
if (rdot <= FTINY && rdot >= -FTINY) /* ray parallels plane */
t = FHUGE;
//else
// t = (DOT(r->rorg, f->norm) - f->offset) / rdot;
if (t <= FTINY || t >= r->rot) /* not good enough */


Suddenly, in the two lines above (even though one is commented out),
you are suddenly using 't' like it was a double, not a pointer.
return(0);
/* compute intersection */
for (i = 0; i < 3; i++)
pisect[i] = r->rorg[i] + r->rdir[i]* t;
r->rot = t;
VCOPY(r->rop, pisect);
r->rod = rdot;
r->ro = o;
VCOPY(r->ron, f->norm);
r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
r->uv[0] = r->uv[1] = 0.0;
r->rox = NULL;
return(1); /* hit */


Several places you assign the value of the function/macro DOT to
actual doubles, like 'rdot' and 'det'. Other places you try to assign
the result of whatever twilight zone specified by an indeterminate
pointer, and that's a definite no-no.

Why are 'u', 'v', and 't' defined as pointers to double, and not just
doubles?


I noticed in the original code that u,v, and t were parameters to the
function, presumably to pass back those values to the caller. That not being
the case, these variables should be defined as straight doubles. Note that
the original code at
http://www.ce.chalmers.se/old/staff/...aytri/raytri.c compiles just
fine. One more point about this code: The pointer 'FACE* f' is never
initialized and will cause more problems.

This code is frankly unreadable and unmaintainable. Single character
variable names some upper case, pre-ANSI function definitions, and
poor structure make it a nightmare.
Well, that's legacy code for you. Just have a look at "Numerical Recipes in
C' (I got edition 2). Who would write maintainable code like that? Yes, I
know several people are questioning some of the advice and algorithms in
that book.

Dag
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Nov 14 '05 #3
Dag Viken wrote:
"Jack Klein" <ja*******@spamcop.net> wrote in message
ma*******@hotmail.com (Marcus Jacobs) wrote in comp.lang.c:
.... snip ...
This code is frankly unreadable and unmaintainable. Single
character variable names some upper case, pre-ANSI function
definitions, and poor structure make it a nightmare.


Well, that's legacy code for you. Just have a look at "Numerical
Recipes in C' (I got edition 2). Who would write maintainable
code like that? Yes, I know several people are questioning some
of the advice and algorithms in that book.


Please snip whatever is not germane to your answer. You included
something like 160 totally unnecessary lines, which increases the
transmission and storage loads everywhere.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #4
"CBFalconer" <cb********@yahoo.com> wrote in message
news:41***************@yahoo.com...
Dag Viken wrote:
"Jack Klein" <ja*******@spamcop.net> wrote in message
ma*******@hotmail.com (Marcus Jacobs) wrote in comp.lang.c:
... snip ...
This code is frankly unreadable and unmaintainable. Single
character variable names some upper case, pre-ANSI function
definitions, and poor structure make it a nightmare.


Well, that's legacy code for you. Just have a look at "Numerical
Recipes in C' (I got edition 2). Who would write maintainable
code like that? Yes, I know several people are questioning some
of the advice and algorithms in that book.


Please snip whatever is not germane to your answer. You included
something like 160 totally unnecessary lines, which increases the
transmission and storage loads everywhere.


Sorry about my bloated email,

Do you have a comment on the issue?

Dag
Nov 14 '05 #5
In <41**************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
idea of scope. If I have not made it available on my download
page, I can do so (in x86 executable form only).

^^^^^^^^^^^^^^^^^^^^^^^^^^^
Does this mean that it can be fed to any x86-based system and it will
happily execute it?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #6
Dan Pop wrote:
CBFalconer <cb********@yahoo.com> writes:
idea of scope. If I have not made it available on my download
page, I can do so (in x86 executable form only).

^^^^^^^^^^^^^^^^^^^^^^^^^^^
Does this mean that it can be fed to any x86-based system and it
will happily execute it?


Assuming it supplies or simulates a suitable subset of the system
calls of MsDos, yes. Does this mean you want it, as I find it is
not presently mounted?

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #7
In <41***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
Dan Pop wrote:
CBFalconer <cb********@yahoo.com> writes:
>idea of scope. If I have not made it available on my download
>page, I can do so (in x86 executable form only). ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Does this mean that it can be fed to any x86-based system and it
will happily execute it?


Assuming it supplies or simulates a suitable subset of the system
calls of MsDos, yes.


There was no mention of MSDOS in your previous post, was it?
Does this mean you want it, as I find it is not presently mounted?


Nope, merely pointing out that the string "x86 executable form" doesn't
make much sense.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #8

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

Similar topics

7
by: Dave | last post by:
Hello all, In the code below, I use a pointer to an object under construction. Is the usage below legal? I have come across similar code at work. It compiles, but I'm not sure it's really...
8
by: Frank Münnich | last post by:
Hi there.. My name is Frank Münnich. I've got a question about pointers that refer to an array of a structure. How do I declare that type? If I have declared a structure struct mystruc {...
9
by: rskeples | last post by:
I write a small program. char *foo(void); char *a = "I like C"; int main(void) { if((strcmp(a,foo())) { printf("\n i like c"); } }
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
9
by: John Veldthuis | last post by:
Hi, I want to be able to read a https web page, enter a user name and password in the fields returned and then read in the information that is returned after this and then close the connection. ...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
13
by: Prisoner at War | last post by:
I know about the * * style="cursor: pointer; cursor: hand;" * * attribute of the <imgtag, but is there a way JavaScript can "load in" one's own graphic for such events?? How? TIA!
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.