The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Few questions gina Shell Programming and Scripting 2 05-28-2007 02:47 AM
A few questions... halluc1nati0n UNIX for Dummies Questions & Answers 2 03-28-2007 07:29 AM
3 questions in 1 alikun UNIX for Dummies Questions & Answers 2 03-26-2007 11:59 PM
about memset fuction ranj@chn High Level Programming 3 01-31-2006 05:59 AM
few questions vivekshankar UNIX for Dummies Questions & Answers 5 05-20-2005 07:50 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 08-08-2007
Registered User
 

Join Date: Oct 2004
Posts: 235
questions in memset

HI all ,

please find the piece of code below

char *t[100];
char *f[100];
char buf[60];
memset(buf,0,50);

after that i am assigning memory

for (i=0; i<100; i++)
{
t[i] = buf+(i*6);
f[i] = "ARUN";
}

my question ..
1) i have run this it is working in one system and not working other system
my doubt is t[i] we are allcation is having 600 bytes memory bu the loop increaments and it calculates as buf+i*6
will this result to segmentation fault(core dump) .. Why it is runningin one system and same exe is making core dump in other
the core dump occurs when i tried to loop through t array

2) I have set 50 byte to 0 by memset when i print by the buf by gdb i am getting only 10 bytes assigned to null not all why ??.. please let me know..

3) Even though i am having only 60 sapces in buf array how the t array points to 600 location ?.. Is it possible..


Thanks,
Arun
Reply With Quote
Forum Sponsor
  #2  
Old 08-08-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,275
Your code has a lot of errors. I'm surprised it compiles at all.
Code:
char *t[100];
char *f[100];
char buf[60];
memset(buf,0,50); /* should be memset(buf, 0, 60); because buf[60] is 60 bytes long*/

/*after that i am assigning memory */

for (i=0; i<100; i++)
{
    t[i] = buf+(i*6);  /* unsigned char allows values 0-255 100*6 = 600 plus buf is a pointer which can be any value */
    f[i] = "ARUN";   /* you have no allocated memory for the pointer, all 100 of them */
}
I cannot believe this runs "correctly" anywhere.
IF this is homework, please do not ask homework questions. I have trouble beleiving this is for production work.
Reply With Quote
  #3  
Old 08-08-2007
kahuna's Avatar
Registered User
 

Join Date: Apr 2007
Posts: 148
I agree, the code has many problems. A minor point
Quote:
Originally Posted by jim mcnamara View Post
Code:
    f[i] = "ARUN";   /* you have no allocated memory for the pointer, all 100 */
This will make every f[i] point to the address of the constant string "ARUN".
Reply With Quote
  #4  
Old 08-08-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Also, buf[60] would have to be buf[600]

as you have 100 elements and are allocating 6 characters per element.

You should be using

Code:
memcpy(f[i],"ARUN",5);
or similar.
Reply With Quote
  #5  
Old 08-08-2007
Registered User
 

Join Date: Oct 2004
Posts: 235
Hi All..

Thanks for you all .. for you surprise it compilles !.. I also surprised after seeing this code ..

THanks,
Arun
Reply With Quote
  #6  
Old 08-08-2007
Registered User
 

Join Date: Oct 2004
Posts: 235
offcourse this is not a home work ... I am maintaning a code and i got a fix there i am surprised to see that it works that so i posted .. Please find the part of the code below
...
...
*module; // What this will do
char *t_array[100];
char *f_array[100];
memset(buf,0,50);

for(cnt1=0; cnt1<100; cnt1++)
{
t_array[cnt1] = buf+(cnt1*6);
f_array[cnt1] = "121212121";
}

cnt1 = 0;

module = text_list_rewind(gbs->modules);

while (module)
{
f_array[cnt1] = module;
cnt1++;
module = text_list_nxt(gbs->modules);
} /* f_array is ready now */

rc = dbsql_exec(gbs->db, sql, t_array[0], sizeof(t_array), 1, NULL, NULL);
if (rc == DBSQL_FAIL)
{
fran_close(gbs);
fatal_exit(-1, "sql command failed: %s\n", sql);
}

else
{
i=i+1;
while (rc == DBSQL_MORE)
{
rc = dbsql_more(gbs->db, t_array[i], 1);
// CORE DUMP IS HAPPENING HERE
i++;
}

i=0;
} /* t_array is ready now */

also i am not seeeing any values in t_array and f_array .. Please advice ..

Thanks,
Arun..
Reply With Quote
  #7  
Old 08-09-2007
kahuna's Avatar
Registered User
 

Join Date: Apr 2007
Posts: 148
Quote:
Originally Posted by porter View Post
Also, buf[60] would have to be buf[600]
I definitely agree that buf needs to be at least 600. From what we can see, the memset probably also needs to use 600 (the size of buf). But since we only see parts of the code, there may be a reason that only part of buf is initialized.
Quote:
Originally Posted by porter View Post
Code:
memcpy(f[i],"ARUN",5);
This would be appropriate if f[i] is initialized to point to a memory space. I would not replace the existing f[i] = "ARUN" code with this.

arunkumar_mca, it would probably take a lot of debugging to go beyond those suggestions. If you have a debugger, I suggest that you use it to trace through the code.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0