Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #106041

Re: Two C code questions

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Two C code questions
Date 2017-03-14 08:53 -0700
Organization None to speak of
Message-ID <lntw6vkg6v.fsf@kst-u.example.com> (permalink)
References <oa7oru$rkf$1@dont-email.me>

Show all headers | View raw


MehdiAmini <atorrses@gmail.com> writes:
> // Clang version 3.8.0
>
> // Question 1:
> #include <stdio.h>
> #include <stdlib.h>
> #include <inttypes.h>
>
> void my_alloc( void *, size_t );
>
> int main(void)
> {
>      unsigned *u_ptr = NULL;
>      //unsigned *u_ptr; // Why using this instead of above line causes 
> core dump?
>
>      my_alloc(u_ptr,sizeof(*u_ptr));
>
>      free(u_ptr);
>      return EXIT_SUCCESS;
> }

Function arguments are passed by value, always.

You are passing the *value* of u_ptr to my_alloc.  If you don't
initialize it, that value is garbage.  It turns out that probably
doesn't matter, because my_alloc never uses the value you passed in.

Since a function cannot change the value of u_ptr, you're passing a
garbage value to free().  That's what causes the core dump.

> void my_alloc( void *v_ptr, size_t size )
> {

Here v_ptr hold a copy of the (garbage) value of u_ptr.

>      v_ptr = malloc(size);

Here you overwrite that garbage value with the value returned by
malloc.

>      if( v_ptr == NULL )
>      {
>          fprintf(stderr,"Memory allocation failed.\n");
>          exit(EXIT_FAILURE);
>      }
>
>      return ;

And here that value is discarded, since v_ptr is local to your function.
(The return statement is unnecesary but harmless.)

> }
>
> // Question 2:
> #include <stdio.h>
> #include <stdlib.h>
> #include <inttypes.h>
>
> #define N 6
> //static const long N = 6; // Why using this instead of above line 
> causes error?

It would have been helpful if you had included the text of the error
message.  Using gcc, I get:

    error: variable-sized object may not be initialized

When you define
    static const long N = 6;
you're *not* creating a compile-time constant.  You're (at least
logically) creating an object named N, with an initial value of 6.  All
"const" does is forbid any attempt to modify it.  ("const" doesn't mean
"constant", it means "read-only").

(In C++, N would be a compile-time constant.  Even in C, most compilers
will treat it as one, but that's an optimization that doesn't affect
what you can legally do with it.)

> int main(void)
> {
>      uint64_t ui[N] = {0};

Since N is not a constant, ui is a VLA (variable length array), and the
language rules don't allow an initializer for a VLA.  (Allowing
initializers would require the compiler to generate code to figure out
*at run time* how many elements to initialize.)

>      return EXIT_SUCCESS;
> }

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Two C code questions MehdiAmini <atorrses@gmail.com> - 2017-03-14 07:15 +0330
  Re: Two C code questions Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-03-13 22:13 -0600
  Re: Two C code questions Ian Collins <ian-news@hotmail.com> - 2017-03-14 17:16 +1300
  Re: Two C code questions Ike Naar <ike@iceland.freeshell.org> - 2017-03-14 07:20 +0000
    Re: Two C code questions MehdiAmini <atorrses@gmail.com> - 2017-03-14 12:01 +0330
      Re: Two C code questions Ike Naar <ike@iceland.freeshell.org> - 2017-03-14 19:23 +0000
        Re: Two C code questions Keith Thompson <kst-u@mib.org> - 2017-03-14 12:40 -0700
  Re: Two C code questions scott@slp53.sl.home (Scott Lurndal) - 2017-03-14 12:44 +0000
  Re: Two C code questions Keith Thompson <kst-u@mib.org> - 2017-03-14 08:53 -0700
    Re: Two C code questions MehdiAmini <atorrses@gmail.com> - 2017-03-15 14:05 +0330
  Re: Two C code questions John Bode <jfbode1029@gmail.com> - 2017-03-14 10:38 -0700
    Re: Two C code questions Keith Thompson <kst-u@mib.org> - 2017-03-14 11:49 -0700
      Re: Two C code questions John Bode <jfbode1029@gmail.com> - 2017-03-14 14:14 -0700

csiph-web