Why am I getting EXC_BAD_ACCESS here?

  • Thread starter Jamin2112
  • Start date
In summary: Alloc is called with a pointer to the desired size. You can also do this with the realloc function, which allows you to change the size of the allocated memory.
  • #1
Jamin2112
986
12
Screenshot:

wB7Wnhg.png
Here's a full code dump if you think the issue could depend on a line not shown in the screenshot: http://codepad.org/zSE1vobz
 
Technology news on Phys.org
  • #2
strcmp compares two strings. You don't show enough code so that I can determine what rt->fx represents, but 'x' is a character constant. Since both args of strcmp are strings (i.e., of type char * or char []), strcmp attempts to access the characters that are pointed to by its two arguments. Passing in 'x' (which happens to have an ASCII code of 0x78) causes strcmp to attempt to access the byte at memory location 0x78. This causes the access error that you are seeing.

All of the string processing functions that are declared in string.h take pointers for their string arguments.
 
  • #3
Mark44 said:
strcmp compares two strings. You don't show enough code so that I can determine what rt->fx represents, but 'x' is a character constant. Since both args of strcmp are strings (i.e., of type char * or char []), strcmp attempts to access the characters that are pointed to by its two arguments. Passing in 'x' (which happens to have an ASCII code of 0x78) causes strcmp to attempt to access the byte at memory location 0x78. This causes the access error that you are seeing.

All of the string processing functions that are declared in string.h take pointers for their string arguments.

Thanks!

I knew that strcmp compares 2 strings ... I'm just an idiot who put 'x' instead of "x".
 
  • #4
Doesn't your IDE or static analyser catch this?

I ran clang-analyser on your code at http://codepad.org/zSE1vobz

It finds the error and reports 7 potential memory leaks. I looked at two at random, and they're both legit.

http://pastebin.com/9gkNijy3

Why do people use malloc? :-)
 
  • #5
Carno Raar said:
Why do people use malloc? :-)
It's faster than new, by a lot. If your'e doing low level memory handling, it allows allows you to do some nice tricks such as explicitly saying when you want a constructor called, or weird things like replacing new, delete, new, delete <- actually allocates and frees memory twice with malloc, ctor, dtor, ctor, dtor, free <- reuses the memory.

That also appears to be C, not C++. The only way to dynamically allocate memory in C is *alloc.
 

FAQ: Why am I getting EXC_BAD_ACCESS here?

1. Why am I getting EXC_BAD_ACCESS here?

EXC_BAD_ACCESS is a common error in programming that occurs when a piece of code attempts to access memory that has already been released. This can happen for a variety of reasons, such as trying to access a variable that has been deallocated or accessing an array out of bounds.

2. How do I fix the EXC_BAD_ACCESS error?

To fix the EXC_BAD_ACCESS error, you will need to carefully analyze your code to find out where the memory access issue is occurring. Use debugging tools and techniques to track down the source of the problem, and make sure to properly allocate and release memory as needed.

3. Can EXC_BAD_ACCESS be caused by a hardware issue?

No, EXC_BAD_ACCESS is typically a software issue and is not caused by hardware problems. However, it is possible for a hardware issue to manifest as an EXC_BAD_ACCESS error, so it's important to rule out any potential hardware problems before addressing the software code.

4. Is there a specific language or platform that is more prone to EXC_BAD_ACCESS errors?

No, EXC_BAD_ACCESS can occur in any programming language and on any platform. It is a fundamental error that can happen in any code that deals with memory management. However, some languages and platforms have built-in features and tools to help prevent and debug EXC_BAD_ACCESS errors.

5. Are there any best practices for avoiding EXC_BAD_ACCESS errors?

Yes, there are several best practices for avoiding EXC_BAD_ACCESS errors. These include properly managing memory allocation and release, using debugging tools to catch potential memory issues, and following coding conventions and best practices. It's also important to regularly test and debug your code to catch any potential EXC_BAD_ACCESS errors before they occur.

Similar threads

Replies
3
Views
3K
Replies
22
Views
2K
Replies
19
Views
3K
Replies
3
Views
581
Replies
22
Views
491
Replies
6
Views
1K
Back
Top