size_t

Class A{
int p [100];
};

What is size_t A?

Is this true that every type actually has a constant "size"
size_t is a type itself (e.g. sizeof resolves to this).

Is this true that every type actually has a constant "size"
Yes
So size_t A will be pretty small. A has like a pointer, and that's it. right?
Where do p[100] is located then when we do


A someA=new A();

Heap?
So size_t A will be pretty small. A has like a pointer, and that's it. right?
if you write size_t A then 'A' has nothing to do with the aforementioned class. 'A' has the type size_t (usually unsigned long). No pointer at all.

Where do p[100] is located then when we do


A someA=new A();

Heap?
'p' is part of 'class A' so yes: heap.
Where do p[100] is located then when we do


A someA=new A();

Heap?

Ah wrong question.

Where do P[100] is located when we do.

A someA;

I forget that in C++ new returns a pointer. He he he he.... That means my previous code is invalid.
Also what would be sizeof (A)? It should be pretty small isn't it because p is just a pointer.
I was under the impression that p would be allocated where-ever A was allocated?

So,
1
2
A someA1; // someA1 and associated p are allocated onto the stack
A *someA2 = new A(); // someA1 and associated p are allocated onto the heap 


However, if A was defined to create p using new, then p would always be put on the heap.

1
2
3
4
5
6
7
8
9
10
11
Class A{
int p*;
public A(void)
{
    p = new int[100];
}
public ~A(void)
{
    delete[] p;
}
};


If I'm wrong, someone please correct me :P
teguh123 wrote:
Where do P[100] is located when we do.

A someA;
stack. All members of a class/struct will be located where the enveloping class/struct is located.

helloworld922 is right

teguh123 wrote:
Also what would be sizeof (A)? It should be pretty small isn't it because p is just a pointer.
p is not a pointer (can be used as such) in your case it's an array of int. So sizeof(A) will be 100 * sizeof(int) + may be some overhead.
Topic archived. No new replies allowed.