|
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.
|
|
|
|
|
|
|
Try
#include <stdio.h>
#include <string.h>
#define MAX 10
int srch(const char *a[],char *b)
{
int i = 0;
while ( a[i] != NULL)
{
if ( ! strcmp( b, a[i]) ) return i;
++i;
}
return -1;
}
int main()
{
int index;
const char *a[]={"abc","xyz","lmn", NULL};
char b[MAX];
printf("\nplease, enter string to find\n");
if ( fgets(b, MAX, stdin) )
{
for (index = 0; index < MAX; ++index)
if ( b[index] == '\n')
{
b[index] = '\0';
break;
}
printf("string to find '%s'\n",b);
index = srch( a, b);
if ( index != -1 )
printf("string found at index = %d\n", index);
else
printf("string not found\n");
}
return 0;
}
Please note, fgets includes the newline in the string, that's the reason for the clumsy code used to remove it.
|
|
|
|
|
I am typing the text and changing the color using SetSel() and SetSelectionFormat().On clicking ctrl+z single time I need to delete that text.I dont want to remember SetSel() and SetSelectionFormat() operations for undo.
When I do ctrl+z second time, I need to do redo operation?
How to achieve this?
Or How to achieve multilevel undo/redo operations for CRichEditCtrl?
|
|
|
|
|
|
|
char *pinNumber = this->gpionum;
cout << " pinNumber " << pinNumber << endl;
This is what I need - insert char * into char array
char constant + char * + char constant
I CANNOT USE STRING
char *setdir_str ="/sys/class/gpio/gpio"; // + "pinNumber" + "/direction";
the final setdir_str = "/sys/class/gpio/gpioX/direction"
I know how the size the setdir_str and can allocate memory for that.
I have no idea how to concatenate all individual parts of the array WITHOUT using string.
Just char !
Any constructive help would be appreciated,
just remember
NO STRING!
|
|
|
|
|
|
You have to either use std:string or dynamically allocate/reallocate character arrays to add the needed substrings into.
|
|
|
|
|
Just use the standard c string functions like strcpy, strcat or even sprintf to do what you want
Technically you are trying to concat c strings but also looks like you want a int to ascii conversion.
Just messing around here to show sorts of things you can do.
#include <stdio.h>
int gpionum = 1;
char buf[256] = { 0 };
sprintf_s(&buf[0], _countof(buf), "/sys/class/gpio/gpio%d/direction", gpionum);
size_t ss = strlen(&buf[0]) + 1;
char* setdir_str = (char*)malloc(ss);
strcpy_s(setdir_str, ss, &buf[0]);
setdir_str = "/sys/class/gpio/gpio1/direction" in above example
It's all the normal string functions that existed before the string object in c++ existed.
There is also the unicode equivalent of each function if you need them in <tchar.h>
In vino veritas
modified 16-Dec-17 11:18am.
|
|
|
|
|
Thanks Leon.
Exactly what I need.
Simple and clean...
char setdir_str[256];
int n = sprintf(setdir_str, "/sys/class/gpio/gpio%s/direction", this->gpionum);
#ifdef CCC_DEBUG
printf("[%s] is a string %d chars long\n", setdir_str, n);
cout << "direction string " << setdir_str << endl;
#endif
What is interesting the parameter type passed to sprintf has to be %s "string" - not %c.
I sure get mixed up with all these characters / string and string class.
Thanks again
-- modified 16-Dec-17 17:50pm.
|
|
|
|
|
Vaclav_ wrote: What is interesting the parameter type passed to sprintf has to be %s "string" - not %c.
%c is used for a single character.
%s - for the char sequence.
|
|
|
|