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: Aryan Ameri
Subject: Re: [linuxiran] [OT] C programming, variable size array
Date: Fri, 12 Dec 2003 22:45:25 +0200
User-agent: KMail/1.5.3

On Friday 12 December 2003 22:05, Arash Partow wrote:
> 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.
>

I just studied pointers, but I didn't yet know of the existence of 
malloc and realoc. Anyway, your code, as well as these functions man 
pages are now putting me in the right direction. Thanks for the help.

And I want to be bold, and ask again, cause this is really important for 
me: Anyone know of any C mailing list? (pretty strange that I can't 
find one!!).

Cheers

-- 
/*  Trademarks, Copyrights, Patents, etc are all loans from the public 
domain. They are not a property ('intellectual' or otherwise.) */
        

Aryan Ameri




reply via email to

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