-1

On xcode c project, the code:

#include <stdio.h>

int main()
{

    int* pi = (int*) malloc(sizeof(int));
    *pi = 5;
    printf("*pi: %d\n", *pi);
    free(pi);



    return 0;
}

prints 't' , instead of 5, although I explicitly included the %specifier, that according to spec should print the signed decimal integer. I would expect the '%c' specifier would print that. What is going on?

enter image description here

Draif Kroneg
  • 743
  • 13
  • 34
  • I cannot spot any mistake making the program undefined, implementation-defined, or semantically wrong. Except maybe the cast of the `malloc` return value. – cadaniluk Dec 27 '15 at 21:08
  • 1
    But note that Pi does not equal `5` for real and is not an integer. ;-) – cadaniluk Dec 27 '15 at 21:09
  • I use standard unmodified unaltered Xcode, this is my first project and almost freshly installed el capitan as OS. No inclusion of any libraries. – Draif Kroneg Dec 27 '15 at 21:10
  • 2
    running correct on ubuntu x86 - output is "*pi: 5" - please recompile, maybe you have old code. – recycler Dec 27 '15 at 21:11
  • 2
    http://ideone.com/O9qE6O Runs fine here as well. – user007 Dec 27 '15 at 21:11
  • 1
    Try adding `#include `. – kaylum Dec 27 '15 at 21:11
  • I am running it on 2015 macbook pro retina, i did rerun in several times, and even rebooted – Draif Kroneg Dec 27 '15 at 21:11
  • @DraifKroneg; I too ran on 2015 macbook pro retina display and its output display `5`. – haccks Dec 27 '15 at 21:13
  • including did not help, it is still t – Draif Kroneg Dec 27 '15 at 21:13
  • 1
    Add an additional printf to the code. If you don't see it, you're not running the program you think you are. – user5071535 Dec 27 '15 at 21:14
  • i do not have any other code - this is my first project in Xcode – Draif Kroneg Dec 27 '15 at 21:18
  • 7
    But the output in the screen shot doesn't seem to match the code. Output shows "Hello World" but there is no such print in the code. And code has "hi" which is not shown in the output. So it appears it could be an IDE issue rather than a code issue. – kaylum Dec 27 '15 at 21:22
  • right click on your file that contains your desired code to input and you should be able to see a run option. click it and see. IDE usually start off with a hello world script to start of the user and check correct compilation. – learningbyexample Dec 27 '15 at 23:25
  • in C, do not cast the value returned from `malloc()` as that returned value has the type `void*` so can be assigned to any other pointer. Casting just clutters the code, making it more difficult to understand, debug, maintain. – user3629249 Dec 28 '15 at 08:36
  • the posted code fails to compile. The compiler will output a message about the implicit declaration of function malloc() and function free(). this statement: `#include ` needs to be added. after correcting the code, the function outputs: `*pi: 5` – user3629249 Dec 28 '15 at 08:38

1 Answers1

2

The C dynamic memory allocation functions are defined in stdlib.h header.

If you add the 'standard library' to your code, it should run without any errors.

Also, malloc() returns a null pointer when it fails to allocate the space requested. So, you should make sure that what you received is not NULL by using a simple if-else block.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int* pi = (int*) malloc(sizeof(int));

    if(pi==NULL){
       printf("Malloc Failed!");
       return -1;
    }
    else{
        *pi = 5;
        printf("*pi: %d\n", *pi);
        free(pi);
        return 0;
    }

}

The code block above gives the intended result *pi: 5.

Hope this helps,

Reference: https://en.wikipedia.org/wiki/C_dynamic_memory_allocation

Berk Soysal
  • 2,356
  • 1
  • 18
  • 17