By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Loop Does'nt Work, Hard code does

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
> 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

P: n/a
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

P: n/a

"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

P: n/a
> > 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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.