![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 */
}
IF this is homework, please do not ask homework questions. I have trouble beleiving this is for production work. |
|
||||
|
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.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|