Linux RAID subsystem development
 help / color / mirror / Atom feed
* [OT] C programming problem
@ 2018-02-07 19:46 Wol's lists
  2018-02-07 20:00 ` Linus Lüssing
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Wol's lists @ 2018-02-07 19:46 UTC (permalink / raw)
  To: mdraid

It is for raid, honest :-)

My C is so rusty it's jammed solid :-) but I'm trying to prototype a 
simple algorithm for "de-clustering" a raid-60/61 array.

Googling for my error -

main.c:20:5: warning: incompatible implicit declaration of built-in 
function ‘memset’ [enabled by default]
      memset( &array, 0, blocks * sizeof(int) );

says "you need to include stdlib.h" - but I have!

(I'm getting the same error on the malloc ...)

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

void main()
{
     int disks, logdisks, mirrors, prime;

     printf( "%s", "Enter the number of disks ");
     scanf( "%d", &disks);
     printf( "%s", "Enter the number of logical disks ");
     scanf( "%d", &logdisks);
     printf( "%s", "Enter the number of mirrors ");
     scanf( "%d", &mirrors);
     printf( "%s", "Enter the prime ");
     scanf( "%d", &prime);

     int blocks, *array;

     blocks = logdisks * mirrors * disks;
     array = (int *) malloc( sizeof(int) * blocks );
     memset( &array, 0, blocks * sizeof(int) );

     int i;

     for (i=0; i < blocks; i++) {
         array[i] = (i * prime) % blocks;
     }

     for (i=0; i < blocks; i++) {
         if ( (i % disks) == 0)
             printf( "\n");
         printf( "%4d", array[i]);
     }
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [OT] C programming problem
  2018-02-07 19:46 [OT] C programming problem Wol's lists
@ 2018-02-07 20:00 ` Linus Lüssing
  2018-02-07 20:00 ` Michael Fritscher
  2018-02-07 22:43 ` Phil Turmel
  2 siblings, 0 replies; 6+ messages in thread
From: Linus Lüssing @ 2018-02-07 20:00 UTC (permalink / raw)
  To: Wol's lists; +Cc: mdraid

On Wed, Feb 07, 2018 at 07:46:13PM +0000, Wol's lists wrote:
> It is for raid, honest :-)
> 
> My C is so rusty it's jammed solid :-) but I'm trying to prototype a simple
> algorithm for "de-clustering" a raid-60/61 array.
> 
> Googling for my error -
> 
> main.c:20:5: warning: incompatible implicit declaration of built-in function
> ‘memset’ [enabled by default]
>      memset( &array, 0, blocks * sizeof(int) );
> 
> says "you need to include stdlib.h" - but I have!

You need to include string.h. See "man memset" (if your distro
has those).

I tried your code and I didn't get a warning or error for
malloc(). stdlib.h should be fine for malloc().

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [OT] C programming problem
  2018-02-07 19:46 [OT] C programming problem Wol's lists
  2018-02-07 20:00 ` Linus Lüssing
@ 2018-02-07 20:00 ` Michael Fritscher
  2018-02-07 20:22   ` Wol's lists
  2018-02-07 22:43 ` Phil Turmel
  2 siblings, 1 reply; 6+ messages in thread
From: Michael Fritscher @ 2018-02-07 20:00 UTC (permalink / raw)
  To: linux-raid

Hello,

> main.c:20:5: warning: incompatible implicit declaration of built-in
> function ‘memset’ [enabled by default]
>     memset( &array, 0, blocks * sizeof(int) );

at least my compiler says something about
> a.c:21:5: note: include ‘<string.h>’ or provide a declaration of ‘memset’

And yes, the first recipe does indeed help :-P

Best regards,
Michael Fritscher

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [OT] C programming problem
  2018-02-07 20:00 ` Michael Fritscher
@ 2018-02-07 20:22   ` Wol's lists
  0 siblings, 0 replies; 6+ messages in thread
From: Wol's lists @ 2018-02-07 20:22 UTC (permalink / raw)
  To: Michael Fritscher, linux-raid

On 07/02/18 20:00, Michael Fritscher wrote:
> Hello,
> 
>> main.c:20:5: warning: incompatible implicit declaration of built-in
>> function ‘memset’ [enabled by default]
>>      memset( &array, 0, blocks * sizeof(int) );
> 
> at least my compiler says something about
>> a.c:21:5: note: include ‘<string.h>’ or provide a declaration of ‘memset’
> 
> And yes, the first recipe does indeed help :-P
> 
Thank you both! Perfect!

I think actually stdlib had fixed the malloc problem, but I missed it 
because the memset problem was still there.

Cheers,
Wol

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [OT] C programming problem
  2018-02-07 19:46 [OT] C programming problem Wol's lists
  2018-02-07 20:00 ` Linus Lüssing
  2018-02-07 20:00 ` Michael Fritscher
@ 2018-02-07 22:43 ` Phil Turmel
  2018-02-07 23:32   ` Wol's lists
  2 siblings, 1 reply; 6+ messages in thread
From: Phil Turmel @ 2018-02-07 22:43 UTC (permalink / raw)
  To: Wol's lists, mdraid

Hi Wol,

Bug alert!

On 02/07/2018 02:46 PM, Wol's lists wrote:
> void main()
> {

>     blocks = logdisks * mirrors * disks;
>     array = (int *) malloc( sizeof(int) * blocks );
>     memset( &array, 0, blocks * sizeof(int) );
              ^^^^^^

Your 'array' variable is already a pointer.  You should not have
another address-of operator in front of it.  You are overwriting
your pointer and a bunch of static memory right after it, not
zeroing the area pointed to by 'array'.

Phil

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [OT] C programming problem
  2018-02-07 22:43 ` Phil Turmel
@ 2018-02-07 23:32   ` Wol's lists
  0 siblings, 0 replies; 6+ messages in thread
From: Wol's lists @ 2018-02-07 23:32 UTC (permalink / raw)
  To: Phil Turmel, mdraid

On 07/02/18 22:43, Phil Turmel wrote:
> Hi Wol,
> 
> Bug alert!
> 
> On 02/07/2018 02:46 PM, Wol's lists wrote:
>> void main()
>> {
> 
>>      blocks = logdisks * mirrors * disks;
>>      array = (int *) malloc( sizeof(int) * blocks );
>>      memset( &array, 0, blocks * sizeof(int) );
>                ^^^^^^
> 
> Your 'array' variable is already a pointer.  You should not have
> another address-of operator in front of it.  You are overwriting
> your pointer and a bunch of static memory right after it, not
> zeroing the area pointed to by 'array'.
> 
Thanks. Somebody else already pointed out the error, but he was rather 
more "teacher", didn't tell me what exactly it was, and let me find it 
for myself!

Stupid mistake, but as I said, my C-fu is rusted solid :-)

Anyways, I've now got it sorted - I'll probably be posting it to the 
list as an RFC very soon ...

Cheers,
Wol

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-02-07 23:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-07 19:46 [OT] C programming problem Wol's lists
2018-02-07 20:00 ` Linus Lüssing
2018-02-07 20:00 ` Michael Fritscher
2018-02-07 20:22   ` Wol's lists
2018-02-07 22:43 ` Phil Turmel
2018-02-07 23:32   ` Wol's lists

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox