Loop Does'nt Work, Hard code does

N

Nick L

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 = 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
 
N

Niels Dybdahl

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
 
N

Nick L

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
 
S

Sharad Kala

Nick L said:
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
?
 
N

Niels Dybdahl

Probably the error is in your LoadTextureWithAlpha function. Have you
tried

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
 
N

Nick L

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
 
N

Nick L

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
 
N

Niels Dybdahl

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
 
D

Daniel T.

Nick L said:
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 = 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 = 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?
 
R

red floyd

Nick said:
[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.
 
J

Jerry Coffin

[ ... ]
//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 = 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top