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?

6
  • possible duplicate of How to find the sizeof(a pointer pointing to an array) or perhaps Pointer array and sizeof confusion Commented Feb 13, 2013 at 9: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. Commented Feb 13, 2013 at 9:37
  • ok, so is this variable will be gone out from stack when set to NULL? Commented Feb 13, 2013 at 9: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. Commented Feb 13, 2013 at 9: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. Commented Feb 13, 2013 at 9:49

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@ACB Note that this includes the null terminated character.
why not char const wtf[]= "hi"; ?
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.

9 Comments

Is that is same count which the stack or heap uses on execution ?
Plus constant additional memory for each string, in this case for the pointer.
so Am i supposed to do strlen(testChar)*sizeof(testChar)? and the term constant additional memory for each string refers to?
@ACB no. You should do strlen(testChar)*sizeof(char) + sizeof(testChar).
@IvayloStrandjev Do not forget the terminating zero.
|
0

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

Comments

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.

Comments

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;

Comments

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.

Comments