Loop Does'nt Work, Hard code does

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick L

    #1

    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] = LoadTextureWith Alpha(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] = LoadTextureWith Alpha(Name);
    LevelStream >> Cmd;
    Name = strdup (Cmd.c_str());
    MapTextures[1] = LoadTextureWith Alpha(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



  • Niels Dybdahl

    #2
    Re: Loop Does'nt Work, Hard code does

    > This way is'nt preferable because it involves hardcoding a set amount, and
    I[color=blue]
    > really really want to know why the method involving the "for loop" will[/color]
    only[color=blue]
    > 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[/color]
    it[color=blue]
    > 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?[/color]

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

    Niels Dybdahl


    Comment

    • Nick L

      #3
      Re: Loop Does'nt Work, Hard code does

      [color=blue]
      > Probably the error is in your LoadTextureWith Alpha function. Have you[/color]
      tried[color=blue]
      > using a debugger to find the problem ?[/color]

      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


      Comment

      • Sharad Kala

        #4
        Re: Loop Does'nt Work, Hard code does


        "Nick L" <Fearnot003@mch si.com> wrote in message
        news:ElVYc.2637 40$eM2.220413@a ttbi_s51...[color=blue]
        >[color=green]
        > > Probably the error is in your LoadTextureWith Alpha function. Have you[/color]
        > tried[color=green]
        > > using a debugger to find the problem ?[/color]
        >
        > 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[/color]
        the[color=blue]
        > Textre loading when the Consecutive hardcode version works fine?[/color]

        Could you post the minimal code that compiles and demonstrates your problem
        ?


        Comment

        • Niels Dybdahl

          #5
          Re: Loop Does'nt Work, Hard code does

          > > Probably the error is in your LoadTextureWith Alpha function. Have you[color=blue]
          > tried[color=green]
          > > using a debugger to find the problem ?[/color]
          >
          > No Errors, Between Borland Builder 5 and Visual Studio 6 I'm not getting a
          > single error in the file loading aspect.[/color]

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

          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


          Comment

          • Nick L

            #6
            Re: Loop Does'nt Work, Hard code does

            [color=blue]
            > Could you post the minimal code that compiles and demonstrates your[/color]
            problem[color=blue]
            > ?[/color]
            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


            Comment

            • Nick L

              #7
              Re: Loop Does'nt Work, Hard code does

              [color=blue]
              > But you stated earlier that the filenames were read and converted[/color]
              correctly,[color=blue]
              > so you call LoadTextureWith Alpha with the correct filename but it does not
              > load the correct texture. And you still state that there is no error in
              > LoadTextureWith Alpha ?[/color]

              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
              [color=blue]
              > As long as you have not found the reason for the error, you can not be[/color]
              sure[color=blue]
              > that the loop is causing the problem.[/color]

              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



              Comment

              • Niels Dybdahl

                #8
                Re: Loop Does'nt Work, Hard code does

                > Not going to doubt that all, but all logic I can give to this right now[color=blue]
                > 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".[/color]

                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


                Comment

                • Daniel T.

                  #9
                  Re: Loop Does'nt Work, Hard code does

                  In article <86VYc.263646$e M2.249913@attbi _s51>,
                  "Nick L" <Fearnot003@mch si.com> wrote:
                  [color=blue]
                  > 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] = LoadTextureWith Alpha(Name);
                  > }[/color]

                  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] = LoadTextureWith Alpha(Cmd.c_str ());
                  }

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

                  When you say it will only load one texture, do you mean that both
                  MapTextures elements contain the same value?

                  Comment

                  • red floyd

                    #10
                    Re: Loop Does'nt Work, Hard code does

                    Nick L wrote:[color=blue]
                    > [redacted]
                    >[/color]

                    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.

                    Comment

                    • Jerry Coffin

                      #11
                      Re: Loop Does'nt Work, Hard code does

                      "Nick L" <Fearnot003@mch si.com> wrote in message news:<86VYc.263 646$eM2.249913@ attbi_s51>...

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

                      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).
                      [color=blue]
                      > //Start up a for loop
                      > for(int I = 0; I < 2; I++)
                      > { //I then read the filename from the file.
                      > LevelStream >> Cmd;[/color]

                      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.
                      [color=blue]
                      > //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] = LoadTextureWith Alpha(Name);[/color]

                      This doesn't look very good to me. First of all, LoadTextureWith Alpha
                      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:

                      LoadTextureWith Alpha(Cmd.s_str ());

                      This passes a pointer to const char, but LoadTextureWith Alpha really
                      shouldn't be modifying its parameter.
                      [color=blue]
                      > 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] = LoadTextureWith Alpha(Name);
                      > LevelStream >> Cmd;
                      > Name = strdup (Cmd.c_str());
                      > MapTextures[1] = LoadTextureWith Alpha(Name);[/color]

                      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.

                      Comment

                      Working...