C++ :: Changing Global Variable In A Thread

Jul 28, 2013

I have a small problem with my program. It is kinda a mess but I will try to explain you what I am trying to do. I have some threads. One of it, it attempts to detect a game client. So my code is sort of like that:

DWORD ProcessID; // The process ID of the game client
void test() {
char* text;

[Code]....

So basically, its like the variable changes, but only inside the thread... why does that happen?

View 15 Replies


ADVERTISEMENT

C :: Transform Local Variable Into Global Variable?

Oct 25, 2014

I need to transform a local variable into a global variable so I can use it in one of my functions. I thought about passing the value as a parameter to this function but I can do this since the function is called inside the while loop and this variable counts how many times the while loop does (so the final value is outside the loop). Example to visualize better:

Code:
while(condition) {
function(parameter1, parameter2);
count = count + 1;
}
printf("%d
", count);

So, I need to transform the final value of "count" into a global variable. Can I do this?

View 5 Replies View Related

C++ :: Global Variable - Calling Sub Function

Dec 19, 2013

Expected output: 20

But what I got is: 22

Why. While calling sub function it should take the global variable am I right

insert Code:
#include <iostream>
using namespace std;
int a=0;
void sub()

[Code] ....

View 3 Replies View Related

C :: Global Variable - Only Printf Works

Apr 20, 2013

Okay so I am programming an 8051 variant to interact with an SD card. In a separate initialization routine in SD_CARD.c I pull out the vital information of the card into global variables. I then call Menu() which is in another source file but includes a header file with all of the variables declared as extern. Now here is the weird, part this is from my Menu.c

printf("%u" , VOLUME_1_SECTOR);
if(VOLUME_1_SECTOR==16384)
printf("Correct");
else
printf("Incorrect");

Now the output of the first printf is 16384 but the conditional evaluates to false. If I put this code in SD_CARD.c (Where VOLUME_1_SECTOR is defined) the conditional evaluates to true. I am confused why the printf works correctly in Menu.c but not the conditional.

View 2 Replies View Related

C :: How To Scanf Global Variable Outside The Main

Sep 7, 2013

The problem that I want to make an array " vertextDegree [nbColours] " with " nbColours " elements in it ,but the "nbColours" unknown and I have to get it get it from a file .

Code:

int nbEdges,nbVetices, nbColours ;
typedef struct st_graphVertex {
int index;
int colour;
int val ;
int vertexDegree[nbColours]; // it won't work because nbColours unknown
// here and I want get it from file in the main
struct st_graphVertex *next;
t_edgeList *out;
}t_grapheVertex;

View 3 Replies View Related

C++ :: Private Global Variable To Class?

Sep 30, 2014

I want to make a destructor counter...So it needs to be outside that specific instance of the class. And i want to keep it hidden. I don't want to be able to access it from outside the class...I Don't want main.cpp to have access to this variable

Is there a way to do this?

static int destructorCount;

View 8 Replies View Related

C/C++ :: How To Declare Global Variable In Structure

Apr 28, 2015

I am getting this error while trying to compile my program:

It says that my variables "nome, cognome, eta..etc" are being used for the first time in my "inserisci" function.

I tought that I could just declare them as global in my structure like I did in my code, but apparently this doesn't work.

Do I really need to declare them again outside of my structure? Isn't there another way?

Here is my code:

#include <stdio.h>
#include "readline.h"
void inserisci(void);
struct impiegato{
char nome[20];
char cognome[20];
int eta;

[code].....

View 7 Replies View Related

C/C++ :: Global Variable Extern Typedef Int?

Oct 8, 2014

I have a .c file for each function of my program and i have .h,so how can I extern the following

in my main...

typedef int table[100][100];

how can I extern them in the .h and c files?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#define HEIGHT 100
#define WIDTH 100

[code]....

View 3 Replies View Related

C++ :: Creating Global Variable / Pointers?

Jan 6, 2012

I am making two classes using the juce library [URL] ....

this is a problem that has now come up after solving an earlier problem i posted but its a completely seperate issue.

i have a class for loading a file when i button is clicked that seems to work. Here is the function in my LoadButton cpp:

void LoadButton::buttonClicked(Button* button)
{
FileChooser pedoFileChooser ("Choose an Audio File", File::nonexistent, String::empty, true);
if (pedoFileChooser.browseForFileToOpen())
{
File pedoFile (pedoFileChooser.getResult());
}
}

I want to be able to then use the file stored in pedoFile in the cpp of another class called PlayButton. I tried doing this with a pointer? not sure if that's correct way of doing it (i know very little about C++ or programming) by changing the function to this. I'm getting the error invalid initialisation of non-const reference of type 'juce::File*&' from a temporary of type 'juce::File'

void LoadButton::buttonClicked(Button* button)
{
FileChooser pedoFileChooser ("Choose an Audio File", File::nonexistent, String::empty, true);
if (pedoFileChooser.browseForFileToOpen())
{
File* &pedoFile = (pedoFileChooser.getResult());
}
}

View 3 Replies View Related

Visual C++ :: Global Variable Scope

Jun 15, 2014

//I dont understand this why does "<< "
The value of global now is: " << global << "
";" is equals to nine

#include <iostream>
int subtract (int a, int b);
int global = 5;
int main(void) {
using std::cout;
int a, b;

[Code] ....

View 1 Replies View Related

C++ :: Access Variable From Other Scope Without Global Declaration?

Aug 8, 2013

Code:
#include <iostream>
using namespace std;
void f() {
int x=17;
//cout<<main::y<<endl; i want to access y from main scope
}

int main() {
int y=23;
//cout<<f::x<<endl;

I want to access x from f scope is there any way for this without global declaration? specially about function scopes...

View 1 Replies View Related

C :: User Input Variable To Global Array

Oct 11, 2013

I am trying to create a global array with user-defined dimensions.the code is:

Code:

int matr_size()
{
int x = 0;
printf("Please enter the number of nodes: ");
scanf( "%d", &x);
printf("There are %d nodes in this simulation.", x);
getchar();
return x;
}

[code]....

I read that an array cannot be defined by a variable in C so I assume that is the issue, but I'm not sure how else to do it. Previously the size was defined by #define NODES and it worked fine but I need this user input.

View 5 Replies View Related

C/C++ :: Necessity Of Global Variable For Recursive Function

Nov 30, 2014

If I define the variable 'total', everything works well without any problem. But if I define it inside the function 'sum', I get irrelevant results, because each time the function gets executed, the variable total gets defined again, losing its value already assigned to it.

Do I have to use a global variable? Is there any way that I can do with without using a global variable?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int total;
int sum(int a, int B)/>{
if (a < B)/>{
//printf("The total is %d, a is %d, and b is %d", total, a ,B)/>;
total += a;

[Code] ....

View 4 Replies View Related

C/C++ :: Use Global Variable Stored In File In 2 Different Functions?

Mar 13, 2014

Ok so when the program runs the first function the data is stored and displayed in the file. The second function is supposed to read the name entered, compare it to the ones in the file then take the price with it BUT I seem to have done something wrong when reading the files (or maybe it has to do with the global function I'm not sure). Here's parts of the code :

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

[Code]....

View 7 Replies View Related

C/C++ :: Create Local Instances Of A Global Variable?

Oct 4, 2012

is it possible to have a global variable pointing to a different address depending on the thread?

Imagine one would like to use threads with the loop:

for (i=0;i<n;i++){
globalPointerVariable=getAddress(i);
DoThingsUsingThe_globalPointerVariable();
}

View 4 Replies View Related

C++ :: Defining Global Variable - Linker Error

Jun 18, 2012

If I DEFINE a global variable in two source files which belong to the same project, then there is a linker error "multiply defined symbols". But if I DEFINE a global variable in two source files which belong to the different projects, then it compiles fine. Why?

View 8 Replies View Related

C++ :: Any Variable Type That Can Change Its Value In Global Scope

Nov 17, 2013

is there any variable type(or with another keyword) that we can change it's value in global scope?

View 5 Replies View Related

C++ :: Difference Between Static Local Variable And Static Global Variable?

Aug 5, 2013

Here is the code,

Code:
class A {
};
A& CreateObject() {
static A a;
return a;
} static A aa;
int main() {
return 0;
}

So is there any difference between a defined in CreateObject and aa?

View 6 Replies View Related

Visual C++ :: Local Variable Be Passed As Parameter Into A New Thread?

Apr 1, 2013

Can local variable be passed as the parameter for a new created thread procedure? Here is the example code:

Code:
void CDLG::some_function()
{
CString strFileName="abc.doc";
//local variable, can it be valid for being passed into the following new thread???
//Can strFileName still be accessed from within the stack of thread procedure?
::AfxBeginThread(ProcessContentThread,(LPVOID)&strFileName);
}

[Code]...

There is another method using variable on the heap,

Code:

void CDLG::some_function()
{
CString strFileName="abc.doc";
CString* pstrFN=new CString(strFileName);
::AfxBeginThread(ProcessContentThread,(LPVOID)pstrFN);
}

[Code]...

I test these code, both methods work as expected, but I doubt whether the first method is a good way. OR if only the second method is the correct way to pass a parameter to a thread.

View 12 Replies View Related

C :: Save Data From Global Variable Into Linked List

Jul 6, 2013

I'm trying to run so called student-administration program.I got functions that can read and write student data, but the one saving the data from about 30 students has some problem that I can't figure. (warning: I'm quite new to C programming)so this is the code:..I guess I can't use global variables as function arguments?

Code:

//global variables
static char ReturnName[31];
static char ReturnFirstName[31];
static float ReturnAverage;
}

[code]....

incompatible types when assigning to type 'char[31]' from type 'int'

View 2 Replies View Related

C :: String Declaration As Global / Main Local Variable

Nov 14, 2013

When a declare a string e.g.

Code:
char str[30]; as a global variable, the srting is initialized to NULL.

But if I declare char str1[30] INSIDE main(), then the string has garbage inside.... Why this happens??

E.g. the following code snippet...

Code:
#include <stdio.h>
char str[50];
int main(){
char str1[50];

[Code] ....

View 4 Replies View Related

C++ :: Changing Variable In For Loop

Apr 28, 2013

I have a problem with my code which I can't work out:

double Mi = 200*pow(10,30);
cout << "
Enter accreted mass increment in solar masses
";
cin >> dm;
cout << "

[Code] ....

Basically the loop works, but gives the wrong results.

I need, at the end of the loop, to sort of "redefine" Mi as "Mi + Macc". I then need it to repeat the loop, and at the end add another Macc so that Mi becomes "Mi + Macc + Macc", etc.

View 5 Replies View Related

C++ :: Holding Function In Variable And Changing It Dynamically

Jun 30, 2014

I need a variable that will just hold a function that I can change in the middle of the application, even to different function type with different amount of parameters ... is this even possible? At the moment I have this

struct REPLY {
string *sReply;
function<int()> special = [](){return 0;};
REPLY()

[Code] .....

And then change it back to blank function returning 0 after calling it

(reply.special)();
reply.special = [](){return 0;};

This works fine (there is no reason why it should not, right? Now, if I want to have another function, let's say

void test2(string str) {
MessageBox(NULL, str.c_str(), NULL, NULL);
}

How do I point this one to the variable special, when I want to call it like that (or something similar)

(reply.special)("test string");

is this even possible? if so, how? i tried to create function pointer (didnt compile at all) or use template (neither did this) and how to do this as I discovered functional lib just a while ago.

View 12 Replies View Related

C# :: One Thread Trying To Pass Data To Another Thread Using Serial Port

Jul 30, 2014

I have a class which I wrote and one of its object is "SerialPort" .NET class. In my MainWindow I created instance of my class called "SerialPortComm", then I send through some functions of mine, commands to the Serial Port, and I receive answers through "DataReceived" event.

But when I trying to use Dispatcher.BeginInvoke to write my data I have received (successfully), nothing shows on the RichTextBox which I'm trying to write to.

What can caused that, and How I can make it works?

SerialPortComm.cs

public partial class SerialPortComm : UserControl {
public SerialPort mySerialPort = new SerialPort();
public void Open_Port(string comNumber, int baudRate) {
mySerialPort.PortName = comNumber;
mySerialPort.BaudRate = baudRate;

[Code] ....

View 7 Replies View Related

C++ :: Access Objects From Another Thread

Jun 25, 2013

I have the following code below. I am getting a memory access violation when accessing cmd->query_string in the loop function. The loop() function is running in another thread. The cmd object appears to be destroyed after calling the send_command function. How do I create an object on the heap and access the query_string.

std::list<my_command_message_que*> my_command_que;
void loop(){
if(my_command_que.size() > 0){
my_command_message_que * cmd = my_command_que.front();
std::cout << cmd->query_string;

[Code] ....

View 2 Replies View Related

C++ :: Passing Parameters To A Thread

May 9, 2013

I've been using threading for a while, that was no needing parameters. so I used for example the following line :

_beginthread(functionName, 0, (void*)10);

and it worked great...

but now I have a function that has an int parameter (int num), and I dont know how to transfer it using _beginthread...

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved