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

Loop Does'nt Work, Hard code does

I'm working on a function which creates a pointers to an array of unsigned
ints based off a number read from a file. I then continue to read file names
from the file, convert the name to a char* and use it to load an texture
from some outside functions. My problem lies in the "for loop", the Code
goes as follows.

//I create the array of pointers, I'm using 2 just for the sake of an
example
MapTextures = new unsigned int[2];

//Start up a for loop
for(int I = 0; I < 2; I++)
{ //I then read the filename from the file.
LevelStream >> Cmd;
//Convert the string I read to a char*
Name = strdup (Cmd.c_str());
//Use the filename to load the coorisponding image into the first
element on the array
MapTextures[i] = LoadTextureWithAlpha(Name);
}

This is the way I'd like to do it, but for some reason it does'nt work and
for the life of me I can't figure out why. When the code executes it will
read both file names and convert them just fine, but it will only load one
texture. The following code I tried while I was troubleshooting and it works
perfectly.

LevelStream >> Cmd;
Name = strdup (Cmd.c_str());
MapTextures[0] = LoadTextureWithAlpha(Name);
LevelStream >> Cmd;
Name = strdup (Cmd.c_str());
MapTextures[1] = LoadTextureWithAlpha(Name);
This way is'nt preferable because it involves hardcoding a set amount, and I
really really want to know why the method involving the "for loop" will only
load the one texture. Given that this peice of code works it would, to me,
imply that the problem I'm having in the "for loop" lies in the fact that it
is a for loop and not the functions within.
Does it have something to do with the way a for loop is handled after
being compiled? Or am I just missing something obvious?
Thanks
Nick

Jul 22 '05 #1
10 2285
> This way is'nt preferable because it involves hardcoding a set amount, and
I
really really want to know why the method involving the "for loop" will only load the one texture. Given that this peice of code works it would, to me,
imply that the problem I'm having in the "for loop" lies in the fact that it is a for loop and not the functions within.
Does it have something to do with the way a for loop is handled after
being compiled? Or am I just missing something obvious?


Probably the error is in your LoadTextureWithAlpha function. Have you tried
using a debugger to find the problem ?

Niels Dybdahl
Jul 22 '05 #2
Probably the error is in your LoadTextureWithAlpha function. Have you tried using a debugger to find the problem ?


No Errors, Between Borland Builder 5 and Visual Studio 6 I'm not getting a
single error in the file loading aspect. Also, how would the loop affect the
Textre loading when the Consecutive hardcode version works fine?

Nick
Jul 22 '05 #3

"Nick L" <Fe********@mchsi.com> wrote in message
news:ElVYc.263740$eM2.220413@attbi_s51...
Probably the error is in your LoadTextureWithAlpha function. Have you tried
using a debugger to find the problem ?


No Errors, Between Borland Builder 5 and Visual Studio 6 I'm not getting a
single error in the file loading aspect. Also, how would the loop affect

the Textre loading when the Consecutive hardcode version works fine?


Could you post the minimal code that compiles and demonstrates your problem
?
Jul 22 '05 #4
> > Probably the error is in your LoadTextureWithAlpha function. Have you
tried
using a debugger to find the problem ?
No Errors, Between Borland Builder 5 and Visual Studio 6 I'm not getting a
single error in the file loading aspect.


But you stated earlier that the filenames were read and converted correctly,
so you call LoadTextureWithAlpha with the correct filename but it does not
load the correct texture. And you still state that there is no error in
LoadTextureWithAlpha ?
Also, how would the loop affect the
Textre loading when the Consecutive hardcode version works fine?


As long as you have not found the reason for the error, you can not be sure
that the loop is causing the problem.

Niels Dybdahl
Jul 22 '05 #5
Could you post the minimal code that compiles and demonstrates your problem ?

That would be difficult. The function that loads in the texture is a
separate *.h file that contains 137 lines of code, all essential to loading
this image plus multiple that file and this file contain multiple opengl
references. If your up for a huge post, I'll do it, but I'm just trying to
save some aggravation of sifting through code. It's all a matter of, I get
no errors(or warning for that matter) when I compile it either way, one way
works and one doesn't. Kinda aggregating.

Nick
Jul 22 '05 #6
But you stated earlier that the filenames were read and converted correctly, so you call LoadTextureWithAlpha with the correct filename but it does not
load the correct texture. And you still state that there is no error in
LoadTextureWithAlpha ?
Yes, but I gave two peices of code, one in a loop and one that followed
the exact same code as that in the loop, but instead of looping twice I just
wrote it twice. Writing it twice works just fine, looping twice does'nt
As long as you have not found the reason for the error, you can not be sure that the loop is causing the problem.


Not going to doubt that all, but all logic I can give to this right now
points to something about the loop. Why would looping twice not work, when
just writing it twice does. The actual reading and loading code never
changed, I just added a "for loop".

Nick

Jul 22 '05 #7
> Not going to doubt that all, but all logic I can give to this right now
points to something about the loop. Why would looping twice not work, when
just writing it twice does. The actual reading and loading code never
changed, I just added a "for loop".


One possible cause could be that you have a dangling pointer or a buffer
overflow somewhere in your application. That could be located anywhere in
your application and the effect might only be visible under some
circumstances; f.ex when the code is formed as a loop.

If you do not want to use a debugger, you might try boundschecker or
something similar instead.

Niels Dybdahl
Jul 22 '05 #8
In article <86VYc.263646$eM2.249913@attbi_s51>,
"Nick L" <Fe********@mchsi.com> wrote:
I'm working on a function which creates a pointers to an array of unsigned
ints based off a number read from a file. I then continue to read file names
from the file, convert the name to a char* and use it to load an texture
from some outside functions. My problem lies in the "for loop", the Code
goes as follows.

//I create the array of pointers, I'm using 2 just for the sake of an
example
MapTextures = new unsigned int[2];

//Start up a for loop
for(int I = 0; I < 2; I++)
{ //I then read the filename from the file.
LevelStream >> Cmd;
//Convert the string I read to a char*
Name = strdup (Cmd.c_str());
//Use the filename to load the coorisponding image into the first
element on the array
MapTextures[i] = LoadTextureWithAlpha(Name);
}
Your code looks like it leaks memory. Why are you using strdup?

const size_t limit = 2;
unsigned* MapTextures = new unsigned[limit];

for ( unsigned i = 0; i < limit; ++i ) {
string Cmd;
LevelStream >> Cmd;
MapTextures[i] = LoadTextureWithAlpha(Cmd.c_str());
}

This is the way I'd like to do it, but for some reason it does'nt work and
for the life of me I can't figure out why. When the code executes it will
read both file names and convert them just fine, but it will only load one
texture.


When you say it will only load one texture, do you mean that both
MapTextures elements contain the same value?
Jul 22 '05 #9
Nick L wrote:
[redacted]


At the risk of sounding incredibly dumb, are you sure that (purely by
accident) you don't have a semicolon in the wrong place? I've done this
on many occasions.

I.e. if your loop is this:

for (int i = 0; i < N; ++i);
{
// do lots of stuff here
}

It won't work. I've done that. And every time I do it, I kick myself.
Jul 22 '05 #10
"Nick L" <Fe********@mchsi.com> wrote in message news:<86VYc.263646$eM2.249913@attbi_s51>...

[ ... ]
//I create the array of pointers, I'm using 2 just for the sake of an
example
MapTextures = new unsigned int[2];
There have been quite a few comments, but I haven't seen any mention
of the error here: your comment says this is an arry of pointers, but
in fact it's an array of unsigned shorts. If the function does what it
seems to imply, this is almost certainly a major problem (unless
you've transcribed your code and made a typo).
//Start up a for loop
for(int I = 0; I < 2; I++)
{ //I then read the filename from the file.
LevelStream >> Cmd;
Unless you're sure your filename will never contain any white space
characters, using an extraction operator to get it is probably a poor
idea.
//Convert the string I read to a char*
Name = strdup (Cmd.c_str());
//Use the filename to load the coorisponding image into the first
element on the array
MapTextures[i] = LoadTextureWithAlpha(Name);
This doesn't look very good to me. First of all, LoadTextureWithAlpha
should really accept a string (or reference to a string) as its
parameter, rather than requiring a char *. Second, even if there's
good reason to pass it a pointer to char, there shouldn't be a problem
with just using:

LoadTextureWithAlpha(Cmd.s_str());

This passes a pointer to const char, but LoadTextureWithAlpha really
shouldn't be modifying its parameter.
This is the way I'd like to do it, but for some reason it does'nt work and
for the life of me I can't figure out why. When the code executes it will
read both file names and convert them just fine, but it will only load one
texture. The following code I tried while I was troubleshooting and it works
perfectly.

LevelStream >> Cmd;
Name = strdup (Cmd.c_str());
MapTextures[0] = LoadTextureWithAlpha(Name);
LevelStream >> Cmd;
Name = strdup (Cmd.c_str());
MapTextures[1] = LoadTextureWithAlpha(Name);


My guess is that you still have the same problem, but something
inconsequential has changed that happens to hid the problem.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #11

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

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
3
by: Jonathan Driller | last post by:
I am very new to this and would greatly appreciate some insight. I am trying to learn Python by doing something useful; write a script that will count and output my aggregated visits to my website....
8
by: Mark Constant | last post by:
I have a xslt file and it keeps giving me an EOF error when it reaches the point <xsl:for-each select="lc:Entertainment/lc:$Hardware"> and <xsl:for-each select="lc:Entertainment/lc:$Hardware"> ...
1
by: Arjen | last post by:
Hello, Here is my file input field: <INPUT id="ImageFileUpload" style="WIDTH: 353px" type="file" name="XmlFileUpload" runat="server" width="300" cssclass="NormalTextBox"> Here is my...
2
by: Raghu Raman | last post by:
Hi , am storing the session id in my database when the user signs in.The session_on start is firing nice & am doing my DB operations there & quite working good. But when the user closes the...
2
by: lotus | last post by:
HI All.. I'm realtively new to C#. I have MainForm which includes Parent usercontol, and this parent usercontrol also contains child usercontrol. MainForm --> Parent usercontrol --> child...
2
by: mrjoka | last post by:
hi experts, i'm developing a page in ASP but i'm doing also some javascript insode the page. i'm creating a frame and i want to loop this frame with a duplicateloop function so the form will be...
3
by: connectwithme | last post by:
I am using the IE 6.x & Firefox 2.x I am trying the event bubbling concept. Herez the code <html> <head> <script> function call_function(evt) { .........some_code_here.......}...
4
matheussousuke
by: matheussousuke | last post by:
Hi, I have just uploaded a website to server, it's working fine, at least for one thing, if login to it on my computer (localhost) the login works fine, but if I do the same on webserver, it doesn't...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
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:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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:
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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...

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.