variables in a pointer to a function( hurry up and respond somebody)

Write your question here.

Ok, this is not literally a problem just a very important c++ aspect regarding values in a function that is used with a pointer. Its critical i know this and it seems there some c++ god minds that is on this forum site that can avail me this info. PLEASE READ ALL THE COMMENTS BELOW dont skip any of them ITS IMPORTANT.

so check out the code snippet below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  
// the header file
class tops
{
public:

std::vector< ter* > dkl;

void tp();

};

void struct 
{
  ter() = default;
  int c, d;
  ter( int a, int b);
};

// the cpp file

ter::ter( int a, int b)
{
  c=a;  //PLEASE PAY ATTENTION RIGHT HERE. ---IS the int 'c' a VALUE STORED ON HEAP OR STACK?

  d=b;  // THE SAME QUESTION WITH 'd'
}

void tops::tp()
{
   ter tl;

   int a,b;
   a=30;
   b=12;   

   dkl.push_back( new ter( a, b) ); // ARE THESES VALUES and this instance of 'ter' gonna be on heap
// or just the function itself not the values its accepting.
// Cuz i'm wondering if 'a' & 'b' will be values on heap in the location where 
// the address of 'ter' should be located. Or is ter referring them on stack
//Even though 'ter' itself is on heap?
}
c=a; //PLEASE PAY ATTENTION RIGHT HERE. ---IS the int 'c' a VALUE STORED ON HEAP OR STACK?

d=b; // THE SAME QUESTION WITH 'd'
It depends on where the ter instance was allocated.

// ARE THESES VALUES and this instance of 'ter' gonna be on heap
// or just the function itself not the values its accepting.
// Cuz i'm wondering if 'a' & 'b' will be values on heap in the location where
// the address of 'ter' should be located.
a and b are on the stack.
In addition to what helios told you (which is all absolutely correct), the instance of ter that you create at line 37 will be on the heap, because you dynamically allocated it. Therefore, the members c and d of that object will be on the heap.

// Cuz i'm wondering if 'a' & 'b' will be values on heap in the location where
// the address of 'ter' should be located.

Why would they be on the heap? It depends on how you create them - at line 33, where you create them on the stack.

Just because somewhere else nearby in your code, you happen to create a different object on the heap, doesn't magically cause those variables also to be created on the heap.

EDIT: Telling people to "hurry up and respond" is rude. We're all just normal people, giving up our time to be here and help when we can. It's nobody's job to be here. It's nobody's job to answer your question. Barking demands at people is going to make us less likely to help you, not more likely.
Last edited on
I suspect this was asked in the middle of a test, to boot.
Topic archived. No new replies allowed.