LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-12-2012, 01:29 AM   #1
ashish anand
LQ Newbie
 
Registered: Oct 2009
Posts: 23

Rep: Reputation: 0
getting garbage when printing array of string dynamically.


Hi
this problem has confused me .
I am trying to build an array of strings dynamically
through this program

main()
{
int i ;
int numb = 7;
char s[100];
char **inpt=NULL;;
inpt = (char **)malloc(numb*sizeof(char));
memset(inpt,0,numb*sizeof(char));
//char *inpt[7];
int len;
for(i =0;i<numb;i++)
{
scanf("%s",s);
len = strlen(s)+1;
inpt[i]=malloc(len*sizeof(char));
memset(inpt[i],0x0,len*sizeof(char));
strcpy(inpt[i],s);
fflush(stdin);
}
printf("\n\n");
for(i = 0;i<numb;i++)
{
puts(inpt[i]);
}

}


the input i am providing is
WINNT/SYSTEM32/CONFIG
GAMES
WINNT/DRIVERS
HOME
WIN/SOFT
GAMES/DRIVERS
WINNT/SYSTEM32/CERTSRV/CERTCO~1/X86


in this case i am getting garbage at the first index of array i.e, inpt[0]
but when i am using
char *inpt[7]

instead of
char **inpt=NULL;;
inpt = (char **)malloc(numb*sizeof(char));
memset(inpt,0,numb*sizeof(char));


i get correct output.

why when i allocat inpt on heap gives me wrong output as compared to when i allocate it on stack?
 
Old 02-12-2012, 02:36 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

You allocate an array of 7 chars here:
Code:
inpt = (char **)malloc(numb*sizeof(char));
memset(inpt,0,numb*sizeof(char));
This is wrong. You allocating not enough memory.

You should allocate an array of pointers to chars instead:
Code:
inpt = (char **)malloc(numb*sizeof(char*));
memset(inpt,0,numb*sizeof(char*));
On my machine sizeof(char) = 1, but sizeof(char*) = 4.

By the way there are `calloc()' function to allocate and clear memory in one call.

Last edited by firstfire; 02-12-2012 at 02:44 AM.
 
Old 02-12-2012, 06:33 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
In all relevant C standards, sizeof (char) is defined as 1.

I think firstfire is spot on, but I didn't read the code. It is Sunday, and I don't read non-indented code on Sundays. Please, ashish anand, use [CODE][/CODE] tags around code in your post, to make the code more readable. For one, it retains all indentation intact, and uses a fixed-width font.

Last edited by Nominal Animal; 02-12-2012 at 06:35 AM. Reason: un-intended -> non-indented :)
 
Old 02-12-2012, 11:32 PM   #4
ashish anand
LQ Newbie
 
Registered: Oct 2009
Posts: 23

Original Poster
Rep: Reputation: 0
@firstfire

Hi
Thanks for your help .I understood the problem .
 
  


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
bash unset array loops GARBAGE strongdrink Programming 7 06-29-2011 07:56 AM
Printing garbage, not sure what to do John Teisberg Linux - Newbie 1 03-29-2009 08:11 AM
dynamically expanding an array in c++ a1ghagh05t Programming 4 03-07-2004 09:00 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
Printing garbage sakkie Slackware 1 09-19-2003 12:14 PM

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

All times are GMT -5. The time now is 11:49 PM.

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