C :: Detect CPU Usage And Free RAM On Windows X86 In C?
Feb 20, 2015
I don't use Visual Studio and/or C++ so I would like to do it without them. I found this article c++ - How to determine CPU and memory consumption from inside a process? - Stack Overflow
I tried some lines of code but no one works for me.
I was exploring static variable by writing code snippets. I tried below code and it ended up throwing error saying "error: storage class specified for parameter 'b'"
Why static cannot be used in func() ?
Code: int main() { int a; a=5; func(a); printf("%d",a); return 0; }
Now i am utilizing vectors in my codes.If i want to make a vector and i use #define to put a limit on the amount of variables it can contain. Until now i can do that. But let's suppose that i put 40 variables as its max capacity, but an user just needs to use only 12 or 14 variables. Put a rule that if someone has finished inserting his data and he is yet to fill the whole capacity of the vector the code.As to say : "If case you want to end press 0" .Edited : My computer has windows 7 and i took Codeblocks as my editor.
I want to make an application that will check CPU/RAM USAGE and CPU TEMPERATURE and if it meets some requirements it will restart your system or do something else..but my problem is:
1) How do i get that info? (i know that in VB.net there is an easy way) but in C/C++ or Java?
2) Is it possible though a C program to execute a command and parse that info to your program?
I am mainly interested for windows but i need also info for linux too..
I am working on a parallel processing server/client app with a multi-threaded server and several data processing clients, each their own process. I am trying to optimize some of the parameters, such as the size of the chunks that are read, processed, and output, and also some of the timeout values and such. I can track the time to finish a given task well enough, but it would be really nice to be able to track the cpu use. When CPU use is near 100% (on all cores) for the entire run, that is a good sign. I have noticed that with some combinations of parameters, CPU drops quite a bit for long stretches, which is not such a good sign. This app process large input files (2.5GB-65GB so far) and needs to be stable for long periods of time.
Other than sitting and staring at the task manager all day, is there a good way to track/log the CPU usage over runs that take many hours? I know that there are some apps like Everest that chart CPU use, but it would be nice to have something that would write to the same log file I am already using for other program output.
How can I check how much memory my program is using? I have an assignment which asks me to make sure I'm not exceeding some amount of memory but I don't know how I'd do that on Windows 7.
Also, my program parses through a text file and puts each word in a vector so that I can later go through the vector and "locate" where the nth occurrence of a word is by keeping track of a counter and incrementing the counter each time I see the word in the vector. Is this a vector a bad data structure for this? I'm aiming at keeping the efficiency at less than O(n^2).
I am using a thread in my application .. A DLL is written for In and Out instructions for hardware ICS and to read FIFO.
My code is
CCriticalSection crdll , crsec ; UINT ThreadReceiveData(LPVOID param) { for ( ; ; ) { if (bTerminate) break; // bTerminate = 1 in Doc template destructor crdll.Lock();
[Code] ....
I am confused , how and when I should use Ctitical Section ? The program works fine but I am not happy as this is main routine of the program and I have not understood it properly.
Let say h(0)=01 and h(1)=10 and let say our first string is w=0 h(w)=01, h^2(w)=0110 h^3(w)=01101001..etc.
We have used h map, which is given in the program comment statement. Same thing, program is doing for three letters. These three letters we have passed as argument in h_i function. e.g. h_i(2,w,"a","d","g") function will apply the 3 letter map h(definition is given in commented form in program) 2 times on string w.
In program w=a; and three letters are a, d and g. /* h(a)=abcab,h(b)=acabcb,h(c)=acbcacb */ here b=d and c=g.
Above program gives core dump at free(w2) free(w1). if we remove these free statements then it gives correct answer. But, we want to free w1 and w2 in the above code. How can we make free both w1 and w2 strings?
how is the best way to free unsigned pointer array allocated with cmallc?
Code:
uint8_t *buf; buf = cs_calloc(ca->len + 13);
i do like this , but i know this is not quite right , since i got compile warning passing argument 1 of ‘free’ makes pointer from integer without a cast
Code:
for (i = 0; i < ca->len + 13; i++) { free(buf[i]); buf[i] = NULL; }
free(buf); do i need to free each element of array like above?
I'm trying to free allocated memory for structure. It seems like free() gets only pointer and not regular types . my question is basic and simple – is passing pointer to free() frees the pointer or the variable it points at? or both?
I'm currently trying to write a program that takes an integer input from the user and displays that number of square-free numbers. I've gotten most of the program to work, but I encounter an error during the noSquare function that causes it to print incorrectly.
A square-free number is a number that is not evenly divisible by the square of any prime number. It's hard to see the bold in the code area, but it begins after the "//I think below is where the problem is"
#include <iostream> #include <vector> using namespace std;
I'm currently trying to write a program that takes an integer input from the user and displays that number of square-free numbers. I've gotten most of the program to work, but I encounter an error during the noSquare function that causes it to print incorrectly. I have bolded the area where I believe the problem is what I am doing wrong.
The problem with the program is that it prints ALL numbers from 1 to the input number instead of just the square-free.
A square-free number is a number that is not evenly divisible by the square of any prime number ....
#include <iostream> #include <vector> using namespace std; bool noSquare (int num); //Function to determine if the number is square-free vector<int> getPrimes(int num); //Function to get a vector of all primes below num bool isPrime (int number); //Function to determine if individual numbers are prime int main()
[Code]...
By the way, the in the middle of the code was me trying to bold a section, not an actual part of the code. I can't seem to find where to edit my original post.
Full disclosure: this is an exercise from "Sams Teach Yourself C++ in 24 Hours" by Jesse Liberty and Rogers Candenhead. This refers to Chapter 9 (Hour 9 Activity 1)
I created a class called Point, in Point.h
I created a class called Rectangle in Rectangle.h and Rectangle.cpp
If I create an int main() function in Rectangle.cpp (that includes Rectangle.h), I can compile Rectangle.cpp and run the resulting program. Fine.
Question:
I create a separate file called main.cpp. I include Rectangle.h. But now the compiler complains.
Code: $ g++ main.cpp -o main /tmp/cc38JIph.o: In function `main': main.cpp:(.text+0x26): undefined reference to `Rectangle::Rectangle(int, int, int, int)' main.cpp:(.text+0x32): undefined reference to `Rectangle::getArea() const' collect2: ld returned 1 exit status If I can create a class in Point.h and use it in Rectangle.h, why can I not just use Rectangle in main.cpp?
And the files, of course:
file: main.cpp Code: #include <iostream> #include "Rectangle.h" using std::cout; using std::endl;
I am doing a project for a class which involves making a game where there is an arrray that holds a player (you) and a zombie (comp). The player is suppose to be able to turn clockwise or counter clockwise, move forward, and backward and shoot. Im having trouble trying to rotate the char in an array. I am also trying to switch from using the W,S,D,A to the arrow keys but doesnt seem to work.
#include <iostream> #include <windows.h> #include "color.h" using namespace std; using namespace Petter; const int COL = 15; void initBoard(char[][COL], int, int);
I basically have a listbox that has postcode areas i.e : AE,CW,GU etc etc.
The user selects this and then a postback occurs - an sql statement is builts and a database query operation is performed and the results are returned to a datatable called tempdata.
So far so good. I then need to loop through this datatable and copy the records to my main viewstate datatable which is the datasource for google maps api.
DataTable tempstore = GetData(querystring, ""); //check tempstore has rows otherwise add defaultcust as default otherwise map will be blank if (tempstore.Rows.Count == 0) {
[Code].....
So my main datatable can grow and grow as users add more postcodes. However when it gets to around 500 rows or so I get a huge memory spike only on postback and then it settles back down.
My ram usage goes from 2gb to 3gb and if even more postcodes is selected it maxes the memory and crashes my pc.
If I remove the:
dtpc.Importrow(row);
the memory spike goes completely, obviously because the main datatable has no rows. I thought you only run into memory issues when you have thousands of rows?
I am working on a project were I have to read line form ( PLC ) programmable logic controller generated text file with lines like this
Circuit Value Current 2.33 4.32 5.55 there could be up to 3000 lines per txt file
I am using string stream to parse the line, for the sake of good programming I which to check weather first three values are string and last three values are actually floats raise or throw an exception if they are not ....
I'm struggling with the whole stack/heap thing. I totally see the need to create objects on the free store to outlive functions, however, I don't see the purpose of "new" when working with classes.
Code: class Person{ public: Person(std::string strName, int number); ~Person(void);
Resource r;
[Code] .....
Now, what is the difference? Why would I need to create a Resource on the free store? Both variables named r live until the destructor runs, right?
Code: [harshvardhan@hari-rudra] ~/Desktop% gcc49 -o test test.c [harshvardhan@hari-rudra] ~/Desktop% ./test -before Value of len = 1 (in_function)-before Value of len = 1 (in_function)-after Value of len = 1
-after Value of len = 1 I was trying to make a little easier to work with string. Once the memory is allocated by malloc via sb_init() function, the sb_massacre function wasn't working to deallocate the memory. I had used multiple versions of gcc and clang but the result is same.
This directory "H:C" does exist, if I comment out the GetDiskFreeSpaceExA line, the program doesn't crash, but it leads to some peculiar results (some uninitialized and random value, but at least it doesn't crash)
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { unsigned long long int address; float current; unsigned char pressure_units; }
[code]....
But I don't like how I had to use malloc and free. Is there a different way to accomplish copying the string into a char pointer without resorting to dynamic memory allocation?