473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

getting signal SEGV in my small program

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
5 6059
JosAH
11,448 Expert 8TB
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
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
JosAH
11,448 Expert 8TB
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
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
JosAH
11,448 Expert 8TB
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

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

Similar topics

1
by: Adi | last post by:
A java program we have written crashes with IBM JDK 1.3.1 on linux. It works fine on other platforms(Solaris,HPUx). It gets a SIGSERV Signal 11 and crashes just after few minutes after starting up....
10
by: Chris | last post by:
Could anyone write a small program to log the Signal-to-Noise figures for a Netgear DG834 router? I have been getting very variable SNR readings - and I would like to collect some evidence to...
2
by: Bram Stolk | last post by:
Hello, I catch SIGINT signals with a handler. In the handler, I use fd's to write some data to a child process. Is this valid? Because the data never arrives, and I wonder what I'm doing...
3
by: Martin McCormick | last post by:
A C program contains several signal statements to remove a lock file if the program gets killed: /*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/...
11
by: Jackie | last post by:
Hi everyone, I'd like to know when and how signals are used (e.g. SIGFPE, SIGABRT, SIGTERM, SIGSEGV, SIGINT)? Thank you so much.
2
by: gnutuxy | last post by:
Hi, I am newbie in the Unix system programming and in learning phase. I usually read the libc manual and then try to implement small programs to test/check the learnt thing. I read the libc...
26
by: steve | last post by:
Well I've been working all morning and have finally found the source of my "bus error (signal 10)" errors. The source is odd. The error occurs in any function where I make the function call: ...
13
by: vashwath | last post by:
Hi all, In my current project I am using signals for error handling. Since I cannot show full code, I have just shown important piece of code which is relevant. void sigsegenv() {...
2
by: rasmidas | last post by:
Hi, I am getting the following error while I am running my application. EXITING AddToEventLog() EXITING WriteNoDocProdEvent() EXITING SubstituteSummitMessages() EXITING dmgDocumentCreate ()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.