-
dynamic allocation of memory for arrays of type char
Is there anyway of setting the size of an array at runtime? Ive tried several
methods but the compiler wont have any of it! :P. Im a newbie, so if you
could explain as clearly as possible, I would be grateful. Thanks in advance
:)
-
Re: dynamic allocation of memory for arrays of type char
NO. all variables(the pointer only here, type = int usually) go on the stack,
and their size is known at compile time. You can use a vector, or for this
a string, or a pointer.
the pointer is closest to what you asked for:
char * cp = new char[variable_size]; //create it
cp[3] = 'a'; //use like an array if you want too.
delete [] cp; //give memory back to os
"Matthew" <mattjvince@hotmail.com> wrote:
>
>Is there anyway of setting the size of an array at runtime? Ive tried several
>methods but the compiler wont have any of it! :P. Im a newbie, so if you
>could explain as clearly as possible, I would be grateful. Thanks in advance
>:)
-
Re: dynamic allocation of memory for arrays of type char
I don't see why you cannot create an array at runtime?
int size;
cin>>size;
char myArray[size];
What is wrong with that?
-Gandi
"jonnin" <jonnin@vol.com> wrote:
>
>NO. all variables(the pointer only here, type = int usually) go on the stack,
>and their size is known at compile time. You can use a vector, or for this
>a string, or a pointer.
>
>the pointer is closest to what you asked for:
>
>char * cp = new char[variable_size]; //create it
>
>cp[3] = 'a'; //use like an array if you want too.
>
>delete [] cp; //give memory back to os
>
>
>"Matthew" <mattjvince@hotmail.com> wrote:
>>
>>Is there anyway of setting the size of an array at runtime? Ive tried several
>>methods but the compiler wont have any of it! :P. Im a newbie, so if you
>>could explain as clearly as possible, I would be grateful. Thanks in advance
>>:)
>
-
Re: dynamic allocation of memory for arrays of type char
"Gandi" <eclipse_miker@hotmail.com> wrote:
>
>I don't see why you cannot create an array at runtime?
>
>int size;
>cin>>size;
>char myArray[size];
>
>What is wrong with that?
>-Gandi
>
Try it and see.
The size of an array can only be declared using a constant integer. The size
variable in your example isn't.
The only way of dynamically allocating an array at runtime is, as jonnin
explained, by allocating it on the heap using pointers. He made a distinction
between this and a 'real' array but I think that's semantics. If it looks
like an array and acts like an array, then it's close enough for me.
ch0rlt0n
-
Re: dynamic allocation of memory for arrays of type char
You can't do it, because the compiler does not know how much space to reserve.
The memory used by the program, excluding dynamic allocations, is a fixed
number (you can define it somewhat in windows). What if size were 2^64 bytes?
Should the compiler reserve this much of your system's memory in case this
happens?
You can argue that the language should allocate and deallocate memory for
you (and it does, if you use a vector!), but c/c++ does not do this at the
lower levels because that creates unnecessary overhead that must be avoided
in performance critical software. reallocation, which vectors do on the fly
(say you filled the array and need more space, getting it is reallocation)
costs a lot of time. Dynamic allocation is not cheap compared to a fixed
sized array, which is all you need in many places.
Anyway, just accept that the line you typed is not legal in c/c++ and will
always give a compliler error... =)
"Gandi" <eclipse_miker@hotmail.com> wrote:
>
>I don't see why you cannot create an array at runtime?
>
>int size;
>cin>>size;
>char myArray[size];
>
>What is wrong with that?
>-Gandi
>
>"jonnin" <jonnin@vol.com> wrote:
>>
>>NO. all variables(the pointer only here, type = int usually) go on the
stack,
>>and their size is known at compile time. You can use a vector, or for this
>>a string, or a pointer.
>>
>>the pointer is closest to what you asked for:
>>
>>char * cp = new char[variable_size]; //create it
>>
>>cp[3] = 'a'; //use like an array if you want too.
>>
>>delete [] cp; //give memory back to os
>>
>>
>>"Matthew" <mattjvince@hotmail.com> wrote:
>>>
>>>Is there anyway of setting the size of an array at runtime? Ive tried
several
>>>methods but the compiler wont have any of it! :P. Im a newbie, so if you
>>>could explain as clearly as possible, I would be grateful. Thanks in advance
>>>:)
>>
>
-
Re: dynamic allocation of memory for arrays of type char
Gandi wrote:
>
> I don't see why you cannot create an array at runtime?
>
> int size;
> cin>>size;
> char myArray[size];
>
> What is wrong with that?
The size of an array must be known at compile time. Furthermore, its
size must be a constant expression, so even the following won't work:
int size=15;
char arr[size];
This is because the compiler must calculate the size of the array at
compile time. If you wish to create an array whose size may change
dynamically, use a pointer or better yet -- a vetcor. That's C++ and
there's not much that you can do about it.
Danny
> -Gandi
>
> "jonnin" <jonnin@vol.com> wrote:
> >
> >NO. all variables(the pointer only here, type = int usually) go on the stack,
> >and their size is known at compile time. You can use a vector, or for this
> >a string, or a pointer.
> >
> >the pointer is closest to what you asked for:
> >
> >char * cp = new char[variable_size]; //create it
> >
> >cp[3] = 'a'; //use like an array if you want too.
> >
> >delete [] cp; //give memory back to os
> >
> >
> >"Matthew" <mattjvince@hotmail.com> wrote:
> >>
> >>Is there anyway of setting the size of an array at runtime? Ive tried several
> >>methods but the compiler wont have any of it! :P. Im a newbie, so if you
> >>could explain as clearly as possible, I would be grateful. Thanks in advance
> >>:)
> >
-
Re: dynamic allocation of memory for arrays of type char
Thanks a lot, I would have never figured that out! The problem was that I
was using the following to declare the variable:
char* pstrTempStr[] = new char[variable_size];
I thought I needed to include the brackets to show that I was declaring a
pointer to type char[]! Another thing that I was trying to do was use the
following statement...
*pstrString[i];
when refering to an element from a pointer which is passed to the function
by pointers. I think im starting to see what I did wrong :P
"jonnin" <jonnin@vol.com> wrote:
>
>NO. all variables(the pointer only here, type = int usually) go on the stack,
>and their size is known at compile time. You can use a vector, or for this
>a string, or a pointer.
>
>the pointer is closest to what you asked for:
>
>char * cp = new char[variable_size]; //create it
>
>cp[3] = 'a'; //use like an array if you want too.
>
>delete [] cp; //give memory back to os
>
>
>"Matthew" <mattjvince@hotmail.com> wrote:
>>
>>Is there anyway of setting the size of an array at runtime? Ive tried several
>>methods but the compiler wont have any of it! :P. Im a newbie, so if you
>>could explain as clearly as possible, I would be grateful. Thanks in advance
>>:)
>
-
Re: dynamic allocation of memory for arrays of type char
Hi,
I suppose that you could produce a linked list that contains the data members
that you had wished to store in the array. Err, I am not sure if this is
what Danny mean by 'using pointers'.
Hope that is of help.
Danny Kalev <dannykk@inter.net.il> wrote:
>
>
>Gandi wrote:
>>
>> I don't see why you cannot create an array at runtime?
>>
>> int size;
>> cin>>size;
>> char myArray[size];
>>
>> What is wrong with that?
>
>The size of an array must be known at compile time. Furthermore, its
>size must be a constant expression, so even the following won't work:
>
>int size=15;
>
>char arr[size];
>
>This is because the compiler must calculate the size of the array at
>compile time. If you wish to create an array whose size may change
>dynamically, use a pointer or better yet -- a vetcor. That's C++ and
>there's not much that you can do about it.
>
>Danny
>> -Gandi
>>
>> "jonnin" <jonnin@vol.com> wrote:
>> >
>> >NO. all variables(the pointer only here, type = int usually) go on the
stack,
>> >and their size is known at compile time. You can use a vector, or for
this
>> >a string, or a pointer.
>> >
>> >the pointer is closest to what you asked for:
>> >
>> >char * cp = new char[variable_size]; //create it
>> >
>> >cp[3] = 'a'; //use like an array if you want too.
>> >
>> >delete [] cp; //give memory back to os
>> >
>> >
>> >"Matthew" <mattjvince@hotmail.com> wrote:
>> >>
>> >>Is there anyway of setting the size of an array at runtime? Ive tried
several
>> >>methods but the compiler wont have any of it! :P. Im a newbie, so if
you
>> >>could explain as clearly as possible, I would be grateful. Thanks in
advance
>> >>:)
>> >
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks