The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 05:47 AM
A few questions... halluc1nati0n UNIX for Dummies Questions & Answers 2 03-28-2007 10:29 AM
3 questions in 1 alikun UNIX for Dummies Questions & Answers 2 03-27-2007 02:59 AM
about memset fuction ranj@chn High Level Programming 3 01-31-2006 08:59 AM
few questions vivekshankar UNIX for Dummies Questions & Answers 5 05-20-2005 10:50 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-08-2007
arunkumar_mca arunkumar_mca is offline
Registered User
  
 

Join Date: Oct 2004
Posts: 255
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
  #2 (permalink)  
Old 08-08-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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.
  #3 (permalink)  
Old 08-08-2007
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
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".
  #4 (permalink)  
Old 08-08-2007
porter porter is offline Forum Advisor  
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.
  #5 (permalink)  
Old 08-08-2007
arunkumar_mca arunkumar_mca is offline
Registered User
  
 

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

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

THanks,
Arun
  #6 (permalink)  
Old 08-08-2007
arunkumar_mca arunkumar_mca is offline
Registered User
  
 

Join Date: Oct 2004
Posts: 255
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..
  #7 (permalink)  
Old 08-09-2007
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0