812 questions
-2
votes
1
answer
199
views
Simplifying development. C++ function retaining value across calls from C++ or Python, with independent invocation in 2+ places in the same program
Having the scenario of a C++ library where a function does some calculations as it receives a container normally (not always) across multiples calls, and where the function needs to retain an ...
2
votes
1
answer
84
views
Testing functions with static variables in C
I have a problem performing several scenarios one by one in unit testing. I'm testing a function, where static variables are used and they store states of variables after previous test scenarios. Is ...
7
votes
1
answer
579
views
Why does "the concept of a single-threaded program doesn't exist to Rust" with respect to mutable static variables?
I'm learning Rust and while doing research on static variables I came across this comment from 2018:
Rust requires everything to be thread-safe (even if you're not using
threads, the concept of a ...
-1
votes
1
answer
58
views
Nested thread_local variable [duplicate]
When declaring static thread_local variable, then each thread has a copy of that variable. Imagine a thread then spins another thread, will this variable still be a copy in the nested thread?
0
votes
0
answers
102
views
How to ensure initialization of static variables without explicitly including the header file
Considering the following example: A class named ObjectFactory is defined to manage the creation of several classes. These managed classes should be registered with the factory so that we can create ...
0
votes
1
answer
93
views
Is this static struct variable guaranteed to be zero initialized apart from the one field which isn't?
Inside a function, I have a static variable which is a struct, and only one of its fields are initialized:
void func() {
static constexpr xcb_change_window_attributes_value_list_t root_mask {
....
-1
votes
1
answer
1k
views
In Rust, how to create a globally shared singleton with `OnceLock`?
Assuming that we need to define a singleton with global read access with thread safety, the canonical method is to use OnceLock:
/// A synchronization primitive which can be written to only once.
///
/...
1
vote
0
answers
92
views
Why constructor of a statically created object is not executed before main function if the object is defined in library?
I am trying to create "plugin" factory. When an user requests creation of an object factory queries it's child factories and picks suitable one to create the object.
Instances of child ...
0
votes
2
answers
117
views
Does the presence of static variables change the time complexity of a recursive function? If so how does it do so, and if no, explain the below code
int e(int x, int n)
{
static int f=1, p=1; // f for factorial evaluation & p for power evaluation
int r;
if(n==0){
return 1;
}
else {
r = e(x, n-1);
p = p*x;
f = f*n;
...
1
vote
0
answers
79
views
C++ static member variable is not initialized if it's in an static library [duplicate]
I want to implement a self-register pattern in c++. My goal is to have one file defining a class and the class can self-register to a registry. My implmentation goes like this
https://godbolt.org/z/...
2
votes
1
answer
41
views
Python Importing Same Object via Different Paths - Different Behaviour Between Class Attribute and Primitive
Python 3.10.6
Suppose you have the following files:
main.py
setter.py
Folder/shared.py
With these contents:
# Folder/shared.py
class Shared:
i = 0 # something that is used in multiple locations
#...
0
votes
2
answers
130
views
How to check if a function is called for the first time or a subsequent call? [duplicate]
I have a function
def compute_daily_quantities():
# compute some quantities, one of which is the variable, value
After the first call to compute_daily_quantities, we need to add a constant, c to ...
1
vote
1
answer
209
views
Swap alternate elements in an array using recursion?
I wrote the below code to swap alternate elements in an array {eg. (1,2,3,4,5)-->(2,1,4,3,5) }. I wanted to try it using recursion.
#include <iostream>
using namespace std;
void print(int *a,...
0
votes
0
answers
61
views
How to use static variables with templates in C++ [duplicate]
I am just learning templates in C++ and I came across the idea of using static variables in templates. I created a simple class containing a three dimensional vector. I decided to keep the 'unit caps'(...
2
votes
1
answer
547
views
What are the criteria for deciding to use a static variable for optimization?
Here is an example code why I started thinking about this
void renderScene(void)
{
//Clear all pixels
glClear(GL_COLOR_BUFFER_BIT);
// Question : Declaring posAttrib as a static variable ...
0
votes
1
answer
45
views
Do static and global modifiers for variables implement a non-modifiable reference?
Source
The PHP doc says
PHP implements the static and global modifier for variables in terms
of references.
<?php
function test_global_ref() {
global $obj;
$new = new stdClass;
$obj = ...
0
votes
0
answers
48
views
C++ std::list segmentation fault with different compiling order
I have the following testing program:
// manager.h
#pragma once
#include <list>
namespace testapp
{
class Manager
{
public:
static Manager& instance();
void ...
1
vote
2
answers
152
views
How to make increment/decrement static variable for an API?
#include <iostream>
class Test {
public:
static int numelem;
Test() {}
~Test() {}
int increment();
};
int Test::numelem = 0;
int Test::increment()
{
return ++Test::numelem;
}...
0
votes
0
answers
40
views
Is this member variable used to register subclasses in this protype design pattern? If yes, how?
I am studying this piece of code in the book "Design Patterns explained simply".
It is the implementation of proytpe design pattern by the author Alexander Shvets.
Although I thikn I have ...
0
votes
0
answers
54
views
What is this concept where we initilize a call to function, in cpp? [duplicate]
I was reading an article on references in CPP, where I found this example and couldn't understand how is that we are able to call a function and initialize a variable inside its call block.
As the ...
-1
votes
2
answers
127
views
C++. What's the best way to initialize multiple static variables in a templated class?
There are class with multiple static variables, e.g.
template<class T>
struct A
{
// some code...
static int i;
static short s;
static float f;
...
2
votes
3
answers
607
views
Weird behavior of getpwnam
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("%s %s\n", getpwnam("steve")->pw_name, getpwnam("root")->...
1
vote
1
answer
447
views
Why do I get double binary size when I instantiate a static object inside a free function in C++?
On an embedded system (Cortex-M4) I write C++ that compiles with GCC arm-none-eabi-g++. Compiler version 10.2.1 20201103.
My code is kind of complicated to copy paste it here, so here's an example:
I ...
1
vote
0
answers
62
views
Chicken and egg problem in Java class initialization [duplicate]
Say we have this class:
public class Chicken {
public static final Chicken THE_FIRST_ONE = new Chicken("Little");
public static final Chicken THE_FIRST_EGG_1 = new Egg("Humpty-...
2
votes
1
answer
4k
views
My static variables in my Blazor Server app are keeping their values, even if I refresh the page or even I close the tab and login again. Why?
I have a Blazor server app. Some variables on a specific razor page (main.razor) are defined as static because I want that these variables keep their values when the client navigates to other pages in ...