There is a timeout error, what's wrong?

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

#include<stdio.h>
int find_max( int x[] )
{
    int i;
    int max;
    max=x[0];
    for(i=0;i<5;i++){
        if(x[i]>max){
            max=x[i];
        }
    }
    return &max;
}
int main(){
    int n, i;
    int a[5];
    int *maxp;

    for(i = 0; i < 5; i++) {
        scanf("%d", &a[i]);
    }

    maxp = find_max(a);

    printf("max: %d\n", *maxp);

    return 0;
}

c

2022-09-20 14:46

2 Answers

? It's not Timeout, but Segfault.

You will understand quickly if you know that you push and pop functions when you call the stack

Every time a function is called, the stack for the function is emptied and returned.

Thus, the return &max; of the find_max function is interpreted by the compiler (in my case, gcc)

It will automatically return 0; because you specified a stack space that will eventually disappear.

#gcc -o lk lk.c -fno-stack-protector -fPIC -no-pie
gdb-peda$ disas find_max
Dump of assembler code for function find_max:

...overlapping
   0x00000000004005a7 <+80>:    add    DWORD PTR [rbp-0x4],0x1 //i++
   0x00000000004005ab <+84>:    cmp    DWORD PTR [rbp-0x4],0x4 //forloop i=4 end;
   0x00000000004005af <+88>:    jle    0x400571 <find_max+26>
   0x0000004005b1 <+90>: moveax,0x0 // return &max; but returns 0.
   0x00000000004005b6 <+95>:    pop    rbp
   0x00000000004005b7 <+96>:    ret

Dump of assembler code for function main:
   0x00000000004005b8 <+0>:     push   rbp
   0x00000000004005b9 <+1>:     mov    rbp,rsp
   0x00000000004005bc <+4>:     sub    rsp,0x30 // int n, int a[5];
   0x00000000004005c0 <+8>:     mov    DWORD PTR [rbp-0x4],0x0 //int i=0;
...overlapping
   0x00000000004005ff <+71>:    call   0x400557 <find_max>
   0x0000000000400604 <+76>:    cdqe
   0x000000400606 <+78>: mov QWORD PTR [rbp-0x10], Rex // maxp = 0 (return value of find_max)
   0x000000000040060a <+82>:    mov    rax,QWORD PTR [rbp-0x10] // rax = 0
   0x000000000040060e <+86>:    mov    eax,DWORD PTR [rax] // eax = *0 
                                                           // 0x0 Address value is not accessible << Segfault cause 
   0x0000000000400610 <+88>:    mov    esi,eax            
   0x0000000000400612 <+90>:    lea    rdi,[rip+0x9e]        # 0x4006b7 //"max: %d\n"
   0x0000000000400619 <+97>:    mov    eax,0x0
   0x000000000040061e <+102>:   call   0x400450 <[email protected]> // printf(rdi, esi) linux calling convention
the omission of

So the answer to what to do is to create a heap space through the malloc function

I think it's right to point the maxp and put the max value in the space.

#include <stdio.h>
#include <stdlib.h>
void find_max( int x[], int *maxp )
{
    int i;
    int max;
    max=x[0];
    for(i=0;i<5;i++){
        if(x[i]>max){
            max=x[i];
        }
    }
    *maxp = max;
}

int main(){
    int n, i;
    int a[5];
    int *maxp = malloc(sizeof(int));

    for(i = 0; i < 5; i++) {
        scanf("%d", &a[i]);
    }
    find_max(a, maxp);
    printf("max: %d\n", *maxp);

    return 0;
}

// // gcc -o lk lk.c
// The result
2759
3333
1927
6927
3452
max: 6927


2022-09-20 14:46

The other person gave a good answer, so read it carefully.

I think maybe the questioner was trying to do this.

#include <stdio.h>

int *find_max(int x[])
{
    int i;
    int *maxp;
    maxp = x;
    for (i = 0; i < 5; i++)
    {
        if (x[i] > *maxp)
        {
            maxp = x + i;
        }
    }
    return maxp;
}

int main()
{
    int n, i;
    int a[5];
    int *maxp;

    for (i = 0; i < 5; i++)
    {
        scanf("%d", &a[i]);
    }

    maxp = find_max(a);

    printf("max: %d\n", *maxp);

    return 0;
}


2022-09-20 14:46

If you have any answers or tips


© 2022 pinfo. All rights reserved.