C++ Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • micko
    New Member
    • Mar 2007
    • 5

    #1

    C++ Code

    What is wrong with this code?
    Code:
    int *array (int n)
    {
    	return new int(n);
    }
    
    int main()
    {
    	int *p = array(10);
    
    	for(int i = 0; i < 10; i++)
    	{
    		p[i]= 0;	// initialize all to 0
    	}
    	
    	printf( "%d\n", p[0]);
    	p = array(10);
    	printf ("%d\n", p[0]);
    	return 0;
    }
    Last edited by RedSon; Mar 19 '07, 10:53 PM. Reason: CODE Tags!
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Is this an assignment, try to find the problems in the code? Next time you should use CODE tags.

    Comment

    • micko
      New Member
      • Mar 2007
      • 5

      #3
      yes, try to find problem

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        We won't do your homework for you. What do you think is wrong with it?

        Comment

        • micko
          New Member
          • Mar 2007
          • 5

          #5
          I think there is a problem with the array function, it should return new array if integers.

          // CODE

          int *array (int n)
          {
          return new int(n); // should be new int[ ];
          }
          //

          Am I wrong?

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            That's one of the problems I spotted - there is one other similar to this one somewhere else in the code.

            Comment

            • micko
              New Member
              • Mar 2007
              • 5

              #7
              Dynamic allocation, using delete [ ] array?

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Yes, that would be the other problem.

                If you add these two things, do you think the code will work? Try running it through a compiler to see if it does work as expected.

                Comment

                • micko
                  New Member
                  • Mar 2007
                  • 5

                  #9
                  It compiles, first printout is 0 and the second is some address like -84551551.
                  But the program gives the same result without delete [ ] array.
                  I think it is good, it is always good to use delete.

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    I'd say that's correctly working now. The first result is the correct value, as the array returned by the function is filled with 0s. The second result is expected, too, because a new array is created, but not filled.

                    Do you think it is a problem that the second array is never filled, or that the first array is not deleted?

                    Comment

                    Working...