Author Topic: Codeblocks showing wrong output with memmove().  (Read 376 times)

Offline Gobtotem

  • Single posting newcomer
  • *
  • Posts: 3
Codeblocks showing wrong output with memmove().
« on: November 27, 2018, 07:13:09 am »
So i ran the below code on codeblocks and it showed 11234 with both memmove() while the second one should be 12344.I ran the programme on gcc on linux terminal and it showed 12344 for the second memmove() which is correct. The compiler i am using for codeblocks is gcc which i ran on the linux terminal and it gave the correct output.So don't know what is the problem here.
OUTPUT:- https://imgur.com/a/vy8EdL7
CODE:-
Code: [Select]
#include<stdio.h>
#include<string.h>
int main()
{
    char array[5]={1,2,3,4,5};
    memmove(&array[1],&array[0],4);
    for(int i=0;i<5;i++)
    {
        printf("%d",array[i]);
    }
    printf("\n");

 memmove(&array[0],&array[1],4);
    for(int i=0;i<5;i++)
    {
        printf("%d",array[i]);
    }
    printf("\n");
}

BUILD LOG
Code: [Select]

-------------- Clean: Release in test (compiler: GNU GCC Compiler)---------------

Cleaned "test - Release"

-------------- Build: Release in test (compiler: GNU GCC Compiler)---------------

gcc -Wall -O2 -Wfatal-errors -Wextra -Wall -std=c11  -c /home/titan/Desktop/C_source/test/main.c -o obj/Release/main.o
g++  -o bin/Release/test obj/Release/main.o  -s 
Output file is bin/Release/test with size 14.03 KB
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

VERSION INFO:- CODEBLOCKS:- Release 17.12  rev 11256 Sep  3 2018, 07:29:05 - wx3.0.4 (Linux, unicode) - 64 bit
                                 GCC:-gcc (GCC) 8.2.1 20180831
                                 LINUX:-4.18.9-arch1-1-ARCH
« Last Edit: November 27, 2018, 07:39:18 am by Gobtotem »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2105
Re: Codeblocks showing wrong output with memmove().
« Reply #1 on: November 27, 2018, 09:10:05 am »
Quote
So i ran the below code on codeblocks
No, you run it on your operating system. Codeblocks is only a editor, it can not run, or compile code...
Codeblocks does not influence the output of your program.

So my suggestion is that you have simply not build the code and run a older version of your application? Remember, the green arrow only runs the program and does not check if it has to be build first!
Have you tried Build->Rebuild?






Offline Gobtotem

  • Single posting newcomer
  • *
  • Posts: 3
Re: Codeblocks showing wrong output with memmove().
« Reply #2 on: November 27, 2018, 09:34:44 am »
So my suggestion is that you have simply not build the code and run a older version of your application? Remember, the green arrow only runs the program and does not check if it has to be build first!
Have you tried Build->Rebuild?
I guess it is cause of the O2 optimistion as i asked someone else too and they are able to reproduce it because of the O2 flag in gcc 8.x and 9 . So dont know much i guess it is cause of gcc and not codeblocks i guess. I tried running it in on my linux terminal with gcc -Wall -O2 -Wfatal-errors -Wextra -Wall -std=c11 test. c and even it shows 11234 now and without O2 flag it showed the correct output.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2105
Re: Codeblocks showing wrong output with memmove().
« Reply #3 on: November 27, 2018, 11:15:52 am »
Thank you for reporting back!

Offline Krice

  • Multiple posting newcomer
  • *
  • Posts: 77
Re: Codeblocks showing wrong output with memmove().
« Reply #4 on: November 28, 2018, 01:58:19 pm »
There is no need to write code like that, because if it sounds like undefined behavior, it's going to be it at least with compiler optimizations. I don't get what is the point trying funny stuff like that. If you want to find UB features in compiler you will get what you want. But if you want to write working programs you don't use memmove or anything like that (C memory "management" functions) at all and then continue with programming as usual.

Offline Gobtotem

  • Single posting newcomer
  • *
  • Posts: 3
Re: Codeblocks showing wrong output with memmove().
« Reply #5 on: November 28, 2018, 06:06:51 pm »
There is no need to write code like that, because if it sounds like undefined behavior, it's going to be it at least with compiler optimizations.
Don't know what you think is the undefined behavior or else but the bug has been confirmed by gcc and even fixed by gcc.
Here is the bug report if you want to take a look btw:- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88223

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2105
Re: Codeblocks showing wrong output with memmove().
« Reply #6 on: November 28, 2018, 11:07:47 pm »
Quote
There is no need to write code like that, because if it sounds like undefined behavior
from c++ reference:
Quote
Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
So this operation should be fine...

Offline Krice

  • Multiple posting newcomer
  • *
  • Posts: 77
Re: Codeblocks showing wrong output with memmove().
« Reply #7 on: November 29, 2018, 10:58:19 pm »
The point is that when you try to do funny stuff then random things happen. On the other hand if you write working code then the programs could also work as they should.

Offline raynebc

  • Almost regular
  • **
  • Posts: 161
Re: Codeblocks showing wrong output with memmove().
« Reply #8 on: November 30, 2018, 06:14:47 pm »
The better point is memmove was not following its clearly defined behavior.  If a function doesn't work as documented, it can't be counted on to be used.