Loop Does'nt Work, Hard code does

From: Nick L (Fearnot003_at_mchsi.com)
Date: 08/31/04


Date: Tue, 31 Aug 2004 06:43:16 GMT

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



Relevant Pages

  • Re: newbie question
    ... The second assigns a pointer to an array 1000 of char to an int pointer. ... What you pass are the file pointers, ... > second file is identical to the first. ... Look at your while loop. ...
    (comp.lang.c)
  • Re: loop performance question
    ... | such that every possible pair in the array is tested. ... | whenever I access any data in the second loop. ... The pointers to separately allocated objects break the ... Then comes the question of how to avoid cache misses when accessing obj2. ...
    (comp.lang.cpp)
  • Re: Differance between Array and Pointers
    ... Well, except that arrays tend to be much larger than pointers, and the ... An array is a contiguous region of memory containing N elements of M ... indexing vs. a loop). ...
    (comp.arch.embedded)
  • Re: Why does this work? (overloads)
    ... one type to an array originally declared with another type. ... pointers to a common unconstrained array type, ... x: A renames c(0 .. ... for k in v'range loop ...
    (comp.lang.ada)
  • Re: best method to find the freequent numbers
    ... My array size is huge. ... This could save memory, but wouldn't ... less than *max - *min, you can stop counting and break out of the loop, ... A little simpler might be (no need to keep pointers to maximum and ...
    (comp.lang.c)