Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #76081 > unrolled thread
| Started by | "Morten W. Petersen" <morphex@gmail.com> |
|---|---|
| First post | 2015-11-15 09:44 +0100 |
| Last post | 2015-11-20 16:20 -0500 |
| Articles | 20 on this page of 95 — 25 participants |
Back to article view | Back to comp.lang.c
Array manipulation techniques "Morten W. Petersen" <morphex@gmail.com> - 2015-11-15 09:44 +0100
Re: Array manipulation techniques Barry Schwarz <schwarzb@dqel.com> - 2015-11-15 01:56 -0800
Re: Array manipulation techniques Keith Thompson <kst-u@mib.org> - 2015-11-15 10:43 -0800
Re: Array manipulation techniques Chad <cdalten@gmail.com> - 2015-11-15 10:54 -0800
Re: Array manipulation techniques Ian Collins <ian-news@hotmail.com> - 2015-11-16 08:13 +1300
Re: Array manipulation techniques Chad <cdalten@gmail.com> - 2015-11-15 12:50 -0800
Re: Array manipulation techniques Les Cargill <lcargill99@comcast.com> - 2015-11-15 14:07 -0600
Re: Array manipulation techniques Barry Schwarz <schwarzb@dqel.com> - 2015-11-15 15:12 -0800
Re: Array manipulation techniques glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-15 09:57 +0000
Re: Array manipulation techniques Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-15 03:02 -0800
Array manipulation techniques asetofsymbols@gmail.com - 2015-11-15 12:02 -0800
Array manipulation techniques asetofsymbols@gmail.com - 2015-11-15 12:05 -0800
Array manipulation techniques asetofsymbols@gmail.com - 2015-11-15 12:28 -0800
Re: Array manipulation techniques Chad <cdalten@gmail.com> - 2015-11-15 12:48 -0800
Re: Array manipulation techniques Geoff <geoff@invalid.invalid> - 2015-11-15 17:05 -0800
Re: Array manipulation techniques Keith Thompson <kst-u@mib.org> - 2015-11-15 19:09 -0800
Re: Array manipulation techniques asetofsymbols@gmail.com - 2015-11-15 22:52 -0800
Re: Array manipulation techniques asetofsymbols@gmail.com - 2015-11-15 23:00 -0800
Re: Array manipulation techniques Richard Heathfield <rjh@cpax.org.uk> - 2015-11-16 07:17 +0000
Re: Array manipulation techniques asetofsymbols@gmail.com - 2015-11-15 23:41 -0800
Re: Array manipulation techniques Richard Heathfield <rjh@cpax.org.uk> - 2015-11-16 07:47 +0000
Re: Array manipulation techniques David Brown <david.brown@hesbynett.no> - 2015-11-16 09:51 +0100
Re: Array manipulation techniques Richard Heathfield <rjh@cpax.org.uk> - 2015-11-16 16:49 +0000
Re: Array manipulation techniques BartC <bc@freeuk.com> - 2015-11-16 11:10 +0000
Re: Array manipulation techniques Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-16 03:33 -0800
Re: Array manipulation techniques Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-16 13:09 +0000
Re: Array manipulation techniques Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-16 05:59 -0800
Re: Array manipulation techniques Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-16 15:22 +0000
Re: Array manipulation techniques Richard Heathfield <rjh@cpax.org.uk> - 2015-11-16 16:57 +0000
Re: Array manipulation techniques Keith Thompson <kst-u@mib.org> - 2015-11-16 10:29 -0800
Re: Array manipulation techniques David Brown <david.brown@hesbynett.no> - 2015-11-17 09:02 +0100
Re: Array manipulation techniques BartC <bc@freeuk.com> - 2015-11-17 10:46 +0000
Re: Array manipulation techniques Keith Thompson <kst-u@mib.org> - 2015-11-17 07:05 -0800
Re: Array manipulation techniques David Brown <david.brown@hesbynett.no> - 2015-11-17 16:47 +0100
Re: Array manipulation techniques supercat@casperkitty.com - 2015-11-17 12:33 -0800
Development Best Practices and C "nomenclature" Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-16 12:15 -0500
Re: Development Best Practices and C "nomenclature" Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-16 10:33 -0800
Re: Development Best Practices and C "nomenclature" asetofsymbols@gmail.com - 2015-11-16 10:51 -0800
Re: Development Best Practices and C "nomenclature" "Morten W. Petersen" <morphex@gmail.com> - 2015-11-16 20:14 +0100
Re: Development Best Practices and C "nomenclature" asetofsymbols@gmail.com - 2015-11-16 11:28 -0800
Re: Development Best Practices and C "nomenclature" "Morten W. Petersen" <morphex@gmail.com> - 2015-11-17 00:20 +0100
Re: Development Best Practices and C "nomenclature" David Brown <david.brown@hesbynett.no> - 2015-11-17 10:03 +0100
Re: Development Best Practices and C "nomenclature" Keith Thompson <kst-u@mib.org> - 2015-11-17 08:54 -0800
Re: Development Best Practices and C "nomenclature" glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-17 20:12 +0000
Re: Development Best Practices and C "nomenclature" Robert Wessel <robertwessel2@yahoo.com> - 2015-11-17 23:38 -0600
Re: Development Best Practices and C "nomenclature" glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-18 08:22 +0000
Re: Development Best Practices and C "nomenclature" Richard Heathfield <rjh@cpax.org.uk> - 2015-11-16 20:25 +0000
Re: Development Best Practices and C "nomenclature" Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-16 16:20 -0500
Re: Development Best Practices and C "nomenclature" Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-16 13:28 -0800
Re: Development Best Practices and C "nomenclature" Richard Heathfield <rjh@cpax.org.uk> - 2015-11-16 21:30 +0000
Re: Development Best Practices and C "nomenclature" asetofsymbols@gmail.com - 2015-11-16 14:38 -0800
Re: Development Best Practices and C "nomenclature" asetofsymbols@gmail.com - 2015-11-16 14:52 -0800
Re: Development Best Practices and C "nomenclature" asetofsymbols@gmail.com - 2015-11-17 22:51 -0800
Re: Development Best Practices and C "nomenclature" Richard Heathfield <rjh@cpax.org.uk> - 2015-11-18 06:59 +0000
Re: Development Best Practices and C "nomenclature" Richard Heathfield <rjh@cpax.org.uk> - 2015-11-16 20:24 +0000
Re: Development Best Practices and C "nomenclature" David Brown <david.brown@hesbynett.no> - 2015-11-17 09:55 +0100
Re: Development Best Practices and C "nomenclature" raltbos@xs4all.nl (Richard Bos) - 2015-11-17 23:00 +0000
Re: Development Best Practices and C "nomenclature" raltbos@xs4all.nl (Richard Bos) - 2015-11-17 22:55 +0000
Re: Development Best Practices and C "nomenclature" Ian Collins <ian-news@hotmail.com> - 2015-11-18 12:07 +1300
Re: Development Best Practices and C "nomenclature" James Kuyper <jameskuyper@verizon.net> - 2015-11-17 18:45 -0500
Re: Development Best Practices and C "nomenclature" David Brown <david.brown@hesbynett.no> - 2015-11-18 15:50 +0100
Re: Development Best Practices and C "nomenclature" Keith Thompson <kst-u@mib.org> - 2015-11-18 08:39 -0800
Re: Development Best Practices and C "nomenclature" Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-20 13:07 -0800
Re: Development Best Practices and C "nomenclature" Keith Thompson <kst-u@mib.org> - 2015-11-20 14:16 -0800
[OT] Re: Development Best Practices and C "nomenclature" Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-20 17:30 -0500
Re: [OT] Re: Development Best Practices and C "nomenclature" Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-20 19:29 -0800
Re: [OT] Re: Development Best Practices and C "nomenclature" raltbos@xs4all.nl (Richard Bos) - 2015-11-21 22:44 +0000
Re: Development Best Practices and C "nomenclature" jacobnavia <jacob@jacob.remcomp.fr> - 2015-11-22 00:09 +0100
Re: Development Best Practices and C "nomenclature" Keith Thompson <kst-u@mib.org> - 2015-11-21 15:57 -0800
Re: Development Best Practices and C "nomenclature" James Kuyper <jameskuyper@verizon.net> - 2015-11-21 20:49 -0500
Re: Development Best Practices and C "nomenclature" Rosario19 <Ros@invalid.invalid> - 2015-11-25 10:13 +0100
Re: Development Best Practices and C "nomenclature" "J. Clarke" <j.clarke.873638@gmail.com> - 2015-11-25 18:58 -0500
Re: Development Best Practices and C "nomenclature" Rosario19 <Ros@invalid.invalid> - 2015-11-26 12:13 +0100
Re: Development Best Practices and C "nomenclature" "J. Clarke" <j.clarke.873638@gmail.com> - 2015-11-26 07:22 -0500
Re: Development Best Practices and C "nomenclature" glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-22 14:51 +0000
Re: Development Best Practices and C "nomenclature" "Charles Richmond" <numerist@aquaporin4.com> - 2015-11-23 15:53 -0600
Re: Development Best Practices and C "nomenclature" Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-18 08:55 -0800
Re: Development Best Practices and C "nomenclature" Keith Thompson <kst-u@mib.org> - 2015-11-18 09:17 -0800
Re: Development Best Practices and C "nomenclature" James Kuyper <jameskuyper@verizon.net> - 2015-11-18 13:19 -0500
Re: Development Best Practices and C "nomenclature" raltbos@xs4all.nl (Richard Bos) - 2015-11-20 11:32 +0000
Re: Development Best Practices and C "nomenclature" Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-20 13:17 -0800
Re: Development Best Practices and C "nomenclature" glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-21 07:08 +0000
Re: Development Best Practices and C "nomenclature" Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-22 13:52 -0800
Re: Array manipulation techniques Geoff <geoff@invalid.invalid> - 2015-11-15 17:03 -0800
Re: Array manipulation techniques Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-15 15:09 -0500
Re: Array manipulation techniques Richard Heathfield <rjh@cpax.org.uk> - 2015-11-15 21:47 +0000
Re: Array manipulation techniques Geoff <geoff@invalid.invalid> - 2015-11-15 17:02 -0800
Re: Array manipulation techniques Keith Thompson <kst-u@mib.org> - 2015-11-16 10:44 -0800
Re: Array manipulation techniques asetofsymbols@gmail.com - 2015-11-16 11:13 -0800
Re: Array manipulation techniques Keith Thompson <kst-u@mib.org> - 2015-11-16 11:57 -0800
Re: Array manipulation techniques Ian Collins <ian-news@hotmail.com> - 2015-11-17 10:30 +1300
Re: Array manipulation techniques Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-17 08:11 -0500
Re: Array manipulation techniques BartC <bc@freeuk.com> - 2015-11-17 13:24 +0000
Re: Array manipulation techniques Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-20 13:09 -0800
Re: Array manipulation techniques Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-20 16:20 -0500
Page 1 of 5 [1] 2 3 4 5 Next page →
| From | "Morten W. Petersen" <morphex@gmail.com> |
|---|---|
| Date | 2015-11-15 09:44 +0100 |
| Subject | Array manipulation techniques |
| Message-ID | <n29glh$u6r$1@speranza.aioe.org> |
Hi there. I have an array of integers, where I need to replace one integer with a sequence of integers. For example, the array containing 1,2,3,1,2,3,1,2,3 needs to be expanded to contain 1,2,3,3,3,1,2,3,3,3,1,2,3,3,3, in other words, 3 needs to be replaced by 3,3,3. And the array has been dynamically allocated. Are there any standard functions that would do that, or do I need to implement something myself? -Morten
[toc] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-11-15 01:56 -0800 |
| Message-ID | <c7lg4bpu2d70gj8m8llk3l0e21joia7uvi@4ax.com> |
| In reply to | #76081 |
On Sun, 15 Nov 2015 09:44:38 +0100, "Morten W. Petersen"
<morphex@gmail.com> wrote:
>Hi there.
>
>I have an array of integers, where I need to replace one integer with a
>sequence of integers.
>
>For example, the array containing 1,2,3,1,2,3,1,2,3 needs to be expanded
>to contain 1,2,3,3,3,1,2,3,3,3,1,2,3,3,3, in other words, 3 needs to be
>replaced by 3,3,3. And the array has been dynamically allocated.
>
>Are there any standard functions that would do that, or do I need to
>implement something myself?
You have to do it yourself:
scan the original contents to determine the number of entries in
the result
realloc the array with enough memory for this quantity
set index_2 to the end of the result
loop index_1 from last original entry to 0
if the data at index_1 is key
store the desired number of entries, decrementing
index_2 for each
else
store the data from index_1 at index_2 and decrement
index_2
decrement index_1 and repeat loop
return address of new array
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-11-15 10:43 -0800 |
| Message-ID | <lnfv07m7bs.fsf@kst-u.example.com> |
| In reply to | #76082 |
Barry Schwarz <schwarzb@dqel.com> writes:
> On Sun, 15 Nov 2015 09:44:38 +0100, "Morten W. Petersen"
> <morphex@gmail.com> wrote:
>>I have an array of integers, where I need to replace one integer with a
>>sequence of integers.
>>
>>For example, the array containing 1,2,3,1,2,3,1,2,3 needs to be expanded
>>to contain 1,2,3,3,3,1,2,3,3,3,1,2,3,3,3, in other words, 3 needs to be
>>replaced by 3,3,3. And the array has been dynamically allocated.
>>
>>Are there any standard functions that would do that, or do I need to
>>implement something myself?
>
> You have to do it yourself:
> scan the original contents to determine the number of entries in
> the result
> realloc the array with enough memory for this quantity
> set index_2 to the end of the result
> loop index_1 from last original entry to 0
> if the data at index_1 is key
> store the desired number of entries, decrementing
> index_2 for each
> else
> store the data from index_1 at index_2 and decrement
> index_2
> decrement index_1 and repeat loop
> return address of new array
It's a bit easier if you just malloc() the new array and then traverse
the original array while setting entries in the new one. It means you
don't have to worry about clobbering data that you still need -- and
there are similar problems where just traversing the array in reverse
order isn't enough to avoid that.
malloc()ing the new array and then (if you no longer need it) free()ing
the original one is likely to do just about the same thing as realloc()
anyway.
--
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"
[toc] | [prev] | [next] | [standalone]
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2015-11-15 10:54 -0800 |
| Message-ID | <0f0b4c23-5c53-4b44-8654-be06249acc27@googlegroups.com> |
| In reply to | #76102 |
How can someone who claims to be working on a XML parser, like uhh... cannot figure out what appears to be an easy programming problem?! I say appears to be easy because I remember doing a similar programming problem in one of the lower division Computer Science classes at Stanford.
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2015-11-16 08:13 +1300 |
| Message-ID | <das3r5Fi0ttU14@mid.individual.net> |
| In reply to | #76104 |
Chad wrote: > How can someone who claims to be working on a XML parser, like uhh... > cannot figure out what appears to be an easy programming problem?! Just as someone who slags of others should know how to post? -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2015-11-15 12:50 -0800 |
| Message-ID | <4585c92c-63ed-449d-a15f-da85fc6f5f30@googlegroups.com> |
| In reply to | #76107 |
On Sunday, November 15, 2015 at 11:13:58 AM UTC-8, Ian Collins wrote: > Chad wrote: > > How can someone who claims to be working on a XML parser, like uhh... > > cannot figure out what appears to be an easy programming problem?! > > Just as someone who slags of others should know how to post? > U mad bro?
[toc] | [prev] | [next] | [standalone]
| From | Les Cargill <lcargill99@comcast.com> |
|---|---|
| Date | 2015-11-15 14:07 -0600 |
| Message-ID | <n2ao96$tg7$2@dont-email.me> |
| In reply to | #76104 |
Chad wrote: > How can someone who claims to be working on a XML parser, like uhh... > cannot figure out what appears to be an easy programming problem?! I > say appears to be easy because I remember doing a similar programming > problem in one of the lower division Computer Science classes at > Stanford. > Different people get here by different paths. Be patient with them; it'll be your turn soon enough. -- Les Cargill
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-11-15 15:12 -0800 |
| Message-ID | <324i4blmnsiqcb0f2sg81pn33ufk9551m3@4ax.com> |
| In reply to | #76102 |
On Sun, 15 Nov 2015 10:43:51 -0800, Keith Thompson <kst-u@mib.org> wrote: >Barry Schwarz <schwarzb@dqel.com> writes: >> On Sun, 15 Nov 2015 09:44:38 +0100, "Morten W. Petersen" >> <morphex@gmail.com> wrote: >>>I have an array of integers, where I need to replace one integer with a >>>sequence of integers. >>> >>>For example, the array containing 1,2,3,1,2,3,1,2,3 needs to be expanded >>>to contain 1,2,3,3,3,1,2,3,3,3,1,2,3,3,3, in other words, 3 needs to be >>>replaced by 3,3,3. And the array has been dynamically allocated. >>> >>>Are there any standard functions that would do that, or do I need to >>>implement something myself? >> >> You have to do it yourself: >> scan the original contents to determine the number of entries in >> the result >> realloc the array with enough memory for this quantity >> set index_2 to the end of the result >> loop index_1 from last original entry to 0 >> if the data at index_1 is key >> store the desired number of entries, decrementing >> index_2 for each >> else >> store the data from index_1 at index_2 and decrement >> index_2 >> decrement index_1 and repeat loop >> return address of new array > >It's a bit easier if you just malloc() the new array and then traverse >the original array while setting entries in the new one. It means you >don't have to worry about clobbering data that you still need -- and >there are similar problems where just traversing the array in reverse >order isn't enough to avoid that. Yes, there are SIMILAR problems where this is true. But for the task AT HAND the proposed approach will never overlay data you still need. >malloc()ing the new array and then (if you no longer need it) free()ing >the original one is likely to do just about the same thing as realloc() >anyway. If memory is fragmented and the array is large, there are times when realloc could succeed but malloc would not. -- Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | glen herrmannsfeldt <gah@ugcs.caltech.edu> |
|---|---|
| Date | 2015-11-15 09:57 +0000 |
| Message-ID | <n29ktc$71n$1@speranza.aioe.org> |
| In reply to | #76081 |
Morten W. Petersen <morphex@gmail.com> wrote: > I have an array of integers, where I need to replace one integer with a > sequence of integers. > For example, the array containing 1,2,3,1,2,3,1,2,3 needs to be expanded > to contain 1,2,3,3,3,1,2,3,3,3,1,2,3,3,3, in other words, 3 needs to be > replaced by 3,3,3. And the array has been dynamically allocated. > Are there any standard functions that would do that, or do I need to > implement something myself? You can use realloc() to reallocate to the new size. Otherwise, there is no standard C library routine to do the replacement you ask about. But is is pretty easy to write such loops in C. -- glen
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-11-15 03:02 -0800 |
| Message-ID | <c566ccec-e0df-420f-b6b9-b70a164c7756@googlegroups.com> |
| In reply to | #76081 |
On Sunday, November 15, 2015 at 8:44:48 AM UTC, Morten W. Petersen wrote: > Hi there. > > I have an array of integers, where I need to replace one integer with a > sequence of integers. > > For example, the array containing 1,2,3,1,2,3,1,2,3 needs to be expanded > to contain 1,2,3,3,3,1,2,3,3,3,1,2,3,3,3, in other words, 3 needs to be > replaced by 3,3,3. And the array has been dynamically allocated. > > Are there any standard functions that would do that, or do I need to > implement something myself? > No, you can't reasonably expect a standard function for a requirement like that. It's best to do two passes through the array, one to get the size, and one to write out the data. I write loops like that using i and j. i is the loop counter that steps through the array. j is the output data head location.
[toc] | [prev] | [next] | [standalone]
| From | asetofsymbols@gmail.com |
|---|---|
| Date | 2015-11-15 12:02 -0800 |
| Message-ID | <c8454ce2-02db-406c-bb59-3f8892555065@googlegroups.com> |
| In reply to | #76081 |
I don't know if it is right
#define R return
#define F for
u8* strExp(u8* a, i32 z)
{u32 i,j,cnt, v, r;
i32 k;
u8 *b;
if(!('2'<=z&&z<='9')||a==0) R 0;
v=z-'0'; --v;
F(i=0, cnt=0;k=a[i]; ++i, ++cnt)
if(k==z)
{cnt+=v;
if(cnt>0xFFFFFF) R 0;
}
b=malloc(cnt+4);
if(b==0) R 0;
F(i=0, j=0; (k=a[i])&&i<cnt; ++i)
{b[j++]=k;
if(k==z)
{F(r=0; r<v; ++r)
b[j++]=k;
}
}
R b;
}
...
b=strExp("12345337678", '3');
if(b==0) error();
....
free(b);
[toc] | [prev] | [next] | [standalone]
| From | asetofsymbols@gmail.com |
|---|---|
| Date | 2015-11-15 12:05 -0800 |
| Message-ID | <a426cfc7-125a-4bb8-899d-966967d7933c@googlegroups.com> |
| In reply to | #76110 |
I don't know if it is right
I forget to close with 0 the array
#define R return
#define F for
u8* strExp(u8* a, i32 z)
{u32 i,j,cnt, v, r;
i32 k;
u8 *b;
if(!('2'<=z&&z<='9')||a==0) R 0;
v=z-'0'; --v;
F(i=0, cnt=0;k=a[i]; ++i, ++cnt)
if(k==z)
{cnt+=v;
if(cnt>0xFFFFFF) R 0;
}
b=malloc(cnt+4);
if(b==0) R 0;
F(i=0, j=0; (k=a[i])&&i<cnt; ++i)
{b[j++]=k;
if(k==z)
{F(r=0; r<v; ++r)
b[j++]=k;
}
}
b[j]=0;
R b;
}
...
b=strExp("12345337678", '3');
if(b==0) error();
....
free(b);
[toc] | [prev] | [next] | [standalone]
| From | asetofsymbols@gmail.com |
|---|---|
| Date | 2015-11-15 12:28 -0800 |
| Message-ID | <b17e757e-d81b-480c-90a9-ae498778fda5@googlegroups.com> |
| In reply to | #76111 |
I don't know if it is right
#define R return
#define F for
u8* strExp(u8* a, i32 z, u32 v)
{u32 i,j,cnt, r;
i32 k;
u8 *b;
if(a==0||v<1) R 0;
--v;
F(i=0, cnt=0;k=a[i]; ++i, ++cnt)
if(k==z)
{cnt+=v;
if(cnt>0xFFFFFF) R 0;
}
b=malloc(cnt+4);
if(b==0) R 0;
F(i=0, j=0; (k=a[i])&&j<cnt; ++i)
{b[j++]=k;
if(k==z)
{F(r=0; r<v; ++r)
b[j++]=k;
}
}
b[j]=0;
R b;
}
...
b=strExp("12345337678", '3', 3);
if(b==0) error();
....
free(b);
[toc] | [prev] | [next] | [standalone]
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2015-11-15 12:48 -0800 |
| Message-ID | <1f852b99-5818-4987-b59a-cafc0e679d2f@googlegroups.com> |
| In reply to | #76113 |
On Sunday, November 15, 2015 at 12:28:18 PM UTC-8, asetof...@gmail.com wrote:
> I don't know if it is right
> #define R return
> #define F for
>
> u8* strExp(u8* a, i32 z, u32 v)
> {u32 i,j,cnt, r;
> i32 k;
> u8 *b;
>
> if(a==0||v<1) R 0;
> --v;
> F(i=0, cnt=0;k=a[i]; ++i, ++cnt)
> if(k==z)
> {cnt+=v;
> if(cnt>0xFFFFFF) R 0;
> }
> b=malloc(cnt+4);
> if(b==0) R 0;
> F(i=0, j=0; (k=a[i])&&j<cnt; ++i)
> {b[j++]=k;
> if(k==z)
> {F(r=0; r<v; ++r)
> b[j++]=k;
> }
> }
>
> b[j]=0;
> R b;
> }
>
> ...
> b=strExp("12345337678", '3', 3);
> if(b==0) error();
> ....
> free(b);
Horrible.
[toc] | [prev] | [next] | [standalone]
| From | Geoff <geoff@invalid.invalid> |
|---|---|
| Date | 2015-11-15 17:05 -0800 |
| Message-ID | <5sai4b15uitn38qkrbi06moqfiev00n0td@4ax.com> |
| In reply to | #76113 |
On Sun, 15 Nov 2015 12:28:02 -0800 (PST), asetofsymbols@gmail.com wrote: [snip] Still more bad code corrected yet again because the author can't discern how his own code is supposed to work and he still hasn't tested it. /Ignore/
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-11-15 19:09 -0800 |
| Message-ID | <lny4dyljx0.fsf@kst-u.example.com> |
| In reply to | #76148 |
Geoff <geoff@invalid.invalid> writes:
> On Sun, 15 Nov 2015 12:28:02 -0800 (PST), asetofsymbols@gmail.com
> wrote:
>
> [snip]
>
> Still more bad code corrected yet again because the author can't
> discern how his own code is supposed to work and he still hasn't
> tested it.
>
> /Ignore/
I do not think that word means what you think it means.
--
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"
[toc] | [prev] | [next] | [standalone]
| From | asetofsymbols@gmail.com |
|---|---|
| Date | 2015-11-15 22:52 -0800 |
| Message-ID | <ebff76d0-711d-4bf0-9094-3817f340d5d5@googlegroups.com> |
| In reply to | #76148 |
And what would be the error? In function strExt(u8*, i32, u32) if I remember well I debugged it now and as memory reservation and result it seems ok...
[toc] | [prev] | [next] | [standalone]
| From | asetofsymbols@gmail.com |
|---|---|
| Date | 2015-11-15 23:00 -0800 |
| Message-ID | <b74b1470-2a8e-400a-93db-975a6c6b379f@googlegroups.com> |
| In reply to | #76161 |
This seems right:
#define u8 unsigned char
#define i32 int
#define u32 unsigned int
#define R return
#define F for
u8* strExp(u8* a, i32 z, u32 v)
{u32 i,j,cnt, r;
i32 k;
u8 *b;
if(a==0||v<1) R 0;
--v;
F(i=0, cnt=0;k=a[i]; ++i, ++cnt)
if(k==z)
{cnt+=v;
if(cnt>0xFFFFFF) R 0;
}
b=(u8*)malloc(cnt+4);
if(b==0) R 0;
F(i=0, j=0; (k=a[i])&&j<cnt; ++i)
{b[j++]=k;
if(k==z)
{F(r=0; r<v; ++r)
b[j++]=k;
}
}
b[j]=0;
R b;
}
I change the line where there is
malloc for allow C++ compilation
and add some define:
Define on type can be wrong
and one has to find the right
size type in his machine if exist
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-11-16 07:17 +0000 |
| Message-ID | <n2bvpu$es0$1@dont-email.me> |
| In reply to | #76162 |
On 16/11/15 07:00, asetofsymbols@gmail.com wrote: > This seems right: > #define u8 unsigned char Already it isn't. If you want a new name for an existing type, use typedef, not #define. And pick a meaningful name. u8 is not a good synonym for unsigned char. -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | asetofsymbols@gmail.com |
|---|---|
| Date | 2015-11-15 23:41 -0800 |
| Message-ID | <363022b9-6feb-4216-8f91-5d2d0690f415@googlegroups.com> |
| In reply to | #76163 |
Richard Heathfield wrote: On 16/11/15 07:00, asetof...@gmail.com wrote: > This seems right: > #define u8 unsigned char Already it isn't. If you want a new name for an existing type, use typedef, not #define. And pick a meaningful name. u8 is not a good synonym for unsigned char _____ I in my code it appear to use few typedef and no enum But the name u8 is ok as name especially when one has to write the function definition: u8* f(i32 a, u32 b) it is better (as visual effects) than unsigned char* f(int a, unsigned b) It is better too if one has to use cast example: (u8*) If it is difficult handling with fixed size type, one know the size; It would be more difficult write right code without know the type size
[toc] | [prev] | [next] | [standalone]
Page 1 of 5 [1] 2 3 4 5 Next page →
Back to top | Article view | comp.lang.c
csiph-web