lesson of C++ / strcat (segmentation fault)

Hi,

some beginners question : I would like to draw an array of random letters, but after the loop of initiziliastion of letters, i have a segmentation fault, why ? is it better way to do this ?
in a previous version the loop changed my _nbLignes _ and _nbColonnes _ variables without error…

Could someone answer to this question and help my little brain ?

DRAW LOOP ------------------

  
//--------------------------------------------------------------  
void testApp::draw(){  
  
    char affich_string[60] = {0};  
    char letter[1];  
    int n;  
    int asciiCode;  
  
    for (int i=0; i < 60; i++){  
        asciiCode = ofRandom(65,90);  
        sprintf(letter,"%c",asciiCode);  
        strcat(affich_string,letter);  
    };  
  
  
	ofSetColor(0x00FF00);  
	cout << affich_string << ":" << counter << "\n";  
  
    for (int ii=0; ii<60; ii++){  
        int idxLignes = ii/nbLignes;  
        int idxColonnes = ii%nbColonnes;  
        int step = 40;  
  
        sprintf(letter,"%c",affich_string[ii]);  
        franklinBook.drawString(letter,(idxColonnes+1)*step,(idxLignes+1)*step);  
  
    };  
  
}  

CLASS DECLARATION ----------------------------------------------

  
class testApp : public ofBaseApp{  
  
	public:  
		void setup();  
		void update();  
		void draw();  
  
		void keyPressed  (int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void windowResized(int w, int h);  
  
		ofTrueTypeFont  franklinBook;  
		ofTrueTypeFont	verdana;  
//		ofTrueTypeFont  franklinBookLarge;  
  
		float 			counter;  
		int nbLignes;  
		int nbColonnes;  
  
  
};  

the cause is probably:

  
char letter[1];   
...  
sprintf(letter,"%c",asciiCode);    

c strings are 0 terminated, that means that any string has the lenght of the string that it contains + 1 for the 0 terminator, so letter should have size 2

an easier way to do this would be something like:

  
  
string affich_string;  
string letter;  
letter.resize(2,0);  
for (int i=0; i < 60; i++){  
    letter[0] =  ofRandom(65,90);  
    affich_string += letter;  
}  
  

Thx for that quick reply.

After these corrections,

it seems that drawing almost 500 letters randomly is very slow. Is there a normal phenomenon ? is there a way to do it efficiently ?

Thx for helping in advance.

yes, drawing letters is not very efficient in ofTrueTypeFont, the problem is it needs to bind a different texture for each character + upload the vertexes for the quad. it should be possible to make it way faster by using a texture atlas instead of individual textures for each char

Do you mean drawing one string instead of each char ? Because i tried this way with quite the same result.

it’s nothing you can do from your application it needs some changes in the same ofTrueTypeFont class. When it loads a font it generates a texture for each char in it, instead of that generating one big texture with all the characters in it + texture coordinates for each character will make it much faster cause it will avoid binding and unbinding the texture for each drawn character

Is something planned for OF 007 :wink: ?

no : )

i was curious about this and just implemented it : ) haven’t tested it very throughly so if you find any glitches let me know.

it’s in my ttfTextureAtlas branch:

https://github.com/arturoc/openFrameworks/tree/ttfTextureAtlas

also i’m on linux so the project files for osx can be missing some file and windows is probably super broken

also to make it faster to render several chars or strings one after another you can use:

  
  
font.bind();  
font.drawString(...)  
font.drawString(...)  
font.drawString(...)  
font.unbind();  
  

Ok I’ve downloaded your ttf class, and…

[sub]||=== texts, debug ===|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|6|error: expected unqualified-id before ‘<’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|9|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|11|error: expected constructor, destructor, or type conversion before ‘.’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|11|error: expected )' before '{' token| ..\..\..\libs\openFrameworks\graphics\ofTrueTypeFont.h|11|error: expected unqualified-id before ')' token| ..\..\..\libs\openFrameworks\graphics\ofTrueTypeFont.h|22|error: expected unqualified-id before '<' token| ..\..\..\libs\openFrameworks\graphics\ofTrueTypeFont.h|25|warning: character constant too long for its type| ..\..\..\libs\openFrameworks\graphics\ofTrueTypeFont.h|27|error:var’ does not name a type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|39|error: expected unqualified-id before ‘)’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|43|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|43|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|50|error: expected constructor, destructor, or type conversion before ‘.’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|50|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|51|error: expected constructor, destructor, or type conversion before ‘.’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|52|error: expected constructor, destructor, or type conversion before ‘.’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|52|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|53|error: expected constructor, destructor, or type conversion before ‘.’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|56|error: expected unqualified-id before ‘<’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|61|error: expected constructor, destructor, or type conversion before ‘.’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|61|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|61|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|62|error: expected constructor, destructor, or type conversion before ‘.’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|62|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|63|error: expected constructor, destructor, or type conversion before ‘.’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|63|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|64|error: expected constructor, destructor, or type conversion before ‘.’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|64|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|65|error: expected `)’ before ‘{’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|66|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|67|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|67|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|67|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|67|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|68|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|70|error: expected unqualified-id before ‘)’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|71|error: expected unqualified-id before ‘<’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|206|invalid suffix “Release” on integer constant|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|211|invalid suffix “_android” on integer constant|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|216|invalid suffix “_features” on integer constant|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|221|invalid suffix “drestructuring” on integer constant|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|350|error: invalid token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|350|error: expected unqualified-id before ‘<’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|382|error: expected unqualified-id before ‘<’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|390|error: expected unqualified-id before ‘<’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|400|error: expected unqualified-id before ‘<’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|415|error: expected unqualified-id before ‘<’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|476|error: missing terminating ’ character|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|477|error: stray ‘\226’ in program|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|477|error: stray ‘\134’ in program|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|477|error: stray ‘\145’ in program|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|477|error: stray ‘\226’ in program|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|477|error: stray ‘\134’ in program|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|477|error: stray ‘\147’ in program|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|484|error: expected unqualified-id before ‘<’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|521|warning: character constant too long for its type|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|550|error: missing terminating ’ character|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|568|error: expected constructor, destructor, or type conversion before ‘;’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|568|error: expected unqualified-id before ‘<’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|569|error: expected constructor, destructor, or type conversion before ‘;’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|569|error: expected constructor, destructor, or type conversion before ‘;’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|569|error: expected constructor, destructor, or type conversion before ‘;’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|569|error: expected unqualified-id before ‘<’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|569|invalid suffix “bfa51120d4f3b55939” on integer constant|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|573|error: expected unqualified-id before ‘<’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|764|error: invalid token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|764|error: invalid token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|764|error: expected constructor, destructor, or type conversion before ‘&’ token|
…\libs\openFrameworks\graphics\ofTrueTypeFont.h|764|error: expected unqualified-id before ‘<’ token|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options…|
||=== Build finished: 50 errors, 18 warnings ===|
[/sub]

I believ i’m on windows :slight_smile:

have you downloaded only the class? that’s not going to work, you’ll need to download the whole package, it’s depending on several other classes new in 007 and some functions even in that branch.

Of course just the class :stuck_out_tongue:

But I also try with getCharacterAsPoints it’s really quick.
Il also will try your whole package to test…