473,549 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

value[0] lost in an arrary.

I just wrote a function that draws a fractal by recursively calling
itself, and ran into an odd (odd from my point of view anyway) problem.
I've managed to solve (or rather, circumnavigate) the problem, but I still
don't understand why it happened and would like to.
For starters, here's the function:

void draw_series(SDL _Surface *img, float x1, float y1, float x2, float y2,
MAT_matrix<floa t> *pointlist, int depth, int R, int G, int B)
{
int n;
float px1, py1, px2, py2;
for(n = 0; n < pointlist->xsize - 1; n++){
px1 = x1 + (x2 - x1) * pointlist->value[n];
py1 = y1 + (y2 - y1) * pointlist->value[n];
px1 += (y2 - y1) * pointlist->value[n + pointlist->xsize];
py1 -= (x2 - x1) * pointlist->value[n + pointlist->xsize];

px2 = x1 + (x2 - x1) * pointlist->value[n + 1];
py2 = y1 + (y2 - y1) * pointlist->value[n + 1];
px2 += (y2 - y1) * pointlist->value[n + pointlist->xsize + 1];
py2 -= (x2 - x1) * pointlist->value[n + pointlist->xsize + 1];

if(depth){
draw_series(img , px1, py1, px2, py2, pointlist, depth - 1, R, G, B);
}else{
gfx_line(img, (int)px1, (int)py1, (int)px2, (int)py2, R, G, B);
}
}

}

The version shown here works fine. My problem occurred with an earlier
rendition of it, which used exactly the same code as above with one
exception. Instead of passing a pointer to the "pointlist" object, it
passed the object itself. To see what the code looked like, just replace
every occurance of "->" with ".", and remove the * in the first line.
When I did that, the value of pointlist.value[0] was set to 0. All of the
other values within it were fine. So for instance, if it contained a
series of values like (3, 1, 4, 1, 5, 9), those values would become (0, 1,
4, 1, 5, 9)
pointlist is an object of a matrix class I made. The "value" variable is a
pointer to some memory allocated using "new". It uses a template (in this
case it's a float). I don't know if there's any other information that
would pertain to the problem. Again, it was only the [0] value of the
array that would be set to 0.0. All of the other values in that array
(and all other values within that object) remained unscathed.

Does anyone know what would cause this?
Jul 22 '05 #1
1 2342
Jacob wrote:
I just wrote a function that draws a fractal by recursively calling
itself, and ran into an odd (odd from my point of view anyway) problem.
I've managed to solve (or rather, circumnavigate) the problem, but I still
don't understand why it happened and would like to.
For starters, here's the function:

void draw_series(SDL _Surface *img, float x1, float y1, float x2, float y2,
MAT_matrix<floa t> *pointlist, int depth, int R, int G, int B)
{
int n;
float px1, py1, px2, py2;
for(n = 0; n < pointlist->xsize - 1; n++){
Why "- 1"?
px1 = x1 + (x2 - x1) * pointlist->value[n];
py1 = y1 + (y2 - y1) * pointlist->value[n];
px1 += (y2 - y1) * pointlist->value[n + pointlist->xsize];
When n == pointlist->xsize - 2, i.e. at the last iteration, you're adding
a whole 'pointlist->xsize' to it. What element does it represent? Does
your "matrix" have the element [pointlist->xsize*2 - 2]?
py1 -= (x2 - x1) * pointlist->value[n + pointlist->xsize];

px2 = x1 + (x2 - x1) * pointlist->value[n + 1];
py2 = y1 + (y2 - y1) * pointlist->value[n + 1];
px2 += (y2 - y1) * pointlist->value[n + pointlist->xsize + 1];
py2 -= (x2 - x1) * pointlist->value[n + pointlist->xsize + 1];

if(depth){
draw_series(img , px1, py1, px2, py2, pointlist, depth - 1, R, G, B);
}else{
gfx_line(img, (int)px1, (int)py1, (int)px2, (int)py2, R, G, B);
What does 'gfx_line' do, anything relevant?
}
}

}

The version shown here works fine. My problem occurred with an earlier
rendition of it, which used exactly the same code as above with one
exception. Instead of passing a pointer to the "pointlist" object, it
passed the object itself. To see what the code looked like, just replace
every occurance of "->" with ".", and remove the * in the first line.
With all due respect, seeing "what the code looked like" is pointless
unless we can understand what it _did_, and it's impossible without the
definition of your MAT_matrix template.
When I did that, the value of pointlist.value[0] was set to 0. All of the
other values within it were fine. So for instance, if it contained a
series of values like (3, 1, 4, 1, 5, 9), those values would become (0, 1,
4, 1, 5, 9)
pointlist is an object of a matrix class I made. The "value" variable is a
pointer to some memory allocated using "new".
Here is more reason to see what your MAT_matrix looks like. Do you manage
the dynamic memory correctly? Passing by value uses the copy-constructor,
did you define it properly? Do you know of the "Rule of Three"? Did you
follow it?
It uses a template (in this
case it's a float). I don't know if there's any other information that
would pertain to the problem. Again, it was only the [0] value of the
array that would be set to 0.0. All of the other values in that array
(and all other values within that object) remained unscathed.

Does anyone know what would cause this?


Bad memory management for one.

V
Jul 22 '05 #2

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

Similar topics

20
4807
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
9
2920
by: ckerns | last post by:
I want to loop thru an array of controls,(39 of them...defaults = 0). If value is null or non-numeric I want to assign the value of "0". rowString = "L411" //conrol name if (isNaN(eval ("document.forms."+rowString+".value")) == true ) { //this alert works if the value is a letter,i.e,"a" alert("You have entered an non-numeric...
4
2671
by: Pieter Linden | last post by:
Hi, Hopefully it's just me missing something obvious (but at 2AM, not much is obvious, I guess...) I have a table of courses that people have taken (StudentID,CourseID) and the user selects a student from a combobox on the form, and it highlights courses in the student's specialization (that's fine). But when I run the code to change the...
5
1545
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and if this doesn't see correctly with non-HTML viewers) Ok, I'm fed up with this so I'll explain the situation here and my approach (which sucks),...
2
4986
by: Ashish | last post by:
Hi All, I have a server runnable textbox control. I also have a server side button object but I hook up javascript to it to show a modal dialog. Upon return it places the value in the textbox (client side script). The problem is that after it places the value, postback occurs and value is lost. I have tried overriding SaveViewState and...
2
2719
by: summer00 | last post by:
Hi everyone, I found that the value of a variable(string type for example) is lost after the aspx page postback. E.G: private void Page_Load(object sender, System.EventArgs e) {
3
2516
by: catweezle2010 | last post by:
Hello NG, I have three files (default.aspx, search.aspx and work.aspx). The way is: login on default (if session is newsession). The loginname I write into as sessionvariable (username). So I redirect to my search.aspx. Here I have a form which allows fill in some fields (place, street, name etc.). With this informations i build a sqlquerey...
0
1073
by: alanwo | last post by:
Hi Experts, Interesting finding, when comparing two dictionary of byte(), KeyNotFoundException throwed but, that byte() key is present in another dictionary. Is that the limitation of comparison of dictionary having byte() as key? I just think that dictionary will compare byte arrary internally. Microsoft, what is the workaround? Thanks.
0
1423
vikas251074
by: vikas251074 | last post by:
I am using Oracle 9i and ASP I have empno, empname, designation, category, dob. Category have two option 'S' or 'O' When I enter empno, empname, designation, category and dob and when I press enter key all the data remains on the form but only list value is lost. what should I do ? How can I solve this. <%@ Language=VBScript%>
0
7542
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7467
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7982
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7500
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6066
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5385
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3514
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1961
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 we have to send another system
0
783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.