0

I tried to write code where i have to return the pointer pointing to the first element of array.

I tried using this:-

int *construct(int arr[],int n)
{
  int size=(int)(ceil(log2(n)));
  size=2*pow(2,size)-1;
  int st[size];
  for(int i=0;i<size;i++)
    st[i]=INT_MAX;
  constructUtil(arr,st,0,n-1,0);
  int *pt=&st[0];
  return pt;
} 

This gave me error. But when i declared the same array like this:

int *st=new int[size];

It executed successfully.

What is the difference between these two?

  • 2
    `new int[size]` is a C++ expression, not C. And if you're programming in C++ you should use `std::vector` instead. And perhaps [get a couple of good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn it properly. – Some programmer dude Jul 09 '19 at 04:28
  • 2
    As for the problem it's most likely that you return a pointer to a local variable. The array `st` is local to the `construct` function, its life-time will end when the function return, leaving you with a stray pointer. – Some programmer dude Jul 09 '19 at 04:28
  • The tag `C` is inappropriate for the code context `new int[size]`. Please remove the tag. – harper Jul 09 '19 at 06:11
  • What is `SIGESV`? Did you mean `SIGSEGV`? – Keith Thompson Jul 09 '19 at 06:25
  • This is not valid C because of the `new` expression. It's also not valid C++ because of the use of a variable-length array (`size` is not a constant expression). Some C++ compilers support VLAs as an extension, but the code is not portable. Incidentally, the problem is that you're returning the address of a local object, but that won't cause problems until the caller uses that address -- and you haven't shown us the calling code. For future reference, please provide a [mcve]. – Keith Thompson Jul 09 '19 at 06:28

1 Answers1

1

You can´t return a pointer to a local array in C. You have to use malloc to allocate the memory and generate a pointer to a memory region for your array. Now you can return the pointer and the memory stays valid:

#include <stdio.h>
#include <stdlib.h>

int* construct(int n);

int main()
{
    int* ArrayPointer = construct(100);
    printf("Address: %p", ArrayPointer);

    for(int i = 0; i < 100; i++)
    {
        printf("%i\n\r", ArrayPointer[i]);
    }

    free(ArrayPointer);
    return 0;
}

int* construct(int n)
{
    int* array = (int*)malloc(n * sizeof(int));
    for(int i = 0; i < n; i++)
    {
        array[i] = i;
    }

    return array;
} 

The instruction new does something similar to the malloc function (not exactly the same). The key operation of new is to ask the OS for some free memory for dynamic allocation (like malloc but from another memory pool). See the difference between malloc and new here. Another option (without dynamic memory) is to declare a static array with a fixed size. So the compiler will reserve the memory and you can pass the address of this array to your function

Kampi
  • 1,798
  • 18
  • 26