where is struct field that created by new (heap or stack)?

i read about stack and heap but i don't know about this

where is x ( in heap or in stack ) ? is my code have a memory leak or not ?

struct st
{
    int x;
    int* y;
};

st* stp;

void func()
{
    st* s=new st();

    s->x=2;
    s->y=new int(5);

    stp=s;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{

    func();

    cout << stp->x << "  " << *stp->y <<endl;

    delete stp->y;
    delete stp;
}

output 2 5

Answers


stp is dynamically-allocated, and is therefore on the heap.* Therefore, all its members (including x) are on the heap.

As far as I can see, you don't have a memory leak.


* Technically, the C++ standard doesn't talk about stack vs. heap, so it's up to the compiler where stuff gets allocated. But in practice, it will be stored in a heap-like structure.


Need Your Help

32 bit audio to 24 bit

c++ audio

I'm trying to convert 32 bit audio data into 24 bit audio. However, when I try to play it all I get is noise. It seems I'm missing something very basic?

eclipse warning: Finally block doesn't complete normally

java eclipse try-catch-finally

I have some code that throws a potential error and I catch these, then jump into a finally block, here it is in 'pseudo' form.

Where is the Python startup banner defined?

python python-3.x

I'm compiling several different versions of Python for my system, and I'd like to know where in the source the startup banner is defined so I can change it for each version. For example, when the