2

I am wondered about the memory usage of variables and I tried this :

#include <iostream>

int main()
{
    char* testChar1 = "Hi";
    char* testChar2 = "This is a test variable";
    char* testChar3 = "";

    std::cout <<sizeof(testChar1)<<std::endl; 
    std::cout <<sizeof (testChar2) <<std::endl; 
    std::cout <<sizeof(testChar3)<<std::endl;
}

output is :

4
4
4

I think I am not doing the right thing. I want to know how much memory every variable uses in stack .

EDIT 1

At the same time if I does char* testChar3 = NULL; the program crashes. So does it mean there is no memory usage for the same?

  • possible duplicate of [How to find the sizeof(a pointer pointing to an array)](http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array) or perhaps [Pointer array and sizeof confusion](http://stackoverflow.com/questions/3397098/pointer-array-and-sizeof-confusion) – Suma Feb 13 '13 at 09:16
  • Setting `char* testChar3 = NULL` and then "reading"/accessing it later while it is still `null` will result in a null pointer exception - which is why your program crashes. – Dacto Feb 13 '13 at 09:37
  • ok, so is this variable will be gone out from stack when set to NULL? –  Feb 13 '13 at 09:38
  • No, well, kinda. What you have there is a `char` pointer. If you set the `char*` (`char` pointer) to null then you have still have a `char*`; it just does not point to anything. – Dacto Feb 13 '13 at 09:40
  • Also, the reason you are seeing `4` when you are printing out the size of the pointers is because a pointer consists of 32-bits (4 bytes) for 32-bit executables. – Dacto Feb 13 '13 at 09:49
  • Where exactly your programm crash? – qPCR4vir Feb 13 '13 at 09:58

6 Answers6

2

You simply print the size of the pointers, they always will be the same. What you need is to multiply strlen for the strings by the size of a single char.

EDIT: as per my comment and the correction from @Suma:

cout << (strlen(testChar) + 1) * sizeof(char) + sizeof(testChar);

The 1 is needed for the terminating zero character.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
2

In addition to using strlen, you could also use sizeof on a

char testChar1[] = "Hi";

EDIT: yes, this includes the null terminator, which IMO is an advantage over strlen. The actual size does include the null terminator.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

sizeof(testChar1) return size of pointer, if you want to test string length try replace sizeof with strlen

billz
  • 44,644
  • 9
  • 83
  • 100
0

In this instance you're only printing the size of the pointers and not the chars. So really, you would want to print the pointer, then dereference it and print the size of the memory it points to as well.

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
0

You are actually printing the amonut of bytes a pointer takes on your system . I think what you need to do is to use strlen function . Check it out here . strlen

std::cout<<strlen(testChar1)<<std::endl;
std::cout <<strlen(testChar2) <<std::endl; 
std::cout <<strlen(testChar3)<<std::endl;
rockstar
  • 3,512
  • 6
  • 40
  • 63
0
I want to know how much memory every variable uses in stack .

What your programm print is exactly what you want.

Read the other answer if what you really want is to know how much memory (where??!!) occupy the char-strings pointed by yours variables - pointers.

qPCR4vir
  • 3,521
  • 1
  • 22
  • 32