Memory calculation
Hello.
I got a question: if i decleare a variable and i use it, but not to it's full extent , will the variagble still take up the same amoutn of memory? And how do i calcualtion the amount of memory taken up by variable?
sizeof(), tells us the size of a variable but not the memory taken up...
[307 byte] By [
Quell] at [2007-11-18 19:09:58]

# 1 Re: Memory calculation
Originally posted by Quell
sizeof(), tells us the size of a variable but not the memory taken up...
sizeof(var) is the memory it taken up.
AvDav at 2007-11-9 0:32:13 >

# 2 Re: Memory calculation
Yes, sizeof will give you the right amount of memory, and it will do so correctly for various of operating system and machines that you compile your program on.
It will even calclulate, in case of structures and classes, the granulation that will occur which is again machine/operating system dependent.
But There is one thing to say about it: If you have some pointer in your structure/class, than ofcourse that the sizeof will not compute the size of what is pointed to (if that's what you had in mind:-)).
To compute that you will have to do it by yourself.
# 3 Re: Memory calculation
Originally posted by Quell
Hello.
I got a question: if i decleare a variable and i use it, but not to it's full extent , will the variagble still take up the same amoutn of memory? And how do i calcualtion the amount of memory taken up by variable?
sizeof(), tells us the size of a variable but not the memory taken up...
As AvDav already mentioned, sizeof operator will return the number of bytes in the object representation of its operand.
I would like to ask for some more information on the object whose size/memory size you are looking for ? Because i think your sizeof usage over your own variable has yielded the number of bytes that is different from what you expected and this made you ask such questions......hmmm, I am not really sure but if you provide a little more, perhaps we will be able to offer some more help...
Regards,
-Benny
# 4 Re: Memory calculation
in addition to what have been posted,
using STL containers, there are some containers that
support functions as size() and capacity(), for memory allocation size
and memory usage accordingly.
Guysl at 2007-11-9 0:35:19 >

# 5 Re: Memory calculation
If you have a dynamically allocated memory you can get the number of bytes that has been allocated by CRT's _msize(...) (from malloc.h)
AvDav at 2007-11-9 0:36:18 >

# 6 Re: Memory calculation
thx for all great answers, i was fixing my pc for last few days...
here is my problem with sizeof;
string strSentenceArray[1000];
string strSentenceTemp;
cout<<"Please enter a sentence:"<<endl;
getline(cin,strSentenceTemp);
for(unsigned short i=1;i<800;i++){
strSentenceArray[i]=strSentenceTemp;
}
cout<<sizeof(strSentenceArray);
system("pause");
due to the fact that each string is 4 bytes, therefor i get the answer of 4000. But i use only 800/1000 of these....does sizeof give me the memory that is available to me or that is actually used?
Also i got a question about vector..if i get to use sizeof on a vector typde variable like vector<string>sentence;
adn put inot it a 1000 strings inside the vector..i get the result after suoing sizeof of 12...what is this 12?
Quell at 2007-11-9 0:37:17 >

# 7 Re: Memory calculation
Originally posted by Quell
does sizeof give me the memory that is available to me or that is actually used?
that is availabe.
Also i got a question about vector..if i get to use sizeof on a vector typde variable like vector<string>sentence;
adn put inot it a 1000 strings inside the vector..i get the result after suoing sizeof of 12...what is this 12?
it is number of bytes that has object of vector<string>. You shouldn't think that it will return the summary of vector, because internal implementation is based on pointers.. and pointers are mostly 4 bytes.. if you want to know how many bytes the vector<string> uses, then:
vector<string> str_arr;
.
.
int sz=sizeof(str_arr)+sizeof(vector<string>::value_type)*str_arr.size();
AvDav at 2007-11-9 0:38:23 >

# 8 Re: Memory calculation
is there a way to check what memory is actualy used and not available?
thx in advance
Quell at 2007-11-9 0:39:20 >

# 9 Re: Memory calculation
I'm not entirely clear on what exactly you want to know, but I'll answer the two closest questions that I think you mean:
1st question--"How much memory does the filled part of my array of strings use?":
-solution A: keep track of the length the used portion of your array as well as the maximum size of your array, then do the math:
#define MAX_SIZE 1000
int main()
{
string strSentenceArray[MAX_SIZE];
string strSentenceTemp;
int length=800;
cout<<"Please enter a sentence:"<<endl;
getline(cin,strSentenceTemp);
for(unsigned short i=1;i< length;i++){
strSentenceArray[i]=strSentenceTemp;
}
cout<<(length*sizeof(string));
system("pause");
}
-solution B: initiallize the entire array to NULL, populate the array, then count up all the non-NULL entries. Finally do the same length*sizeof(string) calculation.
Question 2: "How much memory do my strings actually use up?"
This is a little bit more complicated, I'll start by explaining how strings are stored (it sounds like you may not know). Warning! This explaination is a little lengthy.
The string type is actually a pointer to a place in memory where the data in a string is located, with the data in the string being a char array terminated by the null character '\0' (a.k.a. 'null terminated string'). So, sizeof(string) returns 4 on most systems (32-bit--4-byte--memory). So, type string is defined something to the effect of:
typedef char *string;
When you declare an array like:
char myString[4];
The compiler effectively does this for you:
char *myString=(char *) malloc(sizeof(char));
So, if you wanted to know the amount a memory the data in a string uses, use the function strlen(). It's defined by string.h (#include <string>) as:
size_t strlen(const char *s);
"size_t" is the same as the return type of sizeof().
Now, how much memory your strings take up depends on your compiler settings. Your compiler can reuse strings that you have already, i.e. if you if you let your compiler reuse strings, something like this happens:
string string1="the";
string string2="the";
cout<<"String pointer1==String pointer 2? "<<(&string1==&string2)"
//outputs "String pointer1==String pointer 2? 1"
If you don't let your compiler re-use strings, the output would be a 0 instead of a 1.
Now to the my answer, which, for simplicity, assumes that you don't let your compiler reuse strings.
First, initiallize all elements in your array to NULL. Then populate your array however you please. This array of string (actually an array of pointers), consumes the whole amount of memory allocated. You get this amount of memory by the already mentioned
sizeof(strSentenceArray)
Now, each string is a pointer to a null terminated array of chars. You get the memory size of each of these by
strlen(myString)
So, you can step through the array of strings and keep adding these amounts to a variable to keep track of the sum. (Careful, I haven't check to see if strlen() returns 0 if the string you give it is NULL, or whether it crashes.) This would be something like this:
int sum=0;
for(int i=0; i<MAX_SIZE; i++)
{
sum+=strlen(strSentenceArray[i]);
}
//finally:
sum+=sizeof(strSentenceArray);
The variable sum would then give you the total amount of memory effectively used by the strings and the by the array of strings.
Now, as a final note, if you let you compiler reuse strings (which can be much more memory efficient) you would first have to determine whether a particular string has already been counted before youo added it to sum. (I don't know how to do that without using a hash table)
# 10 Re: Memory calculation
Originally posted by Quell
due to the fact that each string is 4 bytes, therefor i get the answer of 4000. But i use only 800/1000 of these....does sizeof give me the memory that is available to me or that is actually used?
allocating the variable as you are doing on the stack means it IS being used. you might not have set anything on an item but it's still being "used".
also you need to know that sizeof() is a compile time value not a run time one. it is an operator, not a function. what this means is that if you are using it to tell how long something is that's dynamically created *such as the text in a string class can be* the value you get will almost never be what you expect. your lovely compiler doesnt generate a call to a "sizeof" function but rather it knows to insert some numeric value.
char *a = "blah blah blah";
sizeof(a);
sizeof(a) in this example is always going to be 4 on a 32 bit system. it doesn't matter that the text is longer, that's not what sizeof does. see jefranki's post for more info on that subject.
# 11 Re: Memory calculation
Originally posted by Quell
Also i got a question about vector..if i get to use sizeof on a vector typde variable like vector<string>sentence;
adn put inot it a 1000 strings inside the vector..i get the result after suoing sizeof of 12...what is this 12? The correct function to use is vector::size() to determine the number of strings in the vector.
The sizeof() on a vector won't help you. All it does is return the number of bytes needed to represent the vector object for your compiler. It does not tell you the number of entries in the vector. As others have pointed out, sizeof() is a compile-time operator, it isn't a function that gets called and can magically determine at run-time the amount of memory used.
Regards,
Paul McKenzie
# 12 Re: Memory calculation
hello..i got several questions...
first of all how do i initialize array& vectors to NULL?
second of all what do you mean by letting compiler reuse the strings.?
One more question:
QueryPerformanceFrequency(&liFrequency);
when i output the liFrequency.QuadPArt it gives mes some speed not it mhz, how can i convert it into mhz, or ghz?
thx
Quell at 2007-11-9 0:43:28 >

# 13 Re: Memory calculation
you cant initiate a reference to a NULL.
Guysl at 2007-11-9 0:44:32 >

# 14 Re: Memory calculation
One more question:
QueryPerformanceFrequency(&liFrequency);
when i output the liFrequency.QuadPArt it gives mes some speed not it mhz, how can i convert it into mhz, or ghz?
thx
Quell at 2007-11-9 0:45:35 >

# 15 Re: Memory calculation
Originally posted by Quell
hello..i got several questions...
first of all how do i initialize array& vectors to NULL?
second of all what do you mean by letting compiler reuse the strings.?
Originally posted by Guysl
you cant initiate a reference to a NULL.
To clarify (and revise, too), when I mentioned that you should initialize the entire array to NULL, each element in the array (I meant the conventional type, not the "vector" standard template class, I've actually never bothered to use vector), is still a string but they are 1-char long strings with that single char==NULL (which is \0, variable value==0). So you still need to count the size of these elements (on most systems, 1 byte each).
Secondly, by letting the compiler reuse strings, every time a string (in this case, I mean the array of chars actually stored and allocated, not the C++ class which has extra stuff) is generated it is checked against a list of strings already generated. If this "new" array of chars has already been generated the "old" array is reused simply by copying the pointer of the "old" array to the new one.
This saves memory at the expense of runtime, and was more important on early systems that didn't have much memory to spare, and that also didn't waste precious processor power on "fancy" graphics and impatient people.
You can see how storage of strings in this way would complicate trying to calculate how much memory they consume, especially if your array contains duplicate strings.
To illustrate how this happens, I put together the code below. To see the difference on one compile I enabled "Pool Strings" and disabled "Don't Reuse Strings", while on the second compile I did the opposite. On the first run, the memory address is the same every time. While on the second run, each line within each scope has a different memory address (although repeated addresses with respect to the same line within the for-loop). I used Codewarrior 8, you may have to search your compiler options to find equivilents.
#include <iostream.h>
#include <string.h>
#define MAX_SIZE 30
int main()
{
for(int i=0; i<MAX_SIZE; i++)
{
cout<<&"The quick, brown fox"<<endl;
cout<<&"The quick, brown fox"<<endl;
}
cout<<&"The quick, brown fox"<<endl;
cout<<&"The quick, brown fox"<<endl;
return 0;
}
# 16 Re: Memory calculation
thx i finally got it...
last question...
QueryPerformanceFrequency(&liFrequency);
when i output the liFrequency.QuadPArt it gives mes some speed not it mhz, how can i convert it into mhz, or ghz?
thx
Quell at 2007-11-9 0:47:29 >
