Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Why am I getting EXC_BAD_ACCESS here?

  1. Jun 10, 2015 #1


    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
  2. jcsd
  3. Jun 10, 2015 #2


    Staff: Mentor

    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.
  4. Jun 10, 2015 #3

    I knew that strcmp compares 2 strings ... I'm just an idiot who put 'x' instead of "x".
  5. Jun 29, 2015 #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.


    Why do people use malloc? :-)
  6. Jun 30, 2015 #5
    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook