Getting sizeof a dynamically allocated array?

Posted on 2003-03-03
Medium Priority
Last Modified: 2010-04-01
    I've got something like this:

int x = 0;
char* Array = NULL;
cin >> x;
Array = new char[x];
cout << sizeof(Array);

Why is it that sizeof(Array) always outputs 4??  If I change the type of array to int, it still gives me 4.  Is there any way to use the sizeof function to act normally? Or maybe there's another function...?

Thanks in advanced,
Question by:Ungenious
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 8062748
Array is a pointer, I guess you are using system with 32-bit pointers.

sizeof interprets argument as an array if it IS an array.
LVL 48

Accepted Solution

AlexFM earned 300 total points
ID: 8062754
sizeof operator is done in compilation time. The only thing compiler knows about Array is that this is pointer char*, which has 32 bits size. To get size of dynamically allocated array write:

cout << sizeof(char)*n;
LVL 48

Expert Comment

ID: 8062765
If you declare array by such way:

char Array[10];

siseof(Array) is 10 (compiler knows it in compilation time). sizeof any pointer is always equals to pointer size in operating system (32 for Win32).

Author Comment

ID: 8062782
okay duh, it's late at night sorry... since i was using char data-type I was used to each frame of an array = 1 therefore I thought sizeof gave the number of frames in the array.  and yeah I just read that part about dynamically allocated arrays to return the # of bytes to store one address.  So, I guess as Alex said, sizeof is done in compilation time & I can't get the sizeof to act "normally". & I have to just use the variable.  Thanks Alex. I need sleep.

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question