no type memory declaration

no type memory declaration

Post by Hany Morcos ( » Fri, 04 Sep 1998 04:00:00



This might be a strange question.

  Does C allow a programmer to  declare some memory block of no specific type at
compile time and allow a programmer to change the memory type at run time? \\\

Thnak You
Hanyh

 
 
 

no type memory declaration

Post by Fletcher Gle » Fri, 04 Sep 1998 04:00:00


You are really asking "Does C support dynamic memory allocation?".
The answer is yes.  See the man page for malloc().

--
                Fletcher Glenn


Quote:

>This might be a strange question.

>  Does C allow a programmer to  declare some memory block of no specific type at
>compile time and allow a programmer to change the memory type at run time? \\\

>Thnak You
>Hanyh


 
 
 

no type memory declaration

Post by Hany Morcos ( » Fri, 04 Sep 1998 04:00:00


  NO,

        I know about malloc already.

     I s there any way for me to get a chunk of memory at compile time.
Not at run time or at initilization time.

Thank you
Hany

: You are really asking "Does C support dynamic memory allocation?".
: The answer is yes.  See the man page for malloc().

: --
:               Fletcher Glenn


: >
: >
: >
: >This might be a strange question.
: >
: >
: >  Does C allow a programmer to  declare some memory block of no specific type at
: >compile time and allow a programmer to change the memory type at run time? \\\
: >
: >
: >
: >
: >
: >Thnak You
: >Hanyh
: >

 
 
 

no type memory declaration

Post by Aaron Cra » Fri, 04 Sep 1998 04:00:00




Quote:> Is there any way for me to get a chunk of memory at compile time.  Not at
> run time or at initilization time.

The language guarantees that for any type T, you can convert a pointer to T
into a pointer to character type and back, and the result will compare equal
to the original pointer.  This implies that you can allocate an array of
characters, and convert a pointer to the first character of that array into
a pointer to any other type.  That's not quite the full story, though,
because you have to worry about alignment.  You need to make sure that your
block of memory has the strictest alignment requirements that exist on your
platform.  The C standard doesn't define which type has the strictest
alignment requirements, but in most implementations, double is a good
choice.  Try this, for some constant expression N:

    union memory_block {
        double not_used;                /* force alignment */
        char memory[N];
    };

Since a pointer to a union is also (when suitably cast) a pointer to any of
its members, you can freely cast a (union memory_block *) into any other
type.

--

 
 
 

no type memory declaration

Post by A.van.Kesse » Fri, 04 Sep 1998 04:00:00



Quote:

>  NO,

>        I know about malloc already.
>     I s there any way for me to get a chunk of memory at compile time.
>Not at run time or at initilization time.

Your Question is still not clear.

There are three important moments in memory allocation

Compile time: if fixed (static/global/auto) allocations are encountered,
this info is written (in some form) to the object file.
On dynamic allocation the function call to malloc/free is put
in the object file.
No memory is allocated at compile time (except by the compiler)

Loading/initialisation:
static/global data is allocated and maybe initialized. Some fixed
 initializers are allready present in the object, some are calculated by the
loader/linker.
Automatic data is allocated on function entry (typically by altering the stack)
, If it had an initializer, it is initialized (at each function call !!)

dynamic memory is allocated at run time. It is always uninitialized( or zero).
It's size is specified at run time, and can be "variable" .

Of course memory allocation does not allocate anything. The memory has
always been there, the program just gets a chance to reference it.

Memory does not have a "type" (*1) . After the compiler has determined the
size/alignment properties and written them to the objectfile, the typing info
is only kept for compile-time type checking (*2).

1) sorry guru's, I know about bizarre architectures with separate
 segments for byte aligned data, need to simplify ...
2) Yes it can be maintained in the debugging info ...
--
Happy hacking,

Adriaan van Kessel.
Ingres DBA, C/Unix hacker

(remove NotThere. from the address in the header)
*** Nederlandstalige zachtwaar is een pijn in de aars ***

 
 
 

no type memory declaration

Post by Robert F » Fri, 04 Sep 1998 04:00:00


In that case, I'd say not that I know of, though your particular compiler
could possibly have some utilities that do such a thing. Since malloc
doesn't work for you, and I'm assuming a static memory allocation
wouldn't work, there's not much more you can do.

The most nearly portable solution I can think of is something like:

#ifndef COMPILE_TIME_ARRAY_SIZE
#define COMPILE_TIME_ARRAY_SIZE 0
#endif
char compile_time_array[ COMPILE_TIME_ARRAY_SIZE];

And putting a -DCOMPILE_TIME_ARRAY_SIZE <value> option in your compiler
command line. Assuming, of course, that your compiler supports doing such
a thing. (And, of course, you can replace the 0 with any reasonable minimum).


>  NO,

>        I know about malloc already.
>     I s there any way for me to get a chunk of memory at compile time.
>Not at run time or at initilization time.


>: You are really asking "Does C support dynamic memory allocation?".
>: The answer is yes.  See the man page for malloc().

>: >
>: >This might be a strange question.
>: >
>: >  Does C allow a programmer to  declare some memory block of no specific type at
>: >compile time and allow a programmer to change the memory type at run time? \\\
>: >

--
  Bob           One good thing about being wrong is the joy it brings to others
 
 
 

no type memory declaration

Post by Crazy Pe » Fri, 04 Sep 1998 04:00:00


Quote:>      I s there any way for me to get a chunk of memory at compile time.
> Not at run time or at initilization time.

I am not sure I understand your question either.  Why can't you just
declare an array of char and cast it however you like?

Peace

Peter