cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1999/06/13/17:53:15

From: "Al Morgan" <muaddib AT proaxis DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Pointers, functions and memory
Date: Sun, 13 Jun 1999 13:58:44 -0700
Organization: Posted via RemarQ Communities, Inc.
Lines: 60
Message-ID: <929307582.390.40@news.remarQ.com>
References: <37606970 DOT 14959285 AT news DOT xmission DOT com>
NNTP-Posting-Host: 198.106.198.180
NNTP-Posting-Date: Sun, 13 Jun 1999 20:59:42 GMT
X-Trace: 929307582.390.40 4VELK9AUPC6B4C66AC qube-02.us-ca.remarq.com
X-Complaints-To: newsabuse AT remarQ DOT com
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Scott Brown <skb AT xmission DOT removethis DOT com> wrote in message
news:37606970 DOT 14959285 AT news DOT xmission DOT com...
> I have a couple of unrelated questions here...I'd really appreciate it
> if someone can clue me in on either issue.
>
> First question (this might be more of a C question than a DJGPP
> question, but here goes):  I have a routine that stores arbitrary data
> into memory using a scheme where a void* is cast to a specific pointer
> type by a case statement.  In addition to a gamut of standard types, I
> have some data types that needs to be processed separately at runtime.
> My idea for this is to use that void* data pointer to hold a function
> pointer, which can be cast to the appropriate type and invoked to
> handle the data at hand.  So I have code which goes something like
> this:
> extern void *p1, *p2, *p3;
> *(int*)p1 = 42;
> *(float*)p2 = 3.14;
> ((int(*)(int, char *))p3)(99, "done");
>
> Is this a safe thing to do?  Can a data pointer be safely interchanged
> with a function pointer?  If it's okay under DOS/DJGPP, will it still
> work after I port my program to Unix/GCC?
>
>
> Second question:  When my DJGPP program executes a non-DJGPP DOS
> program as a child process, is the child's conventional memory space
> affected by my program's memory allocations?  Is DJGPP's conventional
> memory footprint consistent and/or predictable?  Are there any ways I
> can minimize that footprint for the duration of the system/spawn call?
>
>
> Thanks,
> -Scott
>
>
>
>
>
>
> ...okay, so it's six questions.  Humor me, willya?

That might not be so safe, using the same variables to point to both data
and functions.  What if you were to call a pointer-function while it still
pointed to a variable?  I'm not sure, but it would either start executing
random instructions (not good), or if it's really smart, generate a runtime
error (not good, but better).  Perhaps instead of making pointers to
functions, you can put them in a class, and have a flag variable, telling
you if the function is really set to a function.

As for the second question, I really don't know.  Does malloc(...) use pure
C to allocate memory, or does it call a higher level OS controller memory
thingy?  If it's the first, you'd probably have problems, the second would
be O.K.

I hope this helps the least bit,
    Al [muaddib AT proaxis DOT com]



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019