# how to avoid macro loops

This topic is 2122 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If found some source code that has this in it

#define FOR(q,n) for(int q=0;q<n;q++)
#define SFOR(q,s,e) for(int q=s;q<=e;q++)
#define RFOR(q,n) for(int q=n;q>=0;q--)
#define RSFOR(q,s,e) for(int q=s;q>=e;q--)
#define ESZ(elem) (int)elem.size()

and it is used as

const char** sProgram = new const char*[ESZ(sLines)];
FOR(i, ESZ(sLines))sProgram = sLines.c_str();

How should i make these loops so i dont have to use these macros?
I tried this

const char** sProgram = new const char* [(int)sLines.size()];

int i = 0;
for(i; sLines.size(); i++)
sProgram = sLines.c_str();

but the for loop gets vector out of bounds error when it hits last line in file (this is used in function that reads text file)

##### Share on other sites

for(i; i < sLines.size(); i++)

L. Spiro

##### Share on other sites

ok thanks sorry for asking stupid question

##### Share on other sites

In case you use GCC, you can compile with -save-temps to trivially figure out any macro abuse, no matter how convoluted it is. For every file foo.c you will find a file foo.ii generated, which contains the output of the preprocessor (i.e. with everything included, and all macros expanded). I've found this immensely helpful at times.

##### Share on other sites

I'm with Servant of the Lord on this. Are you really sure you need to do this?

Sometimes you have to question the quality of your sources. In this case, it's not only converting away from std::string to unsafe raw strings that is a code smell, but the use of macros in this situation also smells nasty. Macro usage like this can conceal or even introduce bugs, and detracts from readability, making it a confusing mess for no real reason other than somebody thought they could save some typing. If they took shortcuts there, the remainder of their code might be smelly, too.

What is it you are trying to accomplish in the larger scheme? Maybe we can help you figure out a less smelly method.

##### Share on other sites
Well it is 'simple' process - the function opens file for reading, reads through entire file with this

// get all lines from a file
vector<string> sLines;
char sLine[255];
while(fgets(sLine, 255, fp))sLines.push_back(sLine);
fclose(fp);

then it does this

const char** sProgram = new const char* [(int)sLines.size()];

int i = 0;
for(i; i < sLines.size(); i++)
sProgram = sLines.c_str();

and processes entire file as needed and does

delete[] sProgram;

now when i look at this I see that I never free sLines vector is that needed? Edited by proanim

##### Share on other sites

Ok i made changes and now same stuff happens only difference is now i can't complete the entire process. There is no error or anything the funtion returns false

bool CShader::loadShader(string sFile, int a_iType)
{
//FILE* fp = fopen(sFile.c_str(), "rt");
//if(!fp)return false;

// get all lines from a file
vector<string> sLines;
//char sLine[255];
//while(fgets(sLine, 255, fp))sLines.push_back(sLine);
//fclose(fp);

ifstream infile(sFile.c_str());

while(!infile.eof())
{
string s;
getline(infile, s);
sLines.push_back(s);
}

infile.close();

//const char** sProgram = new const char*[ESZ(sLines)];
//FOR(i, ESZ(sLines))sProgram[i] = sLines[i].c_str();

//const char** sProgram = new const char* [(int)sLines.size()];
vector<const char *> sProgram;

//for(int i; i < sLines.size(); i++)
//	sProgram[i] = sLines[i].c_str();
for(unsigned int i=0; i<sLines.size(); ++i)
sProgram.push_back(sLines[i].c_str());

//delete[] sProgram;

int iCompilationStatus;

if(iCompilationStatus == GL_FALSE)return false;
iType = a_iType;

return 1;
}

What can cause glGetShaderiv(uiShader, GL_COMPILE_STATUS, &iCompilationStatus); to fail ? Shader files that i use with this are basic color shaders and they work with orginal function but now they fail to compile, I am not sure why exactly.

##### Share on other sites

Per the manual page on glCompileShader you can call glGetShaderInfoLog to get some information on why the compile might have failed.

1. 1
Rutin
30
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632961
• Total Posts
3009494
• ### Who's Online (See full list)

There are no registered users currently online

×