questions in memset


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers questions in memset
# 1  
Old 08-08-2007
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  
Old 08-08-2007
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  
Old 08-08-2007
I agree, the code has many problems. A minor point
Quote:
Originally Posted by jim mcnamara
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  
Old 08-08-2007
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  
Old 08-09-2007
Hi All..

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

THanks,
Arun
# 6  
Old 08-09-2007
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  
Old 08-09-2007
Quote:
Originally Posted by porter
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
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

Memset fails on Solaris

Hi, memset call is failing on solaris for me. I wrote below code and that also fails. Any hints? void *memset(void *dst, int c, size_t n) { if (n) { char *d = dst; do { *d++ = c; } while (--n); } return dst; } (2 Replies)
Discussion started by: skyineyes
2 Replies

2. UNIX for Dummies Questions & Answers

Vi questions

Hello, I would like to know how we can highlight/select a section of a file in vi and delete that section if we don't want to use the dd command to delete one line at at time. There is one where we don't want to delete the whole line , but up to a certain word. (2 Replies)
Discussion started by: Pouchie1
2 Replies

3. UNIX for Dummies Questions & Answers

Just had a few questions

1) The lpr and sort utilities accept input either from a file named on the command line or from standard input. a)Name two other utilities that function in a similar manner. b)Name a utility that accepts its input only from standard input. 2) Explain the following error message. What... (10 Replies)
Discussion started by: youngyou
10 Replies

4. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

5. Shell Programming and Scripting

More ps questions.

Hey all, Thanks for all the help you have given me. Two more things I am trying to figure out. I need to issue a command..example ps -ef | grep <process> This would return about 10-15 running processes. I need to verify that there are x amount of processes running. What is... (7 Replies)
Discussion started by: jeffs42885
7 Replies

6. Programming

C bzero() to memset() issue

Hi guys, my tool works fine in gentoo, ubuntu now im trying to port it to windows but bzero/bcopy I read aren't working on windows and for better portability I should of use memset() so im trying to translate bzero(buffer,256);in printf("MAIL TO"); strcpy(buffer, rcp); ... (4 Replies)
Discussion started by: Jess83
4 Replies

7. Solaris

application Crashes on memset ?? any suggestions

Hi All, we have an application that is written in 'C' programming to connects to various servers in the organization. The bellow code establish a TCP connection to connect to the remote servers. the application works perfectly ok, but, after some time the entire process get's crashed and... (2 Replies)
Discussion started by: sudharma
2 Replies

8. UNIX for Advanced & Expert Users

memset vs calloc

Dear Friends, Can any one tell me the difference between memset and calloc function in C. Regards, Selvi (7 Replies)
Discussion started by: salvi
7 Replies

9. Programming

about memset fuction

Dear all, In my code,i am planning to use memset function to re-initialise an array before populating it everytime. Will using memset function be an overload to the program? (3 Replies)
Discussion started by: ranj@chn
3 Replies
Login or Register to Ask a Question