-3
#include<stdio.h>
#include<conio.h>
int main()
{
 int b=5;
 int *a;
 a=(int *)malloc(1*sizeof(int));
 *a=b;
 printf("\nb=%d\n*a=%d",b,*a);
 printf("\nEnter a:");
 scanf("%d",a);
 getch();
 printf("\nb=%d\n\n*a=%d",b,*a);
 return 0;
}

The Above code never gets exeuted past the point of input of 'a'. It takes the input value but crashes and I am unable to figure it how to correct it. Please Help!

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • What do you mean it doesn't get executed? It faults? – Fiddling Bits Apr 26 '21 at 19:41
  • yes it does displaying the eror that the file has stopped working. – Gaurav Shukla Apr 26 '21 at 19:44
  • 1
    is "dev c++" an IDE? Are you compiling this as C or as C++? I think this is either C or C++, but not both – 463035818_is_not_an_ai Apr 26 '21 at 19:45
  • 1
    Dev-C++ is a garbage IDE for C++, yes. – sweenish Apr 26 '21 at 19:46
  • Dev c++ is a lightwieght IDE. The code is in C – Gaurav Shukla Apr 26 '21 at 19:47
  • 1
    Are you allowed to use a debugger (such as [GDB](https://www.gnu.org/software/gdb/)...)? Can you compile your code with some variant of [GCC](http://gcc.gnu.org/) invoked as `gcc -Wall -Wextra -g` to get all warnings and debug info? What are the warnings? Why don't you call [fflush](https://en.cppreference.com/w/c/io/fflush) in your C program before the call to `getch`? Did you read a good book on C programming, like [*Modern C*](https://modernc.gforge.inria.fr/)? – Basile Starynkevitch Apr 26 '21 at 19:47
  • 1
    Why are you trying to read an integer into a char array? Your compiler should have warned you about this. – klutt Apr 26 '21 at 19:51
  • Did you read a recent C standard, such as [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) or better? – Basile Starynkevitch Apr 26 '21 at 19:53
  • no using flush did'nt change anything – Gaurav Shukla Apr 26 '21 at 19:55
  • no i have not read recent C standards – Gaurav Shukla Apr 26 '21 at 19:55
  • And using a debugger did not help you? You need to spend hours in reading the documentation of your compiler and of your debugger! You could take inspiration from the source code of existing open source programs, such as [GNU bash](https://www.gnu.org/software/bash/), [GNU bison](https://www.gnu.org/software/bison/) – Basile Starynkevitch Apr 26 '21 at 19:55
  • Can u recommend some ? – Gaurav Shukla Apr 26 '21 at 19:56
  • "Recommend some" what? [*Modern C*](https://modernc.gforge.inria.fr/) is a book. There are other books about C programming. – Basile Starynkevitch Apr 26 '21 at 19:57
  • 2
    You are _not_ doing `#include ` to get a declaration for `malloc`. So, the compiler will assume `malloc` is (e.g.) `int malloc()` [vs. `void *malloc()`]. You are probably on a 64 bit system. The return value from `malloc` will probably get truncated to 32 bits. When I add the `#include` and run locally, it runs to completion. – Craig Estey Apr 26 '21 at 19:58
  • i m merely a beginner and i often get stuck into problems like these , i am still learning my way around – Gaurav Shukla Apr 26 '21 at 19:58
  • 1
    First thing you should do is to ALWAYS compile with AT LEAST the flags `-Wall -Wextra`. And then I'd recommend trying to get the basics down properly. Sorry to say it, but this code is just like firing from the hip and hoping for the best. And that's not a good way to learn C. – klutt Apr 26 '21 at 19:59
  • Are you allowed to use some static source code analyzer like [Clang static analyzer](https://clang-analyzer.llvm.org/) ? – Basile Starynkevitch Apr 26 '21 at 20:00
  • thanks i will be sure to work on my basics a bit more – Gaurav Shukla Apr 26 '21 at 20:01

1 Answers1

3

First of all, you’re using a really old DOS header - conio.h, which is incompatible with most modern compilers. You can remove this line, but make sure to remove the call to getch() as well:

// #include <conio.h>
...
// getch();

Secondly, malloc is defined in the stdlib.h header. Without it, the compiler is declaring the malloc function implicitly, with default type int:

int malloc(int)

But the correct declaration is

void *malloc(size_t)

Int is by definition a 32-bit type, so the result of such implicit malloc is interpreted differently. Both void* and size_t on modern machines are 64-bit by default. The return value of malloc in your program is cut and only half of it is stored, resulting in an invalid pointer and a crash.