LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   char malloc + uknown characters (https://www.linuxquestions.org/questions/programming-9/char-malloc-uknown-characters-361263/)

alaios 09-07-2005 08:21 PM

char malloc + uknown characters
 
query=(char *)malloc(100*sizeof(char));
printf("String meta to malloc %s \n",query);
Hi i have some problems with the dynamic allocation of char...
These two lines give the a string that is not empty

String meta to malloc \uffff\uffff\u0777

P.s The string returns me instead of an empty string some square boxes.... that cannot be print here

Matir 09-07-2005 10:09 PM

Ok... you never assigned a value to query.

rstewart 09-08-2005 10:35 AM

malloc() does not initialize the memory block that it returns. The contents of the memory will contain whatever was last placed in it by any previously run program. You should initialize the memory block to zeros before actually doing anything useful with it. You can use any number of functions such as memset(), or bzero() to perform this initialization.

Matir 09-08-2005 10:40 AM

As stated above, all malloc does is ask the system to set aside a block of n bytes for use by that program, and then give the address back to the program. It is up to you to put something in that memory.


All times are GMT -5. The time now is 02:28 AM.