Hi, all i have big assignment and don't have any compiler problem just have runtime bug--this make more headache.

Here is the driver;

int main{
          int * values2 = new int [ARRAY_SIZE * 2];
	  values2[ARRAY_SIZE * 2 - 2] = 8;
	  values2[ARRAY_SIZE * 2 - 1] = 5;
         stack.pop (values2, 0);
/*stack belong to Intstack class and the pop function is to pop everything inside the stack into the values2;that means pop(values2,0) didn't do anything, but pop(values2,1) means pop out the top int element inside stack to the values2[0]. There already have 200 int element inside the intstack*/
	  stack.pop (values2, 1);
       	   stack.pop (&(values2[1]),ARRAY_SIZE - 1);
     //print out the array  
     std::cout << "About to output  values2 arrays" << std::endl;

		for (i = 0; i < COUNTOF (values); ++i)
		{
			std::cout << << values2 [i] << std::endl;
		}

		std::cout << "Finished output of values2 arrays" << std::endl;
}

Here is my code

class Intstack 
{
//private element top, p and size;
//public functions and constructor including pop and overload pop function;
};

void IntStack::pop(int a[],size_t n)//this is my pop function to implement the main
{
 for(int i=0;i<n;i++)
 {
 
  if(top==-1)
  {cout<<"There is empty\n";}
  else
  {
   int v;
   v=p[top];
   top--;
   a[i]=v;
  }
 }
}

So basically, the results should like:

About to output values2 arrays
element from 1-200
Finished output of values2 arrays

Dani AI

Generated

A short diagnosis and concrete checks

The behavior described (printing addresses or nonsense values, and different results on XP vs Vista) very often comes from undefined behavior — buffer overruns, underflows, or printing a pointer instead of an int. As observed, a stray or malformed << could cause a wrong expression to be emitted; as pointed out, macros that compute counts and missing declarations hide the real size/indices issues. The two most likely root causes are (1) the code is printing a pointer instead of an integer, or (2) memory corruption (writing out of bounds) has corrupted nearby memory.

Step-by-step troubleshooting

  1. Make a minimal reproducible test that only contains the IntStack internals, the allocation for the destination array, the exact pop calls, and the print loop. Remove unrelated code.
  2. Turn on compile warnings and runtime checks: use -Wall -Wextra, and in Visual Studio enable runtime checks (/RTC) and run a debug build. Run under AddressSanitizer or Valgrind to catch overruns (AddressSanitizer, Valgrind manual).
  3. Verify the print statement actually prints the array element (e.g. std::cout << dest[i]) and not the pointer (dest), and confirm your loop limit is a real element count (beware doing sizeof on a pointer — see sizeof operator).
  4. Add assertions/logging inside pop to print top, p (the buffer pointer), the index being written, and the destination pointer passed. If top becomes negative or you write past allocated space you will see it.

Safer replacement pattern (sketch)

Replace a void pop that blindly writes N items with a checked routine that returns how many items were actually written and validates the destination pointer. For example, a pop_to(int* dest, size_t capacity) that returns the number written prevents overrun and makes the caller handle the actual count. Then print exactly that many elements.

Final notes

Different OSes showing different symptoms is a hallmark of undefined behavior. Prefer std::vector or std::array for storage, avoid custom size macros, and post a minimal, complete example (including ARRAY_SIZE, the IntStack fields and p buffer) if further help is needed.

Recommended Answers

All 8 Replies

Eewww, Slove your own bug.

Sorry about the format.
Post it again:
Hi, all i have big assignment and don't have any compiler problem just have runtime bug--this make more headache.

Here is the driver;

/*stack belong to Intstack class and the pop function is to pop everything inside the stack into the values2;that means pop(values2,0) didn't do anything, but pop(values2,1) means pop out the top int element inside stack to the values2[0]. There already have 200 int element inside the intstack*/

int main{
          int * values2 = new int [ARRAY_SIZE * 2];
	  values2[ARRAY_SIZE * 2 - 2] = 8;
	  values2[ARRAY_SIZE * 2 - 1] = 5;
         stack.pop (values2, 0);

	  stack.pop (values2, 1);
       	   stack.pop (&(values2[1]),ARRAY_SIZE - 1);

     //now print out the array  
     std::cout << "About to output  values2 arrays" << std::endl;

		for (i = 0; i < COUNTOF (values); ++i)
		{
			std::cout << << values2 [i] << std::endl;
		}

		std::cout << "Finished output of values2 arrays" << std::endl;
}

Here is my code

class Intstack 
{
//private element top, p and size;
//public functions and constructor including pop and overload pop function;
};

void IntStack::pop(int a[],size_t n)//this is my pop function to implement the main
{
 for(int i=0;i<n;i++)
 {
 
  if(top==-1)
  {cout<<"There is empty\n";}
  else
  {
   int v;
   v=p[top];
   top--;
   a[i]=v;
  }
 }
}

So basically, the results should like:

About to output values2 arrays
element from 1-200
Finished output of values2 arrays

My problem is on the runtime the array pointer give me the address instead of the real int element. I don't know where is the problem.
Thanks la.

why nobody help me??

std::cout << << values2 << std::endl;
TWO sets of <<
Is that even legal?

Ok, this is some bugs when i post it here.
Sorry about that.
I check my code again in the windows XP platform its working fine now. Don't know why??
Cause i run it at home which the OS is vista, it have some bugs.

How is anyone suppost to GUESS what is wrong with that ??

There is no definition for Intstack but a cryptic comment about what is private/public.

Beginners using #define macros/parameters are heading for a (100-epsilon)% certainty of having incomprehensible bugs. Use const int or a function.

There are no declarations. What is ARRAY_SIZE, did you leave it undefined so you have [-1] on line 6?

You allocate memory (values2) and it gets dropped on program exit only. Really not good practice.

If you use macros like COUNTOF(values) [by the way what is values??? did you forget the 2) , especially as I can guess that
COUNTOF is something like

// DONT EVER WRITE THIS:
#define COUNTOF(v) sizeof(v)/sizeof(int)

I am beginnng to think that we need a stick post that says :
(a) Use code tags
(b) format your code so it look nice
(c) if you want people to find bugs post a complete example
(d) check that your example REALLY compiles if you are looking for a runtime bug.
(e) Spend some time simplifing your example and adding appropiate comments.
(f) Copy and paste DIRECTLY.
(g) Check you post.

You are asking people to FREELY give up there time and help, show them some respect.

To all sorry for the rant, I have read a large number of posts recently that I think fail to fill that brief.

well, if you don't want to read my post, you don't have to.

Apparently, this is just part of my code and which you point out are all incorrect. Except that guess.

About the format post, i will pay more attention next time and show my respects.

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.