change array size at runtime

E

Eric Sosman

sunny_rao said:
Is it possible to change the size of an array dynamically at runtime ?

No.

In the older "C90" Standard, every array has a size
that is fixed at compile time; the size is a compile-time
constant. In the newer "C99" Standard the array's size
can be a run-time expression (e.g., a variable), but once
the array is created its size remains constant for as long
as the array exists; changing the value of the size-giving
variable after the array is created does not change the
size of the array.

A region of dynamically-allocated memory can act as an
array for many purposes, and the realloc() function can
change the size (and possibly the location) of the region.
It's not *quite* the same thing, but it often suffices.
 
J

Jason

sunny_rao said:
Is it possible to change the size of an array dynamically at runtime
?

Check out "void *realloc(void *p, size_t size)" in <stdlib.h>.

-Jason
 
R

Robert Gamble

Is it possible to change the size of an array dynamically at runtime ?

C'mon, read the FAQ, it was posted earlier today for crying out loud...

Rob Gamble
 
M

Martin Ambuhl

sunny_rao said:
Is it possible to change the size of an array dynamically at runtime ?

A declared array cannot be resized. A region of memory allocated via
malloc, calloc, or realloc can be resized with realloc.
 
N

Null_Ptr_72

Hey,

If you needed to resize any data-structure at run-time, i suggest
you read more on malloc _and_ realloc, a very good practice would be
for you to create a Linked List, wait for user inpput and
adjust&&readjust the size at run time.

As far as Arrays[ ], ISO forbids for us to change the size at
run-time, depending on your implementation (i.e Borland C++ builder 6)
will flag an error, while gcc Won't. The whole idea behind this is to
help us not "write post fence" data in memory.

Hope this helps
 
K

Keith Thompson

Null_Ptr_72 said:
As far as Arrays[ ], ISO forbids for us to change the size at
run-time, depending on your implementation (i.e Borland C++ builder 6)
will flag an error, while gcc Won't. The whole idea behind this is to
help us not "write post fence" data in memory.

gcc won't flag an error for what? I don't know how you'd even *try*
to change the size of an array. Perhaps something like
sizeof(array_obj) *= 2;
but I'd expect any compiler to reject that.
 
G

Gregory Pietsch

The comp.lang.c Oracle has pondered your question deeply.

Your question was:
Is it possible to change the size of an array dynamically at runtime
?

And thus spake the Oracle:

} Not a fixed-size array, but if you declare a pointer, you can change
the
} amount of space it points to via realloc().
} See the code in FreeDOS Edlin 2.4 for a complete dynamic array
implementation.

Gregory Pietsch
 
E

Emmanuel Delahaye

sunny_rao wrote on 15/04/05 :
Is it possible to change the size of an array dynamically at runtime ?

If it was dynamically allocated, yes, by a proper use of realloc() (see
the FAQ). Static arrays can't be resized.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
N

Null_Ptr_72

Dear Thompson,
"but I'd expect any compiler to reject that."
There are many tricks we can play to go on doing illegal operations
at compile time! "Implementation" however can determine how limited we
are in "being" slick &&|| Tricky.

Try your code "sizeof(array_obj) *= 2" on gcc, and try it on a
different compiler, see the results. One will accept it "because its
tricky" while the other won't "because its expensive!"
 
K

Keith Thompson

Null_Ptr_72 said:
Dear Thompson,
"but I'd expect any compiler to reject that."
There are many tricks we can play to go on doing illegal operations
at compile time! "Implementation" however can determine how limited we
are in "being" slick &&|| Tricky.

Try your code "sizeof(array_obj) *= 2" on gcc, and try it on a
different compiler, see the results. One will accept it "because its
tricky" while the other won't "because its expensive!"

I just tried it on five different compilers, including gcc; all of
them rejected it. I would be astonished if *any* compiler supported
modifying the size of an array object in this manner; it would be very
difficult to do so while maintaining the required C semantics.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top