Why am I getting EXC_BAD_ACCESS here?

  • Thread starter Thread starter Jamin2112
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a coding issue related to an EXC_BAD_ACCESS error encountered in a program. Participants explore the potential causes of this error, particularly focusing on the use of the strcmp function and memory management practices in C and C++.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant notes that strcmp requires string arguments and points out that passing a character constant like 'x' instead of a string literal could lead to an access error due to incorrect memory access.
  • Another participant expresses self-reflection on their mistake of using 'x' instead of "x" in the strcmp function.
  • A participant mentions the use of static analysis tools, like clang-analyser, which can identify such errors and reports potential memory leaks in the provided code.
  • There is a discussion about the use of malloc versus new in memory allocation, with one participant arguing that malloc can be faster and allows for more control over memory management, while also noting that malloc is specific to C.

Areas of Agreement / Disagreement

Participants generally agree on the importance of using the correct types for string functions, but there is a divergence in opinions regarding the merits of malloc versus new for memory allocation, indicating that multiple views remain on this topic.

Contextual Notes

Some participants highlight the limitations of the provided code snippet, noting that without additional context, it is difficult to fully diagnose the issue. There are also mentions of potential memory leaks that remain unresolved.

Jamin2112
Messages
973
Reaction score
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
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.
 
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".
 
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? :-)
 
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 19 ·
Replies
19
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
1K
Replies
17
Views
2K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 22 ·
Replies
22
Views
2K