9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: rog@vitanuova.com
To: 9fans@cse.psu.edu
Subject: Re: [9fans] realloc
Date: Wed,  2 Mar 2005 18:16:36 +0000	[thread overview]
Message-ID: <546fc93e2b9a8dae55875ff7e04830a7@vitanuova.com> (raw)
In-Reply-To: <1807515500.20050302192656@mail.ru>

> This code cause my problem...

i'm not surprised, i'm afraid. this is really horrible code.
you need to sort out your ideas about how C pointers work.
for instance, given that Text is an array of runes,

		*((Rune*)(Text+text_pos))=r;

is exactly equivalent to:

		Text[text_pos] = r;

i haven't time to work out exactly how you're going wrong,
(you *are* scribbling over the end of a malloc block)
but here's a likely candidate:

	memmove(Text+text_pos+2,Text+text_pos,text_count-text_pos);

you're using text_pos in two different ways here:
as an index into a array of Runes, and as a byte offset
as expected by memmove.

i *think* you're probably trying to write code something like:

	if(text_count >= msize(Text) / sizeof(Rune))
		Text = realloc(Text, (text_count+BLOCKSIZE)*sizeof(Rune));
	memmove(Text+textpos+1, Text+textpos, (text_count-text_pos)*sizeof(Rune));
	Text[text_pos] = r;
	text_pos++;
	text_count++;

which seems to work ok.

if you're going to write Plan 9 code, it would a good idea to look at
some existing Plan 9 code (try /sys/src/cmd, for starters) and copy
the style, as far as you can, in particular regarding naming
conventions and code layout.  there's loads of inspiration there.  it
*is* possible to write simple, understandable code.

  cheers,
    rog.



  reply	other threads:[~2005-03-02 18:16 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-26 17:37 Sergey Reva
2005-02-26 17:58 ` Sergey Reva
2005-02-26 18:05   ` Charles Forsyth
2005-02-26 19:18 ` Russ Cox
2005-03-02 17:26   ` Sergey Reva
2005-03-02 18:16     ` rog [this message]
2005-03-03  6:13       ` Sergey Reva
2005-03-03  9:18         ` Charles Forsyth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=546fc93e2b9a8dae55875ff7e04830a7@vitanuova.com \
    --to=rog@vitanuova.com \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).