简体   繁体   中英

working without allocating memory for array

My friend did this code. It works fine. aim was to separate every digit of number in to each element of array. But memory for a is not allocated anywhere. Any idea how this one is working?

    #include<iostream> 
    #include<math.h> 
    using namespace std; 

    int* arrray(int num, int *x){ 
        int len=0; 
        while(num!=0){ 
            x[len]=num%10; 
            num/=10; 
            len++; 
        }  
        return x; 
    } 
    int main(){ 
        int num1=123;  
        int l=0,k=0; 
        int *a=arrray(num1,a); 
        int n1[3]; 
        for(int i=2;i>=0;i--){ 
            n1[l]=a[i]; l++; 
        } 

    }

You can use a variable on the same line you declare it:

int* a = foo(a);

Though you could fabricate a defined behavior doing this, in your case it is unsafe and will execute undefined behavior (personally, I don't think this type of usage should ever be employed).

Unassigned C++ variables hold undefined values. Could be zero, could be a value left their from the last variable that existed in that memory location. We generally call this data garbage . Using a garbage value as a pointer address is unsafe. It might "just work", it might "seg fault" and abort your application. You will find that this same code works some times, and aborts other times, or even causes other unexpected behavior (it could even corrupt it's own stack). Much of this depends on the compiler, the environment, and random luck.

It can be assumed the programmer made a mistake in this case (as you suspected), and it should indeed use allocated space.

Note that running this code in visual studio in debug mode, an assert is triggered:

Run-Time Check Failure #3 - The variable 'a' is being used without being initialized.

EDIT 2 There are other problems with this code, not only that a is uninitialized and a pointer is being used on unallocated space. The method arrray is working on an pointer with no information about the size of an array it points to (assumed). later, back in main , a for loop assumes it is safe to iterate from index 2 (inclusively) down to 0.

Also, because arrray never allocates space, and takes the array as a parameter, it is not necessary to return a pointer to. Not that this is wrong, technically, it can be a choice of style in fact, I'm only pointing out that it isn't necessary. It might make more sense to return the number of digits written.

Also, num could be zero to begin with, and that seems valid.

Perhaps a better arrray method would look like this:

int separateDigits(int num, int* p, int size)
{
  int digitsWritten = 0;

  for (int index = 0; index < size; index++)
  { 
    p[index] = num % 10; 
    num /= 10;
    digitsWritten++;

    if (num == 0)
      break;
  }

  return digitsWritten;
}

Then main can use the result:

int main()
{ 
  int num1 = 123;
  const int size = 10;
  int a[size];
  int digitsWritten = separateDigits(num1, a, size); 

  int n1[size]; 

  for(int i = 0; i < digitsWritten; i++)
  { 
    n1[i] = a[digitsWritten - i - 1];
  }

  return 0;
}

Visual C++

error C4700: uninitialized local variable 'a' used

Cygwin 64 bit

It does compile and run, sadly there is a segmentation fault on line 8 of the code since the array a is uninitialized.

This works because it is undefined behavior , even though there will be warnings:

warning: 'a' is used uninitialized in this function [-Wuninitialized] or error in the case of Visual C++.

Regardless, remember Scott Meyers in Effective C++: Item 47: Ensure that non-local static objects are initialized before they're used. I know this is for non-local static objects but in general you should initialize your variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM