By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,204 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,204 IT Pros & Developers. It's quick & easy.

getting signal SEGV in my small program

P: 56
I have written a file as below:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char* items[2];
int i;
for(i=0;i<2;i++)
{
char* temp = new char[11];
strcpy(temp,"AB CD EFGH");
items[i] = temp;
printf("items is:%u=%u\n",&items[i],items[i]);
printf("items is:%s\n",*temp);
}
printf("\n\nitems is:%u\n",items);
printf("\nitems is:%u\n",&items);
items[2] = 0;
char** item_ptr = items;
while (*item_ptr != 0)
{
printf("item_ptr is:%u\n",*item_ptr);
printf("item_ptr is:%u\n",**item_ptr);
printf("item_ptr is:%u\n",item_ptr);
printf("item_ptr is:%d\n",item_ptr);
item_ptr++;
}
return 0;
}

While I am compiling it, it gives segmentation fault:
Here I am pasting

(L)SUMV3/sparse/src/stk/dmg_meta_doc$ gcc ras1.cc
(L)SUMV3/sparse/src/stk/dmg_meta_doc$ ./a.out
items is:4026512392=165408
Segmentation Fault

Now I tried to debug that using dbx which is as below:

(L)SUMV3/sparse/src/stk/dmg_meta_doc$ dbx a.out
changes (topic)
The major new features of this release relative to 6.0 are:

o The dbx environment variable "cfront_demangling" has been removed.
The "-F" option to dbx (allow cfront-demangling) has also been removed.
Use an earlier version of dbx to get cfront support.
o This release continues to support Fortran intrinsics on Intel, even
though the Intel Fortran compiler was not shipped with WorkShop 6.
The next dbx release will not support Fortran intrinsics on Intel.

See also `help changes60'


To suppress this message, add the following line to your .dbxrc file:

dbxenv suppress_startup_message 5.0
Reading a.out
dbx: warning: cannot demangle '__10bad_typeid'
dbx: warning: cannot demangle '__13bad_exception'
dbx: warning: cannot demangle '__14__si_type_infoPCcRC16__user_type_info'
dbx: warning: cannot demangle '__16__user_type_infoPCc'
dbx: warning: cannot demangle '__17__class_type_infoPCcPCQ217__class_type_info9b ase_infoUi'
Reading ld.so.1
Reading libc.so.1
Reading libdl.so.1
Reading libc_psr.so.1
(dbx) where
dbx: program is not active
(dbx) run
Running: a.out
(process id 23331)
items is:4026512168=165408
signal SEGV (no mapping at the fault address) in strlen at 0xef6a53e4
0xef6a53e4: strlen+0x001c: ldub [%o1], %o3
(dbx) where
dbx: program is not active
(dbx) run
Running: a.out
(process id 23331)
items is:4026512168=165408
signal SEGV (no mapping at the fault address) in strlen at 0xef6a53e4
0xef6a53e4: strlen+0x001c: ldub [%o1], %o3
(dbx) where
=>[1] strlen(0x0, 0x41, 0x0, 0x1, 0x3a, 0xef72c8ac), at 0xef6a53e4
[2] _doprnt(0x16ceb, 0x0, 0x0, 0x41, 0x0, 0x0), at 0xef6dc834
[3] printf(0x16ce0, 0xef72d134, 0xef725930, 0xefffb328, 0x16c00, 0xef698ea8), at 0xef6e5990
[4] main(0x1, 0xefffb3a4, 0xefffb3ac, 0x28518, 0x0, 0x0), at 0x12538
(dbx) quit

Here it is showing that the error is in the function strlen. Which I didnot use. Rather I used strcpy().

Please let me know what is wrong in my program and how I will print the string
"AB CD EFGH".
Thanks in advance.
Aug 9 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 10K+
P: 11,448
char* temp = new char[11];
strcpy(temp,"AB CD EFGH");
printf("items is:%s\n",*temp);
This is definitely wrong; temp is a char* (a pointer to a char) so *temp is a char
and you can't treat it as if it were a char* (%s)

kind regards,

Jos

pa. the SIG_SEGV occurs during *runtime*, not during *compile* time.
Aug 9 '07 #2

P: 56
So, can I write that as below:

char* temp = new char[11];
char buffer[11]="AB CD EFGH";
strcpy(temp,buffer);

Please suggest me. Thanks.
Aug 9 '07 #3

Expert 10K+
P: 11,448
So, can I write that as below:

char* temp = new char[11];
char buffer[11]="AB CD EFGH";
strcpy(temp,buffer);

Please suggest me. Thanks.
That's not needed; it the following line that is wrong:

Expand|Select|Wrap|Line Numbers
  1. printf("items is:%s\n",*temp);
  2.  
Make it read like this:

Expand|Select|Wrap|Line Numbers
  1. printf("items is:%s\n",temp);
  2.  
kind regards,

Jos
Aug 9 '07 #4

P: 56
One more question, In my program while debugging why
it is showing that the error is in the function strlen(). Which I didnot use. Rather I used strcpy(). Thanks.
Aug 10 '07 #5

Expert 10K+
P: 11,448
One more question, In my program while debugging why
it is showing that the error is in the function strlen(). Which I didnot use. Rather I used strcpy(). Thanks.
Maybe you didn't call that function, but most likely the printf() function called it.

kind regards,

Jos
Aug 10 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.