1

I need to make simple program that makes new array which is made of sqrt-ed elements from first array. I must write elements of new array in main. I tried to debug my code, and variables x and y are displaying good numbers (I initialized them only so I can watch progress on debugger), but when I try to write elements of my new array, its showing some weird numbers. Can some good soul explain me what I am doing wrong ?

void returnSqrtArrayt(int* myArray, int length, double** newArray)
{
    int i;
    double x, y;
    double* p;
    double* arraySqrt=(double*)malloc(length*sizeof(double));
    for(i=0;i<length;i++)
    {
        x=*myArray;
        *arraySqrt=sqrt(x);
        y=*arraySqrt;
        myArray++;
        arraySqrt++;
    }

    *newArray=arraySqrt;

    p=arraySqrt;

    for(i=0;i<length;i++)
    {
        printf("%e\n",*p);
        p++;
    }
    printf("\n");
}

void main()
{
    int myArray[5]={4,2,16,4,81};
    int length=sizeof(myArray)/sizeof(int);
    int i;
    double* newArray;

    returnSqrtArrayt(myArray, length, &newArray);

    for(i=0;i<length;i++)
    {
        printf("%e\n",*newArray);
        newArray++;
    }   
}
danglingpointer
  • 4,708
  • 3
  • 24
  • 42
Mahir Duraković
  • 135
  • 1
  • 23
  • `double* arraySqrt=(double*)malloc(length*sizeof(double));` Is not correct. It's a good reason to [not cast the return value of malloc](http://stackoverflow.com/a/954785/817643). – StoryTeller - Unslander Monica Feb 15 '14 at 22:22
  • You shouldn't do `newArray++;`, you're immediately going to lose your reference to the memory you `malloc()`ed, and you're not going to be able to use those elements again, unless you back up. – Crowman Feb 15 '14 at 22:23
  • 1
    @StoryTeller: The `malloc()` call is fine, apart from the cast. – Crowman Feb 15 '14 at 22:24
  • StoryTeller: when I dont cast malloc, I get this error: IntelliSense: a value of type "void *" cannot be used to initialize an entity of type "double *" @PaulGriffiths: Can u explain me how can I write elements of my new array from main ? – Mahir Duraković Feb 15 '14 at 22:28
  • @MahirDuraković: Then you have mis-tagged your question as C, when you're actually using C++. You can write elements of your new array as `newArray[i] = whatever;` once you've fixed the problem with storing the wrong pointer. – Crowman Feb 15 '14 at 22:29
  • @PaulGriffiths, you haven't read the bit I linked to, have you!? It's pretty good general advise. Mahir, that seems like an IDE error, and not a compiler error. Are you by any chance using Visual Studio? – StoryTeller - Unslander Monica Feb 15 '14 at 22:30
  • @StoryTeller: I agree that casting the return from `malloc()` is a bad idea in C. But, contrary to what you claimed, the `malloc()` call here is not incorrect. – Crowman Feb 15 '14 at 22:32
  • @PaulGriffiths, I could have sworn `arraySqrt` is of type `double**`. My bad. – StoryTeller - Unslander Monica Feb 15 '14 at 22:33

1 Answers1

2

What you experience is undefined behavior because what you do is:

void returnSqrtArrayt(int* myArray, int length, double** newArray)
{
    ...
    double* arraySqrt= malloc(...);         // create new array

    for(i=0;i<length;i++)
    {
        ...
        arraySqrt++;                        // increment pointer to this array
    }

    *newArray=arraySqrt;                    // use incremented pointer

    // any use of *newArray or arraySqrt here tries to access incorrect memory !
}
It's good that you use temporary pointer p for printing since incrementing p doesn't affect original pointer, but try the following changes:
  • do *newArray=arraySqrt; immediately after malloc call
  • instead of p=arraySqrt; use p=*newArray; while printing inside your function
Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167