Arrays as arguments in a function

I want to pass an array as an argument in the following function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void sortlist (int& t, nodes *kk=new nodes){
	nodes temp;             // holding variable
      bool didSwap;
    do
    {
        didSwap = false; //assume there are no swaps
       
        for (int i = 0; i < t - 1; i++)
        {
            if (kk[i].hops < kk[i+1].hops)
            {
                //I am not cheating
                temp = kk[i];
                kk[i] = kk[i+1];
                kk[i+1] = temp;
                didSwap = true; //there was a swap...
            }  
        }
    } while (didSwap);
    do
    {
        didSwap = false; //assume there are no swaps
       
        for (int i = 0; i < t - 1; i++)
        {
            if (kk[i].hops <= kk[i+1].hops&&kk[i].non<kk[i+1].non)
            {
                //I am not cheating
                temp = kk[i];
                kk[i] = kk[i+1];
                kk[i+1] = temp;
                didSwap = true; //there was a swap...
            }  
        }
    } while (didSwap);
    delete kk;
)


I try to do this with the following code but it produces a segfault. Can you tell me what is wrong with it?

1
2
3
4
 nodes *arr=new nodes;
	arr=&nodearr[n]; 
	sortlist(n,arr);
	delete arr;
You need to read up and understand some basics:

 
nodes *arr=new nodes;


allocates ONE nodes instance and returns a pointer to it.

In

 
arr=&nodearr[n];


where did nodearr[] come from?!? Is it an automatic/local variable or dynamically allocated?
Also, by making that assignment, you've just created a memory leak because nothing points to the new nodes that you allocated on the previous line.

I think you may want something like

 
nodes *arr = &nodearr[0];


without a new and delete, but I am very confused by your code.

Further down, you are calling

 
delete arr;


effectively deleting a pointer that you did not allocate (because it's not pointing to the memory that you did allocate). Not only is this very bad practice, it's at least one of the causes for your segfault.

Understand what you are trying to do before you code, or you will have a lot of problems with pointers, even if you don't segfault... I think there is a lot of confusion here.

Last edited on
Here's an example for what you want to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
callingFunction()
{
   int n;
   int arr[10];
   sortList(n, arr);  //arr is pointer to arr[0]
}


sortList(int n; int* arr)
{
   //implementation of sortList function
}


Passing an array as argument would be easier if you recognize that name of an array is a pointer to the first element of an array and subsequent elements can be accessed by that pointer
I agree with kfmfe04. The name of the array is a constant pointer to the first element of the array. What I see is arr=&nodearr[n]. If you write arr=nodearr[n] then it stores the address of the first element of the array into the pointer arr. If i am not wrong then &nodearr will store the address of the pointer nodearr to arr which probably you do not wish to do.
Topic archived. No new replies allowed.