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


Groups > comp.lang.c > #147447 > unrolled thread

memcpy not memcpying?

Started byDFS <nospam@dfs.com>
First post2019-11-01 08:42 -0400
Last post2019-11-01 23:19 +0000
Articles 8 on this page of 88 — 19 participants

Back to article view | Back to comp.lang.c


Contents

  memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 08:42 -0400
    Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 08:49 -0400
      Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-01 09:24 -0400
        Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 09:46 -0400
          Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-01 09:49 -0400
          Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-01 10:19 -0700
            Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-01 21:14 +0100
            Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 17:16 -0400
              Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-01 22:42 +0000
                Re: memcpy not memcpying? Richard Damon <Richard@Damon-Family.org> - 2019-11-01 21:10 -0400
                  Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 11:36 +0000
              Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-02 02:15 -0700
              Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-02 13:04 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 13:43 +0000
                  Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-02 17:24 +0100
                    Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 17:00 +0000
                      Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-02 13:06 -0700
                        Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 21:05 +0000
                          Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 22:40 +0000
                          Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-02 15:59 -0700
                            Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 23:47 +0000
                              Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-02 18:22 -0700
                                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 13:27 +0000
                                  Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-03 17:46 +0100
                                    Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 17:20 +0000
                                      Re: memcpy not memcpying? Manfred <noname@add.invalid> - 2019-11-03 19:44 +0100
                                        Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 19:36 +0000
                                          Re: memcpy not memcpying? Manfred <noname@add.invalid> - 2019-11-03 22:57 +0100
                                          Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-03 22:39 +0000
                                      Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-03 23:06 +0100
                                        Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 23:28 +0000
                                          Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-04 11:00 +0100
                                            Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 11:14 +0000
                                            Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 11:22 +0000
                                              Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-04 13:10 +0100
                                              Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-04 10:03 -0800
                                                Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 19:36 +0000
                                                  Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-05 10:04 -0800
                                                Re: memcpy not memcpying? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-04 20:58 -0800
                                              Re: memcpy not memcpying? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-04 22:01 -0800
                                            Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-04 12:56 +0100
                                              Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-04 09:50 -0500
                                                Re: memcpy not memcpying? scott@slp53.sl.home (Scott Lurndal) - 2019-11-04 20:26 +0000
                                            Re: memcpy not memcpying? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-11-04 04:34 -0800
                                              Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 13:05 +0000
                                                Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 14:09 +0000
                                                  Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 15:11 +0000
                                                    Re: memcpy not memcpying? Melzzzzz <Melzzzzz@zzzzz.com> - 2019-11-04 15:15 +0000
                                                    Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 15:43 +0000
                                    Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 20:20 +0000
                              Re: memcpy not memcpying? Ian Collins <ian-news@hotmail.com> - 2019-11-03 14:37 +1300
                                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 11:01 +0000
                                  Re: memcpy not memcpying? Öö Tiib <ootiib@hot.ee> - 2019-11-03 04:03 -0800
                                    Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 12:52 +0000
                                      Re: memcpy not memcpying? Öö Tiib <ootiib@hot.ee> - 2019-11-03 23:34 -0800
                                        Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 09:36 +0000
                                          Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 16:20 +0000
                                  Re: memcpy not memcpying? Ian Collins <ian-news@hotmail.com> - 2019-11-04 07:46 +1300
                                    Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-03 12:26 -0800
                              Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-03 17:31 +0100
                                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 17:05 +0000
                Re: memcpy not memcpying? already5chosen@yahoo.com - 2019-11-03 09:14 -0800
                  Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-03 12:32 -0800
                    Re: memcpy not memcpying? already5chosen@yahoo.com - 2019-11-03 14:59 -0800
                      Re: memcpy not memcpying? already5chosen@yahoo.com - 2019-11-03 15:19 -0800
                        Re: memcpy not memcpying? already5chosen@yahoo.com - 2019-11-03 17:00 -0800
            Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-02 11:16 -0400
              Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-02 17:26 +0100
      Re: memcpy not memcpying? Barry Schwarz <schwarzb@delq.com> - 2019-11-01 06:39 -0700
        Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 09:56 -0400
          Re: memcpy not memcpying? Barry Schwarz <schwarzb@delq.com> - 2019-11-01 12:39 -0700
      Re: memcpy not memcpying? guinness.tony@gmail.com - 2019-11-01 06:49 -0700
      Re: memcpy not memcpying? jak <nospam@please.ty> - 2019-11-01 20:40 +0100
    Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 14:26 +0100
      Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-01 10:22 -0700
      Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-01 19:44 +0000
        Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 21:31 +0100
    Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-01 09:39 -0400
      Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 14:42 +0100
        Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-01 09:48 -0400
    Re: memcpy not memcpying? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-11-01 06:47 -0700
      Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 14:58 +0100
        Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 10:22 -0400
          Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 18:38 +0100
            Re: memcpy not memcpying? scott@slp53.sl.home (Scott Lurndal) - 2019-11-01 17:54 +0000
              Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 20:24 +0100
            Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 15:24 -0400
          Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-01 23:19 +0000

Page 5 of 5 — ← Prev page 1 2 3 4 [5]


#147455

FromMalcolm McLean <malcolm.arthur.mclean@gmail.com>
Date2019-11-01 06:47 -0700
Message-ID<2d44aa7d-1a88-4a9f-9191-e70d65f9dd53@googlegroups.com>
In reply to#147447
On Friday, 1 November 2019 12:42:49 UTC, DFS  wrote:
> Just trying to combine 2 arrays:
> ====================================================================
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> 
> int i = 0;
> 
> void combinearrays(int a, int b, char *x[], char *y[], char *z[]) {
> 	memcpy(z,   x, a);
> 	memcpy(z+a, y, b);
> }	
> 
> int main(int argc, char *argv[]) {
> 	char *x[] = {"adf1","fg7","Dxx","eff","Mnn","opp","zaa"};
> 	char *y[] = {"A","c","p","S","x"};
> 	int j = sizeof(x)/sizeof(x[0]);
> 	int k = sizeof(y)/sizeof(y[0]);
> 
>          char **z = malloc(j+k);
> 	
Malloc takes an amount of memory in bytes. So if you want to allocate
(j+k) char *s, you need to pass

         char **z = malloc((j+k)*sizeof(char *));

Your other mistake is similar. memcpy() also takes an amount of memory
in bytes.

[toc] | [prev] | [next] | [standalone]


#147461

FromAutist <autist69@gmail.com>
Date2019-11-01 14:58 +0100
Message-ID<qphdn1$hkn$1@news.albasani.net>
In reply to#147455
>> int main(int argc, char *argv[]) {
>> 	char *x[] = {"adf1","fg7","Dxx","eff","Mnn","opp","zaa"};
>> 	char *y[] = {"A","c","p","S","x"};
>> 	int j = sizeof(x)/sizeof(x[0]);
>> 	int k = sizeof(y)/sizeof(y[0]);
>>           char **z = malloc(j+k);
> Malloc takes an amount of memory in bytes. So if you want to allocate
> (j+k) char *s, you need to pass
> 
>           char **z = malloc((j+k)*sizeof(char *));

Or simpy do ...
char **z = malloc( sizeof(x) + sizeof(y) );

[toc] | [prev] | [next] | [standalone]


#147463

FromDFS <nospam@dfs.com>
Date2019-11-01 10:22 -0400
Message-ID<ffXuF.718$gt2.465@fx02.iad>
In reply to#147461
On 11/1/2019 9:58 AM, Autist wrote:
>>> int main(int argc, char *argv[]) {
>>>     char *x[] = {"adf1","fg7","Dxx","eff","Mnn","opp","zaa"};
>>>     char *y[] = {"A","c","p","S","x"};
>>>     int j = sizeof(x)/sizeof(x[0]);
>>>     int k = sizeof(y)/sizeof(y[0]);
>>>           char **z = malloc(j+k);
>> Malloc takes an amount of memory in bytes. So if you want to allocate
>> (j+k) char *s, you need to pass
>>
>>           char **z = malloc((j+k)*sizeof(char *));
> 
> Or simpy do ...
> char **z = malloc( sizeof(x) + sizeof(y) );


I replied to Barry Schwarz:

char **z  = malloc(sizeof j  + sizeof k);
char **z  = malloc(sizeof(j) + sizeof(k));

Neither work on my system (tcc on Windows)

this does, though:
char **z  = malloc(sizeof(char*) * (j+k));

[toc] | [prev] | [next] | [standalone]


#147477

FromAutist <autist69@gmail.com>
Date2019-11-01 18:38 +0100
Message-ID<qphqi7$fvb$1@news.albasani.net>
In reply to#147463
> char **z  = malloc(sizeof j  + sizeof k);
> char **z  = malloc(sizeof(j) + sizeof(k));
> 
> Neither work on my system (tcc on Windows)

Has your sourcefile a .cpp-extension? C++ is nearly fully compatible
to C (in Stroustrup's 2016 book "The C++ Language" these details cover
only 15 pages) but there are some minor details which are different.
One detail is that a void-pointer can be converted to any other
pointer-type in C without any casting but not in C++.
You could also try to install Visual Studio 2019 Commuity Edition.
The only downside of this compiler is that it isn't compatible to
C99 and beyond.

[toc] | [prev] | [next] | [standalone]


#147478

Fromscott@slp53.sl.home (Scott Lurndal)
Date2019-11-01 17:54 +0000
Message-ID<Sj_uF.924$P82.776@fx15.iad>
In reply to#147477
Autist <autist69@gmail.com> foolishly elided attributions (and useful body) to DFS's original posting

>> char **z  = malloc(sizeof j  + sizeof k);
>> char **z  = malloc(sizeof(j) + sizeof(k));
>> 
>> Neither work on my system (tcc on Windows)
>
>Has your sourcefile a .cpp-extension? C++ is nearly fully compatible

They don't work because i and j are declared as type int.  Even if
DFS was using a 32-bit system (where a pointer is the same size as
int), he'd need to multiply the size times the _values_ of i and j.

[toc] | [prev] | [next] | [standalone]


#147481

FromAutist <autist69@gmail.com>
Date2019-11-01 20:24 +0100
Message-ID<qpi0og$t9v$1@news.albasani.net>
In reply to#147478
>>> char **z  = malloc(sizeof j  + sizeof k);
>>> char **z  = malloc(sizeof(j) + sizeof(k));
>>> Neither work on my system (tcc on Windows)

>> Has your sourcefile a .cpp-extension? C++ is nearly fully compatible

> They don't work because i and j are declared as type int.  Even if
> DFS was using a 32-bit system (where a pointer is the same size as
> int), he'd need to multiply the size times the _values_ of i and j.

I just had a short look at the code and guessed that it was the
missing casting.

[toc] | [prev] | [next] | [standalone]


#147482

FromDFS <nospam@dfs.com>
Date2019-11-01 15:24 -0400
Message-ID<jE%uF.1938$fL4.1783@fx41.iad>
In reply to#147477
On 11/1/2019 1:38 PM, Autist wrote:
>> char **z  = malloc(sizeof j  + sizeof k);
>> char **z  = malloc(sizeof(j) + sizeof(k));
>>
>> Neither work on my system (tcc on Windows)
> 
> Has your sourcefile a .cpp-extension? 

No.

> C++ is nearly fully compatible
> to C (in Stroustrup's 2016 book "The C++ Language" these details cover
> only 15 pages) but there are some minor details which are different.
> One detail is that a void-pointer can be converted to any other
> pointer-type in C without any casting but not in C++.
> You could also try to install Visual Studio 2019 Commuity Edition.
> The only downside of this compiler is that it isn't compatible to
> C99 and beyond.

and prior?

[toc] | [prev] | [next] | [standalone]


#147494

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2019-11-01 23:19 +0000
Message-ID<87y2wzkwwf.fsf@bsb.me.uk>
In reply to#147463
DFS <nospam@dfs.com> writes:

> On 11/1/2019 9:58 AM, Autist wrote:
>>>> int main(int argc, char *argv[]) {
>>>>     char *x[] = {"adf1","fg7","Dxx","eff","Mnn","opp","zaa"};
>>>>     char *y[] = {"A","c","p","S","x"};
>>>>     int j = sizeof(x)/sizeof(x[0]);
>>>>     int k = sizeof(y)/sizeof(y[0]);
>>>>           char **z = malloc(j+k);
>>> Malloc takes an amount of memory in bytes. So if you want to allocate
>>> (j+k) char *s, you need to pass
>>>
>>>           char **z = malloc((j+k)*sizeof(char *));
>>
>> Or simpy do ...
>> char **z = malloc( sizeof(x) + sizeof(y) );
>
> I replied to Barry Schwarz:
>
> char **z  = malloc(sizeof j  + sizeof k);
> char **z  = malloc(sizeof(j) + sizeof(k));
>
> Neither work on my system (tcc on Windows)

Yes, they are both wrong (and they mean exactly the same thing), but
they are not what Autist suggested.

-- 
Ben.

[toc] | [prev] | [standalone]


Page 5 of 5 — ← Prev page 1 2 3 4 [5]

Back to top | Article view | comp.lang.c


csiph-web