Program received signal: “EXC_BAD_ACCESS”?


 
Thread Tools Search this Thread
Top Forums Programming Program received signal: “EXC_BAD_ACCESS”?
# 1  
Old 05-21-2011
Program received signal: “EXC_BAD_ACCESS”?

I am making a command line program in C using XCode. When running the program, it initially does what it is supposed to do (asks me for a file path). However, when I type in a valid and existing file path, it gives me the following error:
Code:
Program received signal:  “EXC_BAD_ACCESS”. sharedlibrary apply-load-rules all (gdb)

I have two warnings in my program, both of which have to do with the function strcat. The warnings are:
Code:
warning: implicit declaration of function 'strcat'

and
Code:
warning: incompatible implicit declaration of built-in function 'strcat'

I am wondering why my program is not executing properly.

Thanks,
Mike

My code is posted below:

Code:
#include 
   #include "stdlib.h"
   int main (void)
   {
       char *string1;
       printf("Type in your file path: ");
       scanf("%s", string1);
       char *string2 = "tar czvf YourNewFile.tar.gz ";
       strcat(string2, string1);
       system(string2);
       
   }


EDIT: Maybe it has to do with allocating the chars?

Last edited by mdonova33; 05-21-2011 at 06:56 PM..
# 2  
Old 05-21-2011
For strcat to work you'll have to include strings.h too.
# 3  
Old 05-21-2011
Quote:
Originally Posted by pludi
For strcat to work you'll have to include strings.h too.
It seems to be giving me the error as soon as scanf tries to save my entry.
# 4  
Old 05-21-2011
Code:
char *string1;

This is a character pointer. On a 32-bit system, it takes 4 bytes, on a 64-bit system, 8 bytes. When I type in 'slartibartfast', where is it going to put it?

Inside the pointer? No, not enough room, and besides -- that's not how pointers work. The idea of a pointer is that it points to the memory you want to use, right?

So what memory have you got the pointer pointing to? Nothing? Uh-oh.

That leaves it at a random garbage value. Since pointers have no magic power of predicting what you want to do to them, it doesn't create memory for you to put things in and the pointer remains at what it started at -- some random garbage value. So scanf tries to store it in some undefined location in memory which naturally crashes

You should make the pointer actually point to something if you expect it to work. char string1[128]; or char *string1=malloc(128);
# 5  
Old 05-23-2011
Quote:
Originally Posted by Corona688
Code:
char *string1;

This is a character pointer. On a 32-bit system, it takes 4 bytes, on a 64-bit system, 8 bytes. When I type in 'slartibartfast', where is it going to put it?

Inside the pointer? No, not enough room, and besides -- that's not how pointers work. The idea of a pointer is that it points to the memory you want to use, right?

So what memory have you got the pointer pointing to? Nothing? Uh-oh.

That leaves it at a random garbage value. Since pointers have no magic power of predicting what you want to do to them, it doesn't create memory for you to put things in and the pointer remains at what it started at -- some random garbage value. So scanf tries to store it in some undefined location in memory which naturally crashes

You should make the pointer actually point to something if you expect it to work. char string1[128]; or char *string1=malloc(128);
A string of characters such as "ABCD" is a character array. That we all know.

But an array evaluates to the address of it's first element, so when you see "ABCD" in an expression, it is effectively treated as a char *.

So the statement
Code:
char *string = "ABCD"

is perfectly valid. The pointer "string" is a char * value that is assigned the address of the first element of the "ABCD" array.

The OP's problem is he's trying to concatenate another string on the end of a static, constant array - his strcat() call is trying to write to the memory immediately following where his "tar czvf YourNewFile.tar.gz " string is stored.

And that appears to be read-only memory. Which is better than nuking whatever data is there.
# 6  
Old 05-24-2011
Quote:
Originally Posted by achenle
The OP's problem is he's trying to concatenate another string on the end of a static, constant array
Check again. He's trying to scanf into an uninitialized character pointer(string1).

That said, the problem you noticed will cause it to crash too! Smilie
# 7  
Old 05-24-2011
I thought you were commenting on the assignment of an array to a pointer.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Any help gratefully received

Multiple questions coming your way. A little back ground info as to why I am back to basics. A dear friend of mine passed away recently and as a request by him I have promised to keep his project alive. The problem I have it was being handed over when he was in the last weeks of his life and... (1 Reply)
Discussion started by: nobbyall
1 Replies

2. UNIX for Beginners Questions & Answers

Error received

I have a program that i need to get done that gets the person's name and his grade then prints it in this order "name-grade-gradeletter" so i wrote this code: #!/bin/bash while :; do read -p "Enter the person's name: " name read -p "Enter the grade of the person: " grade case $grade in )... (3 Replies)
Discussion started by: UniverseCloud
3 Replies

3. Forum Support Area for Unregistered Users & Account Problems

No activation email received

I have tried to register today with the username BatterBits, but have not received the activation email I do not live in any of the restricted countries - unless the UK has suddenly become a threat! I'd be grateful if you would assist. many thanks Ian (2 Replies)
Discussion started by: unregistered
2 Replies

4. Programming

Why do I receive Program received signal SIGABRT, Aborted?

Im using gdb and when a user disconnects from my server I receive a message Program received signal SIGABRT, Aborted. 0x7ffe0304 in ?? () I was hoping someone here might have a explination for this message in gdb (26 Replies)
Discussion started by: Errigour
26 Replies

5. Programming

Please help:program hang stuck there signal handling on POSIX Message Queue UNIX C programming

in a single main() function,so need signal handling. Use Posix Message Queue IPC mechanism , can ignore the priority and other linked list message,to implement the scenario: client:Knock Knock server:who's there client: Eric Server:Eric,Welcome. client:exit all process terminated ... (1 Reply)
Discussion started by: ouou
1 Replies

6. AIX

nim: error signal number 2 received

Hi to all, i am trying to make mksysb backup of a NIM client machine from NIM master and while i am reading that the backup is done successfully i get an error message below and it doesnt exit the smit screen. also the status of the command appears to be running. is there anybody who knows why... (3 Replies)
Discussion started by: omonoiatis9
3 Replies

7. AIX

Received signal #11, SIGSEGV [default] on AIX 6.1

Hello, One of our customer is getting segmentation fault when he runs his shell script which invokes our executable on AIX 6.1. On AIX 5.3, there were no issues. Here is the truss output. 811242: __loadx(0x0A040000, 0xF0D3A26C, 0x00000000, 0x00000009, 0x00000000) = 0xF026E884... (0 Replies)
Discussion started by: erra_krishna
0 Replies

8. Forum Support Area for Unregistered Users & Account Problems

no email received

I have tried to register and got a message saying an email would be sent but no email has turned up. Could you try again please? (1 Reply)
Discussion started by: lookingfor help
1 Replies

9. Programming

Program received signal SIGSEGV, Segmentation fault.

Dear all, I used debugger from C++ and these are the message I got: Program received signal SIGSEGV, Segmentation fault. 0x00323fc0 in free () from /lib/tls/libc.so.6 (gdb) info s #0 0x00323fc0 in free () from /lib/tls/libc.so.6 #1 0x00794fa1 in operator delete () from... (5 Replies)
Discussion started by: napapanbkk
5 Replies

10. Programming

Program received signal SIGABRT, Aborted.

I ran degugger in C++ and the followings are the message I got: Program received signal SIGABRT, Aborted. 0x002a57a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2 (gdb) info s #0 0x002a57a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2 #1 0x002e97f5 in raise () from /lib/tls/libc.so.6... (1 Reply)
Discussion started by: napapanbkk
1 Replies
Login or Register to Ask a Question