Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 394489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:21:39+00:00 2026-05-12T16:21:39+00:00

Below the map ‘widgets’ is always size of 1 for some reason. There should

  • 0

Below the map ‘widgets’ is always size of 1 for some reason. There should be 4 when it’s done.

Output:

Widget: widget_ram_label:layout_bar:0 1
Widget: widget_ram_active:layout_bar:0 1
Widget: widget_ram_total:layout_bar:0 1
Widget: widget_wlan0_label:layout_bar:0 1

Here’s widgets:

std::map<const char *, Widget *> widgets;

And here’s the code:

void Generic::BuildLayouts() {
   for(std::map<const char*, std::vector<widget_template> >::iterator l = 
       widget_templates.begin(); l != widget_templates.end(); l++) {
       std::vector<widget_template> layout = l->second;
       for(unsigned int i = 0; i < layout.size(); i++ ) {
           Json::Value *widget_v = CFG_Fetch_Raw(root, layout[i].key);
           if(!widget_v) {
               error("No widget named <%s>", layout[i].key);
               continue;
           }
           Json::Value *type = CFG_Fetch_Raw(widget_v, "type");
           if(!type) {
               error("Widget <%s> has no type!", layout[i].key);
               delete widget_v;
               continue;
           }

           Widget *widget;

           char name[256];
           sprintf(name, "%s:%s", layout[i].key, l->first);
           char tmp[256];
           int i = 0;
           sprintf(tmp, "%s:%d", name, i);

           while(widgets.find(tmp) != widgets.end()) {
               i++;
               sprintf(tmp, "%s:%d", name, i);
           }
           memcpy(name, tmp, 256);

           if(strcmp(type->asCString(), "text") == 0) {
               widget = (Widget *) new WidgetText(this, name, widget_v, 
                   layout[i].row, layout[i].col);
               std::cout << "Widget: " << name << " " << widgets.size() << std::endl;
           } else {
               error("Unknown widget type: %s", type->asCString());
           }
           if(widget) {
               widgets[name] = widget;
           }
           delete type;

       }
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:21:39+00:00Added an answer on May 12, 2026 at 4:21 pm

    Maybe because all your name pointers poitns to the same buffer? Because the content of name changes, but not the value of the pointer to name in the map.

    Try to use std::string instead.

    Replace you name buffer by

    #include <string >
    //...
    std::string name = "the name";
    

    and replace your map by

    std::map< const std::string , Widget* > widgets;
    

    That will make your life easier, safer and more readable.

    To help formatting, use std::stringstream or boost string algorithms.


    A start of example, this code :

                                char name[256];
                sprintf(name, "%s:%s", layout[i].key, l->first);
                char tmp[256];
                int i = 0;
                sprintf(tmp, "%s:%d", name, i);
    
                while(widgets.find(tmp) != widgets.end()) {
                    i++;
                    sprintf(tmp, "%s:%d", name, i);
                }
                memcpy(name, tmp, 256);
    

    would be written like this:

    Widget *widget = NULL; // always initialize your variables!!!
    
    std::stringstream name_stream; // this will let us format our string
                    name_stream << layout[i].key << ":" << l->first;
    
    std::string name = name_stream.str(); // now we got the string formated.
    
    std::stringstream tmp_stream; // same for tmp
    tmp_stream << name << ":" << i; // will automatically convert basic types, see the doc if you want specific formatting
    
    std::string tmp = tmp_stream.str(); // now we got the string formated.
    
    // the while loop have no sense : a map have only one value by key
    // if you want to have several values by key, use std::multi_map instead -- it don't work exactly the same though
    // for now i'll just assume you just need to find the value associated to the name:
    
    
    typedef std::map< const std::string, Widget* > WidgetMap; // for ease of writing, make a shortcut! ease your life!
    WidgetMap::iterator tmp_it = widgets.find( tmp );
    
    if( tmp_it != widgets.end() ) 
    {   
    // starting here I don't understand you code, so I'll let you find the rest :)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is a stored procedure to check if there is a duplicate entry in
Below I have a very simple example of what I'm trying to do. I
Below are two ways of reading in the commandline parameters. The first is the
Below is my current char* to hex string function. I wrote it as an
Below is part of the XML which I am processing with PHP's XSLTProcessor :
Below is my $.ajax call, how do I put a selects (multiple) selected values
Below is the code of a simple html with a table layout. In FF
Below are lines from the c++ programming language template<class T > T sqrt(T );
Below is my (simplified) schema (in MySQL ver. 5.0.51b) and my strategy for updating
Below is my table, a User could have multiple profiles in certain languages, non-English

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.