Loop Does'nt Work, Hard code does

Discussion in 'C++' started by Nick L, Aug 31, 2004.

  1. Nick L

    Nick L Guest

    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
     
    Nick L, Aug 31, 2004
    #1
    1. Advertising

  2. > 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
     
    Niels Dybdahl, Aug 31, 2004
    #2
    1. Advertising

  3. Nick L

    Nick L Guest


    > 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
     
    Nick L, Aug 31, 2004
    #3
  4. Nick L

    Sharad Kala Guest

    "Nick L" <> 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
    ?
     
    Sharad Kala, Aug 31, 2004
    #4
  5. > > 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
     
    Niels Dybdahl, Aug 31, 2004
    #5
  6. Nick L

    Nick L Guest


    > 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
     
    Nick L, Aug 31, 2004
    #6
  7. Nick L

    Nick L Guest


    > 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
     
    Nick L, Aug 31, 2004
    #7
  8. > 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
     
    Niels Dybdahl, Aug 31, 2004
    #8
  9. Nick L

    Daniel T. Guest

    In article <86VYc.263646$eM2.249913@attbi_s51>,
    "Nick L" <> 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 = 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?
     
    Daniel T., Aug 31, 2004
    #9
  10. Nick L

    red floyd Guest

    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.
     
    red floyd, Aug 31, 2004
    #10
  11. Nick L

    Jerry Coffin Guest

    "Nick L" <> 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 = 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.
     
    Jerry Coffin, Aug 31, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henry Johnson
    Replies:
    3
    Views:
    1,490
    Henry Johnson
    Jun 29, 2004
  2. Sudsy
    Replies:
    7
    Views:
    499
    Andrew Thompson
    Mar 4, 2004
  3. thedarkman
    Replies:
    5
    Views:
    396
    Raymond Schmit
    Apr 6, 2009
  4. Metal Storm

    Between hard code writing hours

    Metal Storm, May 13, 2010, in forum: C Programming
    Replies:
    0
    Views:
    246
    Metal Storm
    May 13, 2010
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page