Re: Why this code is working



jammie_linux@xxxxxxxxx wrote:
Simon wrote :
"String literals always have static duration, no matter
where you declare them."

Is the same is also true when you allocate memory via malloc and other
related function

Similar. The memory block allocated by the 'malloc', 'calloc' or 'realloc' functions will remain valid until the base address of the block is passed to the 'free' function. It doesn't matter whether the pointer gets passed to or from functions.


> like in the case below:
Also, in this code the getnode() is not returning anything explicitly(
even though it should), still it is implicitly returning the value of
node * q defined locally in it.

It's not implicitly returning anything. The code is wrong, and will not compile.


Is this what the standard says or is
happening at the mercy of the compiler?

After you fix the errors below, the code is valid according to the standard. It should compile and run on all C compilers.


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

typedef struct node {
  int data;
  struct node * next;
 }node;

node * getnode()
{
  node * q = malloc(sizeof(node));
  if(q == NULL)
   {
    printf("Insufficient memory\n");
    exit(1);

The exit code 1 is not defined by the C standard, and could have unintended effects. If you want to indicate that the program failed, you should write
exit(EXIT_FAILURE);


   }

  q->data = 9;
  q->next = NULL;

You should have the line return q; here.

}

void display(node *q)
{
  if(q == NULL)
   {
    printf("q is NULL in display\n");
    exit(1);
    }
   printf("%d %p\n",q->data, (void*)q->next);
  return;
}


int main(void)
{
node * q;
q = getnode();
display(q);

You should have the line free(q); here.

  return 0 ;
}


Once you correct the errors shown above, this code is fine.

--
Simon.
.



Relevant Pages

  • Re: Dumb Anglos-fwd
    ... muscles onto a pen. ... All fragments will be novel historic tips. ... They send once, compile typically, then collect no matter how the ...
    (sci.crypt)
  • Re: 2 ways to initialize, whats the difference?
    ... other isn't but if both forms compile to identical IL then the argument is ... I'm not sure I'd characterize it as purely a matter of personal choice. ... receive the same attention as one with no spelling errors. ... ..Net languages, which one was likely to be used more often and finally which ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How do I use FormClose
    ... But you're creating instances of the child form in Main, ... If you're still trying to compile it, ... compiler message from the IDE while *compiling* the code. ... It really doesn't matter where it is. ...
    (comp.lang.pascal.delphi.misc)
  • Re: How do I use FormClose
    ... But you're creating instances of the child form in Main, ... If you're still trying to compile it, ... compiler message from the IDE while *compiling* the code. ... It really doesn't matter where it is. ...
    (alt.comp.lang.borland-delphi)
  • Re: A good compiler
    ... just to get it to compile a simple window. ... Thats a question you'd have to ask Microsoft. ... fairly quickly decided it would be more fruitful to build a linux box ... FOSS/Linux no matter what the question. ...
    (comp.lang.c)