Thread: reallocating space for an array and returning the new array

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Unhappy reallocating space for an array and returning the new array

    Hi everyone!

    I've got a seemingly simple project for school in which we have command line tool that needs to add two large integers that get passed to me as arrays. NO LIBRARY FUNCTIONS ALLOWED! No malloc, nothing!

    I've run into a problem when i need perform a carry operation on the final digit (the highest order digit) because there is no space left in the array. In java or C# I would just make a new array that is one larger and loop the old values into the new array, however, I can't really figure out how to get this to work in C. Here is the method signature

    int* large_add(int* input1, int* input2)

    where input1 and input2 are arrays that come from main().

    In the end I tried making a new int array called temp and tried to return it in any way i could think of. Including the following

    return temp;
    return *temp;
    return &temp;
    return temp[0];

    but I'm always getting warnings, and in any case the carry digit never shows up when the program prints the final result.

    So, what I would really like to know is first (1) How do you return a local variable? Is this a smart thing to do in C? and (2) How can I change the size of input1? (I do the calculations on input1 instead of creating a new array because I can't seem to do it right...) and (3) if I am passed int* input1 and int* input 2 as formal parameters, how can i make a new array (called "result" for example) and then return it as int* ?

    Thanks,
    Jim

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, you can return local variables - but you have to "catch" them at once, to ensure validity, because they go out of scope, and are thereafter, available for any other program to use that memory.


    << EDIT: perhaps more clear and to the point >>
    Code:
    int returnLocal(void); //prototype - specifies return will be an integer
    
    int main(void) {  //main() also return an int in C
      int returnedInt;
       
      returnLocal();  //wrong nothing to catch the return value
      
      returnedInt=returnLocal();  //right! :)
      //do something with the value
      printf("\n Returned value: %d", returnedInt);
      //do something else with returnedInt
    
      return 0;
    }
    int returnLocal(void) {
      int a=3;
      int b=4;  
    
      return a+b; //add a and b, and return it.
    }
    I'm not sure how you plan on "making a new array", without using an allocation function (malloc(), calloc(), etc.). Can you explain that a bit more?
    Last edited by Adak; 10-02-2010 at 06:24 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Overwrite one of the arrays passed with the result, and return that. That requires main() to allocate enough memory for the array that will be overwritten, so it can hold the result.

    Alternately, statically allocate an array within the function large enough to hold the result (note there will be an upper limit on the size defined at compile time, and also limitations on how the function can be called).

    Incidentally, you haven't said anything about how you detect the sizes of the arrays passed (or, for that matter, how main() would detect the size of the array returned).

    Adak, it is possible to return local variables, but not the address of local variables - doing so causes the caller to exhibit undefined behaviour if it dereferences the returned address. This also means you cannot return local arrays, unless they are static.
    Last edited by grumpy; 10-02-2010 at 05:24 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Horton's book was big on that: Don't return the address of local variables! I have succeeded in doing that, in some tests, but it's not anything too reliable.

    A local variable can be returned, but has to be "caught" by the receiving function.

    I thought he said he couldn't use ANY functions like malloc(), etc.! Just addresses of static arrays from main() should be enough.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    As Grumpy stated, there HAS to be (Presumably) a limit to how large the numbers are. (how many digits can be stored in your static array). If so, your result array has to be 1 digit larger to account for a possible carry over. Alternatively you can just make it the same size and use a flag to remember whether you have an additional digit(1) at the beginning. Then, when you print the array you start by printing 1 or 0 depending on what the flag is and then move on to your array.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. Returning Array
    By baffa in forum C Programming
    Replies: 26
    Last Post: 02-01-2008, 10:08 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  5. Replies: 6
    Last Post: 10-21-2003, 09:57 PM

Tags for this Thread