bna-linuxiran
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [linuxiran] [OT] C programming, variable size array


From: Arash Partow
Subject: Re: [linuxiran] [OT] C programming, variable size array
Date: Sat, 13 Dec 2003 07:05:36 +1100

Hi Aryan,

There are 2 important methods malloc and realloc
malloc allocated memory and realloc as its name
suggests allow you to re-allocate more memory from the OS.


ie:

 /*
      create dynamic array,  guess an initial size in your case....
 */

   int   myArraySize = 123;
   char* myArray     = (char*)malloc(sizeof(char)*myArray);

   if(myArray == NULL)
   {
      printf("!-ERROR-!  System could not allocate memory...\n");
   }
    .
    .
    .
    now lets say you want to add more to the array but you've
    reached the limit of the array.

   int additionalLength = 123;
   if ((myArray =
(char*)realloc(myArray,sizeof(char)*(myArraySize+additionalLength)) == NULL)
   {
      printf("!-ERROR-!  System could not re-allocate memory...\n");
   }

    .
    .
    .


 If you have anymore questions feel free to ask.


Regards


Arash






__________________________________________________
Be one who knows what they don't know,
Instead of being one who knows not what they don't know,
Thinking they know everything about all things.
http://www.partow.net




----- Original Message ----- 
From: "Aryan Ameri" <address@hidden>
To: <address@hidden>
Sent: Saturday, December 13, 2003 5:40 AM
Subject: [linuxiran] [OT] C programming, variable size array


> Hi There:
>
> A while ago I was asked this question from a fellow friend of mine:
>
> "Write a program, which promts the uset to enter some numbers. The user
> should terminate the sequence of numbers by entering EOF character. The
> program should put numbers entered by the user in to a 1D array".
>
> Which seems pretty simple in first glance, but has one problem. When
> initializing the array, I don't know it's size (and I don't want to ask
> the user to enter it's size). The first soloution that came to my mind
> was to initialize the array to a very big number. However this is not
> elegant programming, and is a waste of memory. My second soloution was,
> to initialize the array inside the loop, so that it enlarges it's size
> continously each time the user inputs a number. I wrote the following
> code:
>
> #include <stdio.h>
>
> main()
> {
> int tmp, cnt = 0;
> static int arr[cnt];
> printf( "Enter Number\n");
> scanf( "%d", &tmp);
> while ( (tmp = getchar() ) != EOF ) {
>         arr[cnt] = tmp;
>         cnt += 1;
>         static int arr[cnt];
>         printf( "Enter Number\n");
>         scanf( "%d", &tmp);
> }
>
> return 0;
> }
>
> This sounded logical to me. But the compiler (gcc 3.2) gives me a syntax
> error saying that 'storage size of 'arr' isn't constant'. Well, I don't
> want it to be constant!
>
> I was wondering if any of you could help me solve this question. This is
> not yet-another-student-asking-for-help-to-do-homework. This is a
> problem for me, which has made me busy for a couple of days, and
> googling and greping /usr/include and other basic methods didn't reveal
> anything to me.
>
> PS: Now that I am on the subject, can anyone point me to a active C
> mailing list? one that I can ask these kind of question from, as they
> come up? preferrably with a tendency towards Unix/Linux. (mailing lists
> please, not newsgroups).
>
>
> Cheers
> -- 
> /*  Trademarks, Copyrights, Patents, etc are all loans from the public
> domain. They are not a property ('intellectual' or otherwise.) */
>
>
> Aryan Ameri
>
>
> _______________________________________________
> bna-linuxiran mailing list
> address@hidden
> http://mail.nongnu.org/mailman/listinfo/bna-linuxiran
>
>





reply via email to

[Prev in Thread] Current Thread [Next in Thread]