|
Dear Vaclav_,
could you edit your post to properly format the code snippet?
Otherwise it is very hard to read/understand.
|
|
|
|
|
it works too..
<pre>#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int log, i, j, k, count1=1, count2=0, c;
printf("Enter the number of logs: \n");
scanf("%d%*c", &log);
char names[log][100];
printf("Enter names: \n");
gets(names[0]);
for(i=1; i<log; i++)
{
gets(names[i]);
if(strcmp(names[i], names[0])!= 0)
{
count2++;
k = i;
for(j=k+1; j<log; j++)
{
gets(names[j]);
while((strcmp(names[j], names[0])!=0 && strcmp(names[j], names[k])!=0) || ((c=names[j][0])==EOF && c=='\n')) {
printf("Invalid entry!\n");
memset(names[j], 0, sizeof(names[j]));
gets(names[j]);
}
if((strcmp(names[j], names[0])==0))
{
count1++;
}
else if((strcmp(names[j], names[k])==0))
{
count2++;
}
}
if(count1>count2)
{
puts(names[0]);
printf("Wins !");
}
else if(count2>count1)
{
puts(names[k]);
printf("Wins !");
}
else if(count1==count2)
{
printf("It's a tie!!");
}
break;
}
count1++;
}
return 0;
}
|
|
|
|
|
I am trying to come up with “calling scheme” to be able to go "from top to bottom".
For example “clear LCD device” via I2C interface and connected to GPIO.
The “clear sequence from “top” is to
send device specific command - “clear”
output binary values of “clear command ” to SDA/SCL interface
output binary values to GPIO
I have a 3 levels basic / working inheritance scheme - with class representing the device , class representing I2C communication format and class representing the GPIO.
I am not sure plain direct inheritance of chain of classes is going to do the job.
I think I need to come up with “nesting” scheme instead of just plain direct inheritance chain using only instance of the “top” class.
Any suggestions will be appreciated.
Something like this, but it looks too scary
http://www.linuxtopia.org/online_books/programming_books/c++_practical_programming/c++_practical_programming_254.html
Cheers
Vaclav
PS Please do not ask for code or "what is it for ? " , this project is under construction and not ready for public scrutiny.
|
|
|
|
|
I would advise you to try some experiments. It can be very simple. Write a simple class hierarchy with derived classes and have them call a virtual method each of them implements. Within the method, have it output a trace statement (TRACE, printf, cout<<, what ever...) You will see the calling sequence by the order of the output. One thing to be careful of is the order of the calls :
virtual void TestMethod( int n )
{
__super::TestMethod( n+1 );
trace( _T( "in TestMethod( %d )\n" ), n );
}
virtual void TestMethod( int n )
{
trace( _T( "in TestMethod( %d )\n" ), n );
__super::TestMethod( n+1 );
}
The output will show you which comes first. Implement as many derivation levels as you want, at least three I think. The base class won't have a __super so you will have to comment that off for it. Anyway, I recommend that you try this experiment. It doesn't take long and it is very informative.
|
|
|
|
|
Below is the simplified version of the code I am using in my project. On running it, I have noticed that it consumes lots of RAM (Memory). I am not getting why is that happening, can anybody guide me in the right direction.
"main.cpp"
#include "ESSolver.h"
using namespace std;
int maxIt;
int minVal;
int maxVal;
int main(){
int function_eval = 1, nVar = 500, mu = 15, lbd = 100, rhoO = 2, rhoS = mu;
maxIt = 1000;
if( function_eval == 1){
minVal = -32; maxVal = 32;
}
ESSolver es(nVar, mu, lbd, rhoO, rhoS);
es.function_eval = function_eval;
es.Init();
es.Optimize();
return 0;
}
"ESSolver.cpp"
#include <iostream>
#include <vector>
#include "ESSolver.h"
#include <random>
using namespace std;
ESSolver::ESSolver(int _nVar, int _mu, int _lbd, int _rhoO, int _rhoS):nVar(_nVar), mu(_mu), lbd(_lbd), rhoO(_rhoO), rhoS(_rhoS)
{
muPop.resize(mu);
muSigma.resize(mu);
muSigma2.resize(mu);
for (int i = 0; i < mu; i++) {
muPop[i].resize(nVar);
muSigma2[i].resize(nVar);
}
lambdaPop.resize(lbd);
lambdaSigma.resize(lbd);
lambdaSigma2.resize(lbd);
for (int i = 0; i < lbd; i++) {
lambdaPop[i].resize(nVar);
lambdaSigma2[i].resize(nVar);
}
mulambdaPop.resize(mu+lbd);
mulambdaSigma.resize(mu+lbd);
mulambdaSigma2.resize(mu+lbd);
for (int i = 0; i < mu+lbd; i++) {
mulambdaPop[i].resize(nVar);
mulambdaSigma2[i].resize(nVar);
}
rhoPop.resize(rhoO);
rhoSigma.resize(rhoS);
rhoSigma2.resize(rhoS);
for (int i = 0; i < rhoO; i++) {
rhoPop[i].resize(nVar);
}
for (int i = 0; i < rhoS; i++) {
rhoSigma2[i].resize(nVar);
}
return;
}
ESSolver::~ESSolver(void) {
return;
}
void ESSolver::Init() {
for (int i = 0; i < mu; i++) {
for (int j = 0; j < nVar; j++) {
muPop[i][j] = rand_r(minVal, maxVal);
}
}
if (mutOp == 0) {
for (int i = 0; i < mu; i++) {
muSigma[i] = rand01_r();
}
}
else {
for (int i = 0; i < mu; i++) {
for (int j = 0; j < nVar; j++) {
muSigma2[i][j] = rand01_r();
}
}
}
}
void ESSolver::Optimize() {
int it = 0;
while (it < maxIt){
it = it + 1;
}
}
int ESSolver::rand_r(int minVal, int maxVal) {
default_random_engine rng( random_device{}() );
uniform_int_distribution<int> dist( minVal, maxVal);
return dist(rng);
}
double ESSolver::rand01_r() {
default_random_engine rng( random_device{}() );
uniform_real_distribution<double> dist(0.0, 1.0);
return dist(rng);
}
"ESSolver.h"
#include <vector>
using namespace std;
extern int maxIt;
extern int minVal;
extern int maxVal;
class ESSolver
{
public:
ESSolver(int, int, int, int, int);
~ESSolver(void);
void Init();
void Optimize();
int rand_r(int, int);
double rand01_r();
private:
int nVar;
int mu;
int lbd;
int rhoO;
int rhoS;
vector<vector<double>> muPop;
vector<double> muSigma;
vector<vector<double>> muSigma2;
vector<vector<double>> lambdaPop;
vector<double> lambdaSigma;
vector<vector<double>> lambdaSigma2;
vector<vector<double>> mulambdaPop;
vector<double> mulambdaSigma;
vector<vector<double>> mulambdaSigma2;
vector<vector<double>> rhoPop;
vector<double> rhoSigma;
vector<vector<double>> rhoSigma2;
};
modified 29-Jan-21 21:01pm.
|
|
|
|
|
seems mostly with vector resize which you are using extensively.
|
|
|
|
|
But they will be executed only once during constructor call
modified 29-Jan-21 21:01pm.
|
|
|
|
|
single run takes more than 1 MB
modified 19-Dec-17 7:01am.
|
|
|
|
|
Actual code has threading. When I ran 10 threads independently, my entire ram of 4GB got full.
modified 29-Jan-21 21:01pm.
|
|
|
|
|
What do you mean by lot of memory?
I think that about 2M are used by the ESSolver instance of your example code.
|
|
|
|
|
I don't know my entire ram got full
modified 29-Jan-21 21:01pm.
|
|
|
|
|
It must be somewhere else in your code because I have compiled the example code and it uses 3355 KiB (waiting for input at end of main and checked the process on another shell in Linux).
|
|
|
|
|
As already noted, I have compiled and run your code. The result is as expected: It consumes less than 4 MiB of memory.
A common case for running out of memory is - besides allocating huge amounts of memory (e.g. many large objects) - assigning newly created instances to pointer member variables without deleting the old instance.
|
|
|
|
|
AtineshS wrote: On running it, I have noticed that it consumes lots of RAM (Memory). How are you verifying this?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
By analyzing system monitor in ubuntu
modified 29-Jan-21 21:01pm.
|
|
|
|
|
If that's anything like Task Manager in Windows, you should not be using it as a "how much RAM is my program using" gauge. Task Manager is showing you the address space that is in use, which has little (to nothing) to do with the amount of that address space your program is actually using. The "memory" numbers are all but completely useless for telling how much of your memory is actually in use. Think of it more as a high-water mark. While you may have made an allocation at one point in time, freeing the blocks does not guarantee the address space numbers will go down. Those blocks are on the free list, still in the address space, but they are available for subsequent allocation.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Thanks Everybody for help issue is resolved. Actually, the problem was in other part of the code where huge amount of memory leaking was happening.
modified 29-Jan-21 21:01pm.
|
|
|
|
|
You are using multi dimensional vectors and loops. That are costly operation. Set you loop params (like 500) to a small number to see the difference.
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
Is there any way to get supported file system for particular OS?
|
|
|
|
|
There are, but the methods are OS dependant.
With Linux for example, just read /proc/filesystems.
Windows has native build-in file systems and support others by drivers.
|
|
|
|
|
Thanks for reply.
Is there any api for Windows OS?
|
|
|
|
|
The four main file systems supported by Windows are NTFS, exFAT, UDF, and FAT32.
Additional file systems can be installed using IFS (Installable File System) drivers.
The API you are looking for is the SetupAPI | Microsoft Docs[^]. It provides functions to enumerate installed drivers of specific types. But I have not used it for file systems so far and can't tell you therefore the GUID(s) to be used (the enumeration is basically identical for all device types).
|
|
|
|
|
I have a program to search and find a given string as below :
#include<stdio.h>
#include<string.h>
#define MAX 10
int srch(char *a[],char *b)
{
int i,r=0;
for(i=0;a[i];i++){
printf("%c\n",a[i]);
if(!strcmp(a[i],b)){
return 0;
}
else{
return -1;
}
}
}
main()
{
char *a[]={"abc","xyz","lmn","NULL"};
char *b;
int l;
printf("\nenter string to find\n");
fgets(b,MAX,stdin);
printf("%c",*b);
l=srch(a,b);
printf("\n%d",l);
}
But I am getting segmentation fault. Why?
|
|
|
|
|
Anonygeeker wrote: But I am getting segmentation fault. Why? Because your original array does not contain a null terminator. You have:
char *a[]={"abc","xyz","lmn","NULL"};
but "NULL" is a quoted string, not a NULL value, so your loop will continue until it causes the fault.
You also have:
printf("%c\n",a[i]);
but the array is an array of pointers, not characters. You need to use the string identifier in your format string like:
printf("%s\n",a[i]);
And finally (although I may have missed other mistakes) you have:
char *b; int l;
printf("\nenter string to find\n");
fgets(b,MAX,stdin); printf("%c",*b);
It should be:
char b[MAX];
int l;
printf("\nenter string to find\n");
fgets(b,MAX,stdin);
printf("%s",b);
I strongly suggest you get a good book on C and learn the basics of single elements, arrays, pointers, printf format strings etc.
|
|
|
|
|
Richard MacCutchan wrote: although I may have missed other mistakes You have missed the logical error here (which should generate a compiler message that not all paths return a value):
int srch(char *a[],char *b)
{
int i,r=0;
for(i=0;a[i];i++){
printf("%c\n",a[i]);
if(!strcmp(a[i],b)){
return 0;
}
else{
return -1;
}
}
} The loop will - if entered - always left after the first iteration.
But it is hard to spot whithout indentation.
|
|
|
|
|