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


Groups > comp.lang.c > #142221

Re: heap memory indexed as an array

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: heap memory indexed as an array
Date 2019-03-21 22:35 -0700
Organization A noiseless patient Spider
Message-ID <861s2zpj3k.fsf@mailhub.linuxsc.com> (permalink)
References <5b6ce6dd-178a-4fb6-9d80-af5349c0cec2@googlegroups.com> <1bk1gu8bdp.fsf@pfeifferfamily.net> <aDpYuPXgbPvO0TB2QttltgH9QFlBQ@bongo-ra.co>

Show all headers | View raw


Spiros Bousbouras <spibou@gmail.com> writes:

> On Tue, 19 Mar 2019 21:37:06 -0600
> Joe Pfeiffer <pfeiffer@cs.nmsu.edu> wrote:
>
>> sinbad <sinbad.sinbad@gmail.com> writes:
>>
>>> Is it safe to use heap memory as an array?
>>>
>>> #include <stdio.h>
>>> #include <string.h>
>>> #include <stdlib.h>
>>>
>>> typedef struct cell {
>>>     char name[5];
>>>     int y;
>>> } cell_t;
>>>
>>> #define MAX_CELLS 5
>>>
>>> int main() {
>>>     int i;
>>>     cell_t *p;
>>>
>>>     p = calloc(MAX_CELLS, sizeof(cell_t));
>
>     if (p == 0) return EXIT_FAILURE ;
>
>>>     for (i = 0; i < MAX_CELLS; i++) {
>>>         strcpy(p[i * sizeof(cell_t)].name, "test");
>>>         p[i * sizeof(cell_t)].y = i;
>>>     }
>>>
>>>     for (i = 0; i < MAX_CELLS; i++) {
>>>         printf("%s %u\n", p[i * sizeof(cell_t)].name,
>>>                p[i*sizeof(cell_t)].y);
>>>     }
>>>
>>>     free(p);
>>>     return 0;
>>>
>>> }
>>
>> Not only is it OK to use dynamically allocated memory with an index
>> ("like an array"), it's easier than your code indicates.
>>
>> You don't need to multiply the index by the cell size -- the compiler
>> does that for you.
>>
>>          printf("%s %u\n", p[i].name,
>>
>> will work in your printf().
>
> That depends on what the full  printf() invocation says.  The obvious
> extrapolation from the original code would be
>     printf("%s %u\n", p[i].name, p[i].y) ;
>
> but this is undefined behaviour because  p[i].y  has type  int  whereas
> %u  is for  unsigned int.

In fact the behavior is defined in this case, because the p[i].y
values are all small and non-negative.  The rules for variadic
arguments say it's okay to pass int for unsigned int, and vice
versa, as long as the value is in the common set of values for
the two types.  (The same applies to long and unsigned long,
etc.)  As a matter of style having the format specification match
the actual type is better of course, but the behavior in this
particular case is well-defined despite the signed/unsigned
mixing.

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


Thread

heap memory indexed as an array sinbad <sinbad.sinbad@gmail.com> - 2019-03-19 20:05 -0700
  Re: heap memory indexed as an array Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-03-19 21:37 -0600
    Re: heap memory indexed as an array Spiros Bousbouras <spibou@gmail.com> - 2019-03-20 04:40 +0000
      Re: heap memory indexed as an array nobody@example.org (Scott) - 2019-03-20 15:56 +0000
        Re: heap memory indexed as an array scott@slp53.sl.home (Scott Lurndal) - 2019-03-20 16:43 +0000
          Re: heap memory indexed as an array fir <profesor.fir@gmail.com> - 2019-03-20 10:00 -0700
          Re: heap memory indexed as an array William Ahern <william@25thandClement.com> - 2019-03-20 15:07 -0700
            Re: heap memory indexed as an array fir is a sexy shemale <cdalten@gmail.com> - 2019-03-20 23:52 -0700
          Re: heap memory indexed as an array nobody@example.org (Scott) - 2019-03-21 16:50 +0000
            Re: heap memory indexed as an array scott@slp53.sl.home (Scott Lurndal) - 2019-03-21 17:23 +0000
              Re: heap memory indexed as an array nobody@example.org (Scott) - 2019-03-22 04:29 +0000
                OT: crashing malloc [was Re: heap memory indexed as an array] nobody@example.org (Scott) - 2019-03-23 15:27 +0000
                Re: heap memory indexed as an array Spiros Bousbouras <spibou@gmail.com> - 2019-03-23 19:24 +0000
                Re: heap memory indexed as an array Melzzzzz <Melzzzzz@zzzzz.com> - 2019-03-23 20:20 +0000
      Re: heap memory indexed as an array Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-03-21 22:35 -0700
  Re: heap memory indexed as an array Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-03-20 07:05 +0000
  Re: heap memory indexed as an array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-03-20 12:12 +0000

csiph-web