FLTK logo

Re: Loading multiple image files

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: Loading multiple image files Alvin Mar 15, 2008  
 
Sanji wrote:

> frames_total = choice.count();
> list = new const char*[frames_total];
> for(int i = 0; i < frames_total; i++)
> list[i] = new char[MAX_CHAR];
> for (int i = 0; i < frames_total; i++)
> {
> list[i] = choice.value(i+1);

What is this block of code doing? I read it as:

1. frames_total is the number of entries selected by the user in the
Fl_File_Browser

2. Construct an array of 'frames_total' strings

3. For each entry in list, allocate a string of MAX_CHAR characters

4. For each entry in list, replace the entry with the i+1 entry in the
Fl_File_Browser.

So, I say your problem is with 4. In 3. you allocate a string, then in the
4. you overwrite that sting with string owned by Fl_File_Browser.

I believe you want to make a copy of the value in 4 instead.

Personally, I would simply use strdup() like this:

   frames_total = choice.count();

   list = new char*[frames_total];

   for(int i = 0; i < frames_total; i++)
      list[i] = strdup(choice.value(i+1));

That way all you need is one for-loop. Also notice that I changed list
from 'const char*' to 'char*'.

Of course, if you do use strdup() then you need to change your clean-up code
to use free instead. Something like this:

   if(list)
   {
      for(int i = 0; i < frames_total; ++i)
         free(list[i]);

      delete [] list;
   }

If you do not want to use strdup(), then you need to use strlen(), strcpy()
and delete. I like strdup() cause it's one function all :).

-- 
Alvin
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.