5

I am attempting to create a character array of a fixed size on the stack (it does need to be stack allocated). the problem I am having is I cannot get the stack to allocate more than 8 bytes to the array:

#include <iostream>
using namespace std;

int main(){
        char* str = new char[50];
        cout << sizeof(str) << endl;

        return 0;
}

prints

8

How do I allocate a fixed size array (in this case 50 bytes. but it may be any number) on the stack?

ewok
  • 20,148
  • 51
  • 149
  • 254
  • 2
    `new` does the opposite of allocating on the stack. What you need is a plain, ordinary array. `char str[50];` will do you fine. If you've got C++11, `std::array str;` would be better. – chris Aug 13 '12 at 16:58
  • 2
    I'd check exactly what it was `sizeof()` was measuring~ It is not what you think it is. – TheZ Aug 13 '12 at 16:58
  • Here's some additional information for you. This is a link explaining how sizeOf works: http://msdn.microsoft.com/en-us/library/4s7x1k91(v=vs.71).aspx – Brandon Aug 13 '12 at 17:09

4 Answers4

10
char* str = new char[50];
cout << sizeof(str) << endl;

It prints the size of the pointer, which is 8 on your platform. It is same as these:

cout << sizeof(void*) << endl;
cout << sizeof(char*) << endl;
cout << sizeof(int*) << endl;
cout << sizeof(Xyz*) << endl;  //struct Xyz{};

All of these would print 8 on your platform.

What you need is one of these:

//if you need fixed size char-array; size is known at compile-time.
std::array<char, 50> arr; 

//if you need fixed or variable size char array; size is known at runtime.
std::vector<char> arr(N); 

//if you need string
std::string s;
Nawaz
  • 353,942
  • 115
  • 666
  • 851
8

Actually, you are allocating 50 * sizeof(char), or 50 bytes. sizeof(str) is sizeof(char*) which is the size of a pointer.

Vlad Ciobanu
  • 1,473
  • 1
  • 11
  • 11
2

To put a C array on the stack, you must write

char myArray[50] = {0};

The C++ way to do this would be - as Nawaz noted

std::array<char, 50> arr;

But i am not sure if the content of this array will reside on the stack.

RED SOFT ADAIR
  • 12,032
  • 10
  • 54
  • 92
  • 1
    `std::array` is simply a wrapper for C-style, fixed length arrays, so it should be on the stack. – chris Aug 13 '12 at 17:03
  • Yes, you are probably right - although i can not find a valid information about this - modified my post. – RED SOFT ADAIR Aug 13 '12 at 17:05
  • The accepted answer [here](http://stackoverflow.com/questions/6632971/what-is-the-difference-between-stdarray-and-stdvector-when-do-you-use-one-o) has the info. – chris Aug 13 '12 at 17:07
1

str here is referencing to an array of characters of size 50*sizeof(char). But sizeof(str) is printing the sizeof of str variable and not the sizeof what it is referencing. str here is just a pointer variable and pointer variable is an unsigned integer whose size is 8.

Also you cannot determine sizeof the reference(In this case, the size of character array). This is because the compiler doesn't know what the pointer is pointing to.

Edit: Getting the sizeof of what the pointer is pointing to doesn't make much sense in C/C++. Suppose a pointer is pointing to a primitive type (like int, long), then you could directly do sizeof(int). And if the pointer is pointing to an array, like in this case, you could directly do arraySize * sizeof(dataTypeOfArray). arraySize is anyways known since C/C++ doesn't allow to define arrays of unknown size, and dataTypeOfArray is anyways known.

Arjun
  • 1,922
  • 1
  • 13
  • 21