what is wrong with my program?

This is a discussion on what is wrong with my program? within the C++ Programming forums, part of the General Programming Boards category; Hi all. Im writing this program in which I use a function that returns int *. Actually I wanted an ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Question what is wrong with my program?

    Hi all. Im writing this program in which I use a function that returns int *. Actually I wanted an array to be returned and since its not possible, I return array s at the end of the function( s here is treated as the pointer to the first element of the array. Isnt it?)
    Code:
    int* function1(int a,int c, int b)
    {
    int s[1000];
    	.
    	.
    	.
    	.
    	return s;
    
    }
    Later in my program I use this function in the following for loop:
    Code:
    int s1[1000];
    for(int y=0 ; y<1000; y++)
    	s1[y]=*(function(4,n,b)+y);
    function1, which was defined before, returns a pointer to the first element of the array. Then it is added with y so that it moves to the next memory space and points to the following elements of array s. in each iteration of the loop the pointer points to a specific element of s and by using the operator * and assigning *(pointer) to array s1, I expected the elements of array s1 to get the values of the previously defined array s.
    But when I debugged my program and I observed the changes in the variables and stuff, I noticed that the elements of array s1 all have the same nonsense value of -858993460.
    I have tested function1 separately and theres nothing wrong with it.

    Can someone please tell me what is wrong with my program?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,749
    Code:
    int s[1000];
    	.
    	return s;
    You are trying to return a pointer to a local variable, this variable will go out of scope and be destroyed when you leave the function.

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    so would it be ok if I declare a global array, then use it in the function and return it when the function is over? I think it might lead to the same mistake.
    I have called this function several times in my program. will it work if I declare a global array which acts as s[1000], then pass the array to the function each time it is called?
    Last edited by Farnaz; 02-28-2011 at 11:22 AM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Quote Originally Posted by Farnaz View Post
    so would it be ok if I declare a global array, then use it in the function and return it when the function is over?
    No. Global variables should be avoided.
    Using them in this situation is even worse.

    Quote Originally Posted by Farnaz View Post
    So how would I do it then?
    Use std::vector.
    Better yet, pass the vector in to the function as a reference in order to avoid relying on compiler optimizations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    I did this:
    Code:
    int s[1000];
    
    int* function(int a,int c, int b,int*s)
    {	
           .
           .
           .
        return s;
    }
    
    for(int y=0 ; y<b+1; y++)
    	s1[y]=*(function(4,n,b,s)+y);
    but it's still the same and does not work.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Use std::vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,749
    will it work if I declare a global array which acts as s[1000], then pass the array to the function each time it is called?
    No, if you pass this array as a function parameter the parameter and the global will be different variables.

    Why can't you pass the array as a function parameter? By passing the array to the function as a parameter any changes made in the function will be made to the array in the calling function.

    Code:
    void myFunct(int marray[], int arraySize);
    int main()
    {
       int s[100];
       myFunct( s, 100);
       return(0);
    }
    Jim

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Don't do that! Use std::vector.
    Sheesh.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,749
    Don't do that! Use std::vector.
    While std::vector has it's uses it is not the only way to get things done. If the OP has not yet learned vectors in class then a old fashioned array will work just fine.

    Jim

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Then you can just dump C++ and go C.
    C arrays are not needed to learn C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,749
    Then you can just dump C++ and go C.
    Why would I want to do that, C++ includes arrays in it's specification where C does not include C++ classes. Arrays are a valuable part of both languages.

    Jim

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    C++ has std::array and std::vector. C arrays be damned.
    They're unsafe. You can't query their size. You can't get bounds checking. And they don't work well with all standard library functions because they lack iterators.
    So don't use them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by Elysia View Post
    Then you can just dump C++ and go C.
    C arrays are not needed to learn C++.
    Yes, but you have to understand basis to get good background of how this stuff works. I wonder whether you started with vectors at once or used regular arrays at first. Arrays are still needed (for APIs for example) and especially for learning.

    Beginners should deal with arrays firstly and feel their weaknesses, then with this expierience they should move to vector.
    Of course I could also say "learn C before C++", but I wouldn't go so far.
    I never put signature, but I decided to make an exception.

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    thank you both but I have like 5 or 6 arrays in my program and I have wrote the whole program with arrays. if I wanna change them to vectors then I'll have to develop the program from the beginning. I've got a question here. if I use vector in my function then I should return vector<int>. and I have to assign it to an array later, when the function is called. can I do such a thing?
    somewhat like:
    for(int i=0 ; i<myvector.size() ; i++)
    s1[i]=myvector[i];

    and there is another point. by passing the array to the function(as shown below) I meant it to be changed (it is not important that array s be changed or not. coz it's not directly used in the program)
    Code:
    int s[1000];
    
    int* function(int a,int c, int b,int*s)
    {	
           .
           s[i]=p;
           .
        return s;
    }
    
    for(int y=0 ; y<b+1; y++)
    	s1[y]=*(function(4,n,b,s)+y);

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Quote Originally Posted by kmdv View Post
    Yes, but you have to understand basis to get good background of how this stuff works.
    Of course. But do you have to understand how arrays work to use vector? No.
    Do you have to understand how vectors and iterators work to create algorithms? Yes.
    My point being that while you have to understand how things work, you don't have to understand them at such a low level.

    I wonder whether you started with vectors at once or used regular arrays at first. Arrays are still needed (for APIs for example) and especially for learning.
    It's a shame I didn't learn vector and stuff before I learned arrays. Because of that, I was a disgusting C+ programmer for many years before finding this forum and absorbing the knowledge here which allowed me to transform myself to a C++ programmer.

    Beginners should deal with arrays firstly and feel their weaknesses, then with this expierience they should move to vector.
    Of course I could also say "learn C before C++", but I wouldn't go so far.
    There are many here who would disagree with you, and I am one of them.
    Learn the language properly, then move into the advanced parts (ie C arrays and C api).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with this program??
    By cprog12 in forum C Programming
    Replies: 3
    Last Post: 11-24-2010, 11:48 AM
  2. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  3. Replies: 5
    Last Post: 01-13-2007, 01:14 AM
  4. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 01:03 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21