Could someone test me?

I want to verify my knowledge of pointers, memory, dynamic allocation, etc.

Could some people ask me some questions to answer? Thanks.
True or false: pointers can be used to refer to stack variables.

What is the heap? How does one allocate memory from it?

What is the stack? How does one allocate memory from it?

What is/are the difference(s) between malloc and new? Between free and delete?

What is array new and array delete? If array new is used, why must array delete
be used on the pointer?

Consider the following code:
1
2
3
4
int* pInt = new int;
*pInt = 4;
delete pInt;
*pInt = 42;


True or false: In a single threaded application, the last line will cause the program to crash.

Wow thanks for the quick reply :)

1. The heap is used to allocate dynamic memory. You allocate memory from it using malloc(), realloc(), calloc() (etc.) (C) or new (C++).
I think it also gets memory from the OS when it can't find a big enough block or doesn't have enough memory left to it.

2. The stack allows functions to return to the point of execution after they were called; e.g.
1
2
3
4
int main() {
    myFunction(); // After myFunction returns, it is popped off of the stack and execution begins at the next instruction
    return 0;
}
*

3. malloc() returns a void pointer to a block of memory (if a big enough block is found), new returns the reference of a block of memory (possibly a pointer to the first byte(s) (depending on type, i.e. sizeof(int) on an x86 should be 4)).

free() takes a void* parameter (pointer to a block of memory) and deallocates it (returns it to the heap). delete deallocates the memory of a variable.

Additionally malloc and free are functions, new and delete are operators.

4. I would guess because an array (without a subscript) is a pointer to the first element of the array (i.e. the [0] element), so if you use ordinary delete on it, you would only delete the memory at array[0] rather than all of array.

5. I'm not sure. Logically I would say true, because pInt doesn't point to anything anymore. However, you haven't deleted the address... I'm going to go for true...

Edit:
* the stack also allocates static variables.
Last edited on
I think you missed:

True or false: pointers can be used to refer to stack variables.

and
How does one allocate memory from [the stack]?
True or false: pointers can be used to refer to stack variables.

True.

How does one allocate memory from [the stack]?

Static variables.
1. Correct.

2. Correct on the first part. Second part not answered.

3. I'll give you half credit for it. Both new and malloc return pointers. malloc is a function that returns a void*. new is an operator that returns a pointer to the type being created. But you missed one critical difference between the two in C++ land, hence the half credit. new also runs the constructor for the type being allocated. malloc does not. Likewise, delete runs the destructor for the type being deallocated whereas free does not.

4. You are sort of correct. The critical difference is that the compiler needs to know to run the destructor of possibly more than one instance in the array delete case rather than just one destructor.

5. This is a trick question. C++ does not guarantee the semantics, so the answer is maybe. In all reality, due to the way the heap is typically implemented by the compiler, no, it won't crash the program immediately, but it will corrupt the heap.
1. Cool.
2. You must not have seen my edit.
3. Thanks. I didn't know that (obviously).
4. Ok.
5. I thought it was a trick question; but I wasn't sure; I must have changed my mind at least 4 times :P. Maybe that's something they could add in C++0x...

Thanks for the answers, guys :)
Last edited on
Static variables are not allocated on the stack.

Are they not? Where are they located then?

I thought that when you declared, for example, int x = 0;, x was pushed onto the stack and popped when x goes out of scope...
Last edited on
int x; is automatic, not static
static int x; gets constructed only once and gets destructed when the program ends
I didn't mean static as in the keyword static, I meant static as in "non-dynamic" (automatic, as you put it).

I should watch my wording...

void somefunc()
{
int x ; // non-static local variable on stack
static int y; //not on the stack
}

static variables are stored in statically allocated memory (because the address of these variables needs to be known at compile time). They are stored in the program data area (data segment)
Last edited on
Topic archived. No new replies allowed.