LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2005, 08:21 PM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
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
 
Old 09-07-2005, 10:09 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Ok... you never assigned a value to query.
 
Old 09-08-2005, 10:35 AM   #3
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
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.
 
Old 09-08-2005, 10:40 AM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
xgettext error php language uknown psychomachine Linux - Software 0 12-09-2004 09:28 AM
C pointers confusion - char ** = char [][] ?? saravkrish Programming 12 12-02-2004 10:06 AM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM
uknown timeout.synack key in redhat 9 adrianmak Linux - Security 1 01-16-2004 12:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration