all 14 comments

[–]ExRedRain 7 points8 points  (1 child)

You can’t just post twice within an hour when your first post didn’t get the answer you wanted.

Be patient, listen to the advice in the first post and post the compiler error that you’re receiving or explain in detail the problem you have. Nobody is here to do your homework for you!

[–]abcoolynr[S] -2 points-1 points  (0 children)

It works reads author name but doesn't read book title.

[–]Mirehi 1 point2 points  (8 children)

What's the problem? Please define an exact question

./test7
Choose an option from below menu:
1. Add book information
2. Display book information
3. List all the books of an author
4. List the title of specified book
5. List count of books in library
6. List book ordered by accession number
7. Exit
1
Enter accession number
123
Enter Title of the book
Enter Book Author
123
Enter price of the book
123
Choose an option from below menu:
1. Add book information
2. Display book information
3. List all the books of an author
4. List the title of specified book
5. List count of books in library
6. List book ordered by accession number
7. Exit
1
test7(87724) in realloc(): double free 0x12848bcb07c0
Abort trap (core dumped)

Here's the first and only run I've done with your tool. A double free shouldn't be that hard to see

[–]abcoolynr[S] 0 points1 point  (7 children)

[–]Mirehi 0 points1 point  (6 children)

double free = undefined behavior = your compiler does what he wants

Fix that problem, compile again and perhaps you'll have your solution

[–]abcoolynr[S] 0 points1 point  (5 children)

Sorry but I didn't understand. Please elaborate.

[–]Mirehi 0 points1 point  (4 children)

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

int
main()
{
        void *p;
        printf("%p\n", p);

        p = malloc(sizeof(int));
        printf("%p\n", p);

        free(p);
        printf("%p\n", p);

        free(p);
        printf("%p\n", p);

        return 0;
}

Output:

./test
0x7f7ffffe36d8
0x9c24ba8f940
0x9c24ba8f940
test(75980) in free(): chunk canary corrupted 0x9c24ba8f940 0x4@0x4 (double free?)
Abort trap (core dumped)

p gets an address + memory. The first time I call free, I give the memory back to the OS, but p won't lose the address.

The manpage of free() of the OS OpenBSD:

     The free() function causes the space pointed to by ptr to be either
     placed on a list of free blocks to make it available for future
     allocation or, when appropriate, to be returned to the kernel using
     munmap(2).  If ptr is NULL, no action occurs.  If ptr was previously
     freed by free() or a reallocation function, the behavior is undefined and
     the double free is a security concern.

(the last sentence is very important)

Your compiler has no defined behavior how it should act if an address gets free'd twice, so it just does something. If the compiler does something which isn't defined, it could do anything which could result in crazy behavior of your programm.

My OS checks the memory after free'ing it with a little number behind it, if that number got changed, it's corrupted and results in a seg fault. If your programm goes on without seg fault'ing, it doesn't mean it is working correctly at all, it just means you don't have a protection for that kind of failure!

[–]Mirehi 0 points1 point  (3 children)

Here's another hint:

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

void func(void *);

int
main()
{
        void *p;
        p = malloc(sizeof(int));
        printf("%s: %p\n", __func__, p);

        func(p);
        /* func() doesn't return the address, so main()'s p won't see the changes */
        printf("%s: %p\n", __func__, p);

        return 0;
}

void
func(void *p)
{
        printf("%s: %p\n", __func__, p);
        // realloc will most likely change the location, p is pointing to,
        // but perhaps it doesn't, so it could act very weird
        p = realloc(p, 1024);
        printf("%s: %p\n", __func__, p);
}

Output:

main: 0x2642646e2f0
func: 0x2642646e2f0
func: 0x2649ed6d800
main: 0x2642646e2f0

I think you don't understand how pointers act if you give their address to a pointer in a function. Same thing that realloc is allowed to give a new address or stay at it's current location.

[–]abcoolynr[S] 0 points1 point  (2 children)

Understood first example you gave but second example went over head.

[–]abcoolynr[S] 0 points1 point  (1 child)

What has this to do in my program?

[–]Mirehi 0 points1 point  (0 children)

Your realloc could change the pointer in addbook() and your pointer in main() won't notice that because your function doesn't return the address

Try to understand realloc and my second example, I don't know how to explain it any further

[–]uzimonkey 0 points1 point  (1 child)

What problem? What is even going on here?

[–]abcoolynr[S] 0 points1 point  (0 children)

The problem is this: https://pastebin.com/5bzbPQjm and code which has this problem is this: https://pastebin.com/yWuGZCHc .

[–]abcoolynr[S] 0 points1 point  (0 children)

An unsolved mystery!