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 08:13:23 +1100

Hi,

usenet is probably the best place:
comp.lang.c.*
comp.lang.c++.* 

but be careful no to use your normal e-mail 
because you will receive a thousand spams 
a day after you start posting.

to be honest, for c, programming books are the 
best place to learn, they will give you the right 
answer well before the web ever will.

A good book is The C Programming Language
you can dl a "sample" copy from here:  
http://www.partow.net/downloads/The_C_Programming_Language-SE.pdf



> I just studied pointers, but I didn't yet know of the existence of 
> malloc and realoc. 
malloc, realloc and pointer are like a holy trinity... and free is their 
prophet. if you know of 1 of them you should know of all the others.

btw before the final exit routine you should do a free on the array 
otherwise things like bcheck will detect a memory leak with the 
program
ie:

free(array);


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: "GNU/Linux in Iran" <address@hidden>
Sent: Saturday, December 13, 2003 7:45 AM
Subject: Re: [linuxiran] [OT] C programming, variable size array


> 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
> 
> 
> _______________________________________________
> 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]