![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
I agree, the code has many problems. A minor point
This will make every f[i] point to the address of the constant string "ARUN". |
|
#4
|
|||
|
|||
|
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); |
|
#5
|
|||
|
|||
|
Hi All..
Thanks for you all .. for you surprise it compilles !.. I also surprised after seeing this code .. THanks, Arun |
|
#6
|
|||
|
|||
|
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
|
||||
|
||||
|
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.
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. |
||||
| Google The UNIX and Linux Forums |