Hi people:

Here is my problem:

Codes: (My program is in the exact order as being represented here)

void push1(CStack stk) {
  stk.push(1);
  }

int main(void) {
  CStack mystack;
  printf("Stack initially: ");
  mystack.print();
  push1(mystack);
}

As you may read from above, the push1() function does not work at all.
I overheard from someone that this can be due to the input argument of push1(), e.g. stk(of type CStack) is located in a memory location can not be accessed by push1().
Can anyone give me some more information on this please?

Many thanks

Edit:: Use code tags. This is a warning.- WoLfPaCk

Dani AI

Generated

A few practical follow-ups to based on 's suggestion.

First, note the code shown is C++, not C (C has no references). There are three simple ways to make the callee change the original object: pass a pointer, pass a reference, or return the modified object. A pointer-based variant looks like this:

void push1_ptr(CStack* s) {
  if (s) s->push(1);
}

int main() {
  CStack mystack;
  push1_ptr(&mystack);
}

If the change still "does not work", instrument the class and call sites to see what actually happens. Print the object address in both places or add a logging copy constructor / destructor to detect unexpected copies:

CStack(const CStack& other) {
  puts("CStack copy ctor");
  // perform a deep copy if needed
}

void push1_debug(const CStack& s) {
  printf("addr in func: %p\n", (void*)&s);
}

Common root causes to check (fast tests you can run):

  • The stack type holds raw pointers and lacks a proper copy constructor/assignment (Rule of Three/Five). Passing by value can create two objects that share the same internal pointer, causing corruption or double-free.
  • Object slicing: if you pass a derived object by value to a base-type parameter, derived parts are lost.
  • Tag confusion: compiling as C vs C++ can change behavior.

Quick fixes:

  • For mutation, pass by non-const reference or pointer.
  • For read-only access, pass by const reference to avoid copies.
  • For ownership transfer or heavy objects, prefer move semantics (C++11) or return by value and rely on move/copy elision.
  • Prefer std::stack/std::vector or smart pointers to avoid manual memory bugs.

Instrumenting the copy ctor/destructor and printing addresses will quickly tell whether you are working on the original or on a copy.

Recommended Answers

All 4 Replies

You are passing the stk variable by value. You should pass it by reference if you want to update the original value after it is returned from the function. Try this

void push1(CStack& stk) {
  stk.push(1);
  }

int main(void) {
  CStack mystack;
  printf("Stack initially: ");
  mystack.print();
  push1(mystack);
  printf("Stack after the push: ");
  mystack.print();
}

thanks, wolf.
May I ask you for another favor?
Could you please explain it a bit more?
I mean, the difference between passing by value and passing by reference.

I know some basic knowledge about passing by reference.

Thanks

difference between passing by value and passing by reference.

When you pass a variable to a function by value, the function creates a local copy of that variable and acts upon it. The variable that you passed into the function is not changed. So after the function finishes it's operation the original variable's value remains unchanged.

When you pass by reference, the function does the processing on the original variable's memory location. So the changes that are done to the variable are saved even after the function returns after processing.

That just makes so much sense. Wolf.
Thank you a lot for this fast and consice reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.