I have declared the one static member inside the static method. like as follws:
static void temp1(param..){
static gint x,y ;
#TODO what you needed
values get changed here for x,y;
}
And I want to access this Two in other static method within the same file.
static void temp2 ( param .......){
accessing the x,y
}
How should I do it..? I don't want to declare public member and also don't want to change the method param's .
This might almost be what you want:
static gint x,y ;
static void temp1(param..){
/* TODO what you needed */
values get changed here for x,y;
}
static void temp2 ( param .......){
/* accessing the x,y */
}
x and y are globally accessible, but only within the file, just like your static procedures. I think this is as close as you can get to what you want.
You need to understand these 2 things:
Scope
and
Lifetime
the scope of your static variables is only inside the function they are declared. they cannot be accessed outside.
but the lifetime of your variables is throughout your program, that is they will retain the values until the program is running.
So maybe you would like to declare your variables outside of your function. so instead of
static void temp1(param..){
static gint x,y ;
#TODO what you needed
values get changed here for x,y;
}
you can have
static gint x,y ;
static void temp1(param..){
#TODO what you needed
values get changed here for x,y;
}
The exact use case you have, i think it would not be possible without changing the second function's arguments.
static int getInnerStatic(int* _x, int* _y, int ignore);
static void temp1(param..){
static int x,y ;
////////////////
if (getInnerStatic(&x,&y,1))
return;
////////////////
#TODO what you needed
values get changed here for x,y;
}
static int getInnerStatic(int* _x, int* _y, int ignore){
static int innerInvok = 0;
static int x, y;
if (innerInvok == 1){
x = *_x;
y = *_y;
return innerInvok;//1
}
if (ignore)
return innerInvok;//0
innerInvok = 1;
temp1(/*anything as param...*/);
innerInvok = 0;
*_x = x;
*_y = y;
return innerInvok;//0
}
//get static x y :
static void temp2 ( param .......){
int getX, getY;
getInnerStatic(&getX, &getY, 0); // <- accessing the x,y
}
Here is an example of what you are trying to do:
#include <iostream>
using namespace std;
static void temp1() {
static int x,y ;
x = 5;
y = 8;
}
static void temp2 (){
cout << temp1::x << endl;
}
int main() {
temp2()
return 0;
}
Error message
error: ‘temp1’ is not a class or namespace
Note the error that occurs when you try to access x in temp1 by using the scope resolution operator ::. Here is how to solve this
#include <iostream>
using namespace std;
namespace temp {
class temp1 {
public:
static int x,y;
};
int temp1::x = 5;
int temp1::y = 7;
}
static void temp2 (){
cout << temp::temp1::x << endl;
}
int main() {
temp2();
return 0;
}
Note the namespace is not necessary, but I used it to keep related data together
You cant do that with your existing code you need to modify your code to make x and y as static instance variables so that you can access them in all the static methods.
Related
TL;DR What should the type of x be if x = x(); is valid in C?
The whole story:
I am working on a simple game with multiple scenes. At first my code looked like this:
enum {kSCENE_A, kSCENE_B} ecene = kSCENE_A;
int main() {
while(1) {
switch(scene) {
case kSCENE_A:
// Renders scene a
// And possibly modifies `scene`
break;
case kSCENE_B:
// Renders scene b
// And possibly modifies `scene`
break;
}
}
}
However the main function is too verbose. I extracted the rendering part into different functions, but the switch still makes the code ugly. I defined a scene_map for this:
typedef enum {
kSCENE_A,
kSCENE_B,
kN_SCENES
} Scene;
Scene RenderSceneA();
Scene RenderSceneB();
int main() {
Scene scene = kSCENE_A;
Scene (*scene_map[kN_SCENES])();
scene_map[kSCENE_A] = SceneA;
scene_map[kSCENE_B] = SceneB;
while(1) scene = scene_map[scene]();
}
But I wonder if it would be possible in C that I write the code in this, or some similar way:
SomeType RenderSceneA();
SomeType RenderSceneB();
int main() {
SomeType scene = RenderSceneA;
while(1) scene = scene();
}
So what type should SomeType be, or can I only use void * for it? If the latter is true, how can I write this code in a clear manner than demonstrated in the second code block?
Here's a solution that will solve the problem. Note that it uses void (*)(void) as a generic function pointer type, as opposed to void * which isn't guaranteed to work for function pointers:
#include <stdio.h>
void (*f(void))(void);
void (*g(void))(void);
void (*f(void))(void)
{
printf("This is f.\n");
return (void (*)(void)) g;
}
void (*g(void))(void)
{
printf("This is g.\n");
return (void (*)(void)) f;
}
#define SRCALL(p) ((void (*(*)(void))(void)) p())
int main(void)
{
void (*(*p)(void))(void);
p = f;
p = SRCALL(p);
p = SRCALL(p);
p = SRCALL(p);
return 0;
}
The function pointer casts are ugly, so I encapsulated them in the macro SRCALL (self-ref-call). The output is:
This is f.
This is g.
This is f.
I am trying to create a small fixed size list of string, int tuples. A fixed size array of structs seemed like the way to go, but when manipulating the array entries, I constantly run into memory errors. What I've tried so far:
public struct S {
public string a;
public int b;
public S (string a, int b) {
this.a = a;
this.b = b;
}
}
public class Test {
public S arr[5];
public static void main () {
var test = new Test ();
test.arr[0].a = "hi";
test.arr[0].b = 5;
/* alternatively: */
//test.arr[0] = S ("hi", 5);
}
}
I have looked into the compiled C code, but I am not really familiar with C.
I read everything I found about vala structs and arrays of structs, but the little bit that's out there didn't enlighten me either.
The fixed size array seems to get initialized with "empty" structs, do I need to initialize it beyond that, somehow?
What am I misunderstanding about arrays of structs here?
Is there an alternative way to implement a fixed size list of string, int tuples? Are arrays of structs not suited for that?
Any help is greatly appreciated! It seems like such a simple task, but I've been struggling with it for days now :/ ...
First, you can make the C code quite a bit simpler by specific "Compact" on the class and disabling the type on the struct:
[CCode(has_type_id = false)]
public struct S {
public string a;
public int b;
public S (string a, int b) {
this.a = a;
this.b = b;
}
}
[Compact]
public class Test {
public S arr[5];
public static void main () {
var test = new Test ();
test.arr[0].a = "hi";
test.arr[0].b = 5;
/* alternatively: */
//test.arr[0] = S ("hi", 5);
}
}
Not a full answer, but it seems like there is a problem in the compiler generated destruction code:
void test_free (Test* self) {
_vala_array_destroy (self->arr, 5, (GDestroyNotify) s_destroy);
g_slice_free (Test, self);
}
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
if ((array != NULL) && (destroy_func != NULL)) {
int i;
for (i = 0; i < array_length; i = i + 1) {
if (((gpointer*) array)[i] != NULL) {
destroy_func (((gpointer*) array)[i]);
}
}
}
}
Note how the array parameter (which is of type gpointer, but was casted from an S[], namely arr) is casted to a gpointer* before the destroy_func () is called on it.
That would be fine if arr were a dynamic array, but it isn't.
If I modify the compiler output by hand everything works fine:
static void _vala_array_destroy (S* array, gint array_length, GDestroyNotify destroy_func) {
if ((array != NULL) && (destroy_func != NULL)) {
int i;
for (i = 0; i < array_length; i = i + 1) {
if (&array[i] != NULL) {
destroy_func (&array[i]);
}
}
}
}
The destroy function (destroy_func aka s_destroy) is now called on a valid S* (the address of the struct inside the array).
So it seems to me that you have discovered a compiler bug.
PS: Using a dynamic array works just fine, I would either do that or use some higher level data type like a Gee.ArrayList instead of a static array.
I'm new to C and I'm trying to simulate an Undo functionality for a problem. I'm using generic vectors defined like this:
typedef void* Element;
typedef struct {
Element* elems;
int size;
int capacity;
} Vector;
For this, I created a function called "Copy" that should return me a copy of the vector I'm passing:
Vector* copyVector(Vector *v) {
Vector* rez;
rez = createVector();
int i;
for (i = 0; i < getSize(v); i++) {
Element el = getElem(v, i);
add(rez, el);
}
return rez;
}
It works when I call it everytime to save the "before" vector... like when I try to apply an add or remove on my current vector, I call this copy function first on another vector called undoVec like this:
undoVec = copyVector(v);
I checked and it works but when I call my undo function... which should do the reverse of the code before:
v = copyVector(undoVec);
It's not working anymore. Doesn't do anything. It wont modify my vector v... which is really just a pointer I think
void undoVector(Vector *v, Vector *undoVec)
What am I doing wrong? why wont this functionality work? I can paste more code or give more info if required, thanks.
void add(Vector *v, Element elem) {
if (v->size == v->capacity) {
isFull(v);
}
v->elems[v->size] = elem;
v->size++;
}
Element getElem(Vector *v, int pos) {
return v->elems[pos];
}
Use a stack to implement because stack has a top.
To elaborate on The Dark's comment:
Are you assigning v = copyVector(undoVec); in the undoVector function?
If so that will only change the value of the parameter inside
undoVector, not the value of the what ever you called the function
with.
If you have
void undoVector(Vector *v, Vector *undoVec)
{
v = copyVector(undoVec);
}
…
undoVec = copyVector(v);
…
undoVector(v, undoVec);
the parameter v in undoVector() is a different object from v outside of undoVector(), it's just a copy that has initially the same value; thus, the v = copyVector(undoVec) only changes the value of the parameter v in undoVector(), but leaves untouched the independent v outside.
To achieve what you want, make it
void undoVector(Vector **v, Vector *undoVec)
{
*v = copyVector(undoVec);
}
…
undoVec = copyVector(v);
…
undoVector(&v, undoVec);
or - more similar to copyVector() and simple -
Vector *undoVector(Vector *undoVec)
{
return copyVector(undoVec);
}
…
undoVec = copyVector(v);
…
v = undoVector(undoVec);
or - most simple -
undoVec = copyVector(v);
…
v = copyVector(undoVec);
This code compiles but crashes instantly. I've only tried it on devcppPortable. I am trying to make a class that can store a lot of complex data (i.e. an object that has multiple property sets and each set carries multiple figures).
Each object created would have a unique number of property sets and internal property values. I would like to be able to shape the class upon declaration so as not to allocate a bunch of unused space. Is something like this even possible?
#include<iostream>
using namespace std;
class a
{
public:
int amount;
struct b
{
int max;
int* prop;
b() {}
void set(int&);
~b(){delete prop;}
};
b* property;
a(int amt, int max0, int max1=0, int max2=0);
~a(){delete property;}
};
int main()
{
a object(2, 3, 5);
return 0;
}
a::a(int amt, int max0, int max1, int max2)
{
amount = amt;
property = new b[amt];
switch(amt)
{
case 3:
property[2].set(max2);
case 2:
property[1].set(max1);
case 1:
property[0].set(max0);
}
}
void a::b::set(int& m) {max = m; prop = new int[max];}
You allocate property with new[] but deleting it with delete, not delete[]
I made a mistake while using union,and I don't know why.
the problem is in the function goto_xy();
I read it from the book, but it cannot be compiled.
In this function I am trying to locate the cursor, but REGS variable is not declared. I want to know what is its function.
#include<stdio.h>
#include<windows.h>
#include<dos.h>
#include<conio.h>
void goto_xy(int x,int y); //goto is a key word;define the subfunction to creat the original cursor int the coordinate system
void rectangle_clear(int x1,int x2,int y1,int y2); //define the rectangle_clear opening subfunction
void center_clear(int x1,int x2,int y1,int y2); //define the center_clear opening subfunction
void creat(); //define the subfunction of creating the star
int main() //the main function
{
creat();
getch();
center_clear(0,25,0,79);
getch();
}
void center_clear(int x1,int x2,int y1,int y2) //the subfunction which creats the stars while opening the project
{
int x00,y00,x0,y0,i,d;
if((y2-y1)>(x2-x1))
{
d=(x2-x1)/2;
x0=(x1+x2)/2;
y0=y1+d;
y00=y2-d;
for(i=0;i<(d+1);i++)
{
rectangle_clear((x0-i),(x00+i),(y0-i),(y00+i));
}
delay(10); //to delay the dismis of the star
}
else
{
d=(y2-y1)/2;
y0=(y1+y2)/2;
x0=x1+d;
x00=x2-d;
for(i=0;i<d+1;i++)
{
rectangle_clear((x0-i),(x00+i),(y0-i),(y00+i));
}
delay(10);
}
}
void rectangle_clear(int x1,int x2,int y1,int y2) //to creat the star int the shape of a rectangle
{
int i,j;
for(i=y1;i<y2;i++)
{
goto_xy(x1,i);
putchar(' ');
goto_xy(x2,i);
putchar(' ');
delay(10);
}
for(j=x1;j<x2;j++)
{
goto_xy(i,y1);
putchar(' ');
goto_xy(i,y2);
putchar(' ');
delay(10);
}
}
void goto_xy(int x,int y)
{
union REGS r;
r.h.ah=2;
r.h.dl=y;
r.h.dh=x;
r.h.bh=0;
int86(0x10,&r,&r);
}
void creat()
{
int i,j;
for(i=0;i<24;i++)
{
for(j=0;j<79;j++)
{
goto_xy(i,j);
printf("a");
}
}
}
It mostly appears to me that the union REGS must be already present in one of the header files and you are including the same.
As can be seen from your code below, even the members of union like h and the members of h are also present, which means the union is there in some header file and you are including it.
void goto_xy(int x,int y)
{
union REGS r;
r.h.ah=2; //Here you are accessing the member of REGS and even the sub-members of h
r.h.dl=y;
r.h.dh=x;
r.h.bh=0;
int86(0x10,&r,&r);
}
EDIT:
A Google search tells me that UNION REGS will be defined in dos.h and it is some like
union REGS {
struct WORDREGS x;
struct BYTEREGS h;
};
So, you need to include dos.h to solve your problem. But, it appears inspite of you including that, this problem is present. You can as well open dos.h and check if union REGS is present or not.
See here for more details.
To define a union, you need to do the following:
union REGS { some_type h; other_type f; };
Now you can create a variable of type REGS and use the union.