Passing Pointers or References?

This is a discussion on Passing Pointers or References? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Elysia You should not use raw pointers. Use std::string instead of char*, and std::tr1::shared_ptr, or other smart ...

  1. #16
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    Quote Originally Posted by Elysia View Post
    You should not use raw pointers. Use std::string instead of char*, and std::tr1::shared_ptr, or other smart pointers for other types instead of raw pointers.
    And for future reference, using Microsoft's IDE, you can use JIT debugging - that is, run it normally, let it crash and then choose to debug. Very handy for these kinds of things.
    I've discovered how convenient std::string is -- no more stupid crap. Requires a few work arounds here and there but for the most part it's all good.

    I decided that I'd create the char* as a class member variable. It's handy when it comes to needing to get a reference to the data I'm loading up and it's proved pretty useful so far.

    I've use the JIT debugger and, while it's been somewhat helpful, I'm still unable to really figure out how to get certain variables.

    Either way, I've localized the problem to a good bit of code that I wrote for the internal 'gui' system. Seems that somewhere in that code memory is being overwritten because as soon as I comment it out all other problems seem to be resolved. I don't know if this is a case of 'false logic' that will come back to bite me in the ass later but I still want to take a different approach to the GUI system anyway. Sometimes it's good to just throw away small sections of code and rewrite them... so my experience has shown at least.

    Thanks again for all of the tips and the overwhelming amount of support I've received!
    - Leeor

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    It's handy when it comes to needing to get a reference to the data I'm loading up and it's proved pretty useful so far.
    What exactly is this "reference" to the data you are talking about?
    Usually, you would use a smart pointer for this task, perhaps even std::string.

    As for the JIT debugger... what debugger are you using? People can offer assistance, but they must know which one you use.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    I use the built-in VS2009 Debugger. I've had a lot of sucsess with it but there are the nuances that can get confusing sometimes. I'm hesitant to ask about specifics because 1) I don't want to waste someone's time when my question could very well be answered on MSDN and 2) I try to stay away from platform/implementation specific questions in a general C++ forum.

    As for not using a std::string for storing file data, the problem is that PhysicsFS (http://icculus.org/physfs/), the Filesystem library that I'm using, requires an address to a char* to which it writes file data to as a series of bytes. Many projects have used it sucsesfully and so I imagine that I will be able to also -- so far it hasn't given me any problems and it provides exactly the sort of disk/file access I want (including transparent reading/writing to archives such as ZIP/PAK/WAD/7ZIP/etc, big huge plus).

    The 'data' I'm reffering to is basically a byte array stored in a char*. This is then passed to other components that then read whatever file I loaded using the Filesystem (e.g., an OGG would be loaded by the Mixer, a PNG would be loaded by the Renderer, a plain text XML file would be loaded by the Configuration Parser, etc.). I could simply pass this information out as a std::string but it makes it easier to just throw it out as a const char* as that's the 'format' that it's already in.

    I guess you could say my terminology isn't perfect. 'Reference' may have been a poor choice of words.

    I want to make clear that my issue is generally resolved. I do appreciate the continued support, suggestions, advice and efforts to offer additional assistance as I've learned a great deal over the last few days. It's great to know that there's a community I can turn to when I have questions and not be told to RTFM or STFI. Thanks again everybody!
    Last edited by leeor_net; 01-29-2009 at 02:05 AM.
    - Leeor

  4. #19
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    As for not using a std::string for storing file data, the problem is that PhysicsFS (http://icculus.org/physfs/), the Filesystem library that I'm using, requires an address to a char* to which it writes file data to as a series of bytes.
    You can use vector of chars in this case
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by leeor_net View Post
    I use the built-in VS2009 Debugger. I've had a lot of sucsess with it but there are the nuances that can get confusing sometimes. I'm hesitant to ask about specifics because 1) I don't want to waste someone's time when my question could very well be answered on MSDN and 2) I try to stay away from platform/implementation specific questions in a general C++ forum.
    Do that as you want, but there are a few here who has experience with the VS debugger (me included) and can give you hints, tips and guidance should you require it. Becoming a master programmer also means mastering a debugger, or so I would like to say.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    I can definitely agree with that. Debuggers are a beast all of their own and it takes a great deal of finess and some inginuity to be able to use them effectively. I like to think that I'm at least knowledgeable in using my debugger but I wouldn't call myself a master users. Far from it, actually.
    - Leeor

  7. #22
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by leeor_net View Post
    No, I can't. It doesn't crash in the debugger in either debug or release and everything appears to be normal which is what is making this so frustrating and why I've posted on cprogramming.com... It only crashes when I run the program outside the debugger which isn't exactly helpful.
    So run it outside, wait till it crashes. Then - without closing the error message - use aatach to process fature of the debugger and exemine the stack.

    Note that sometime stack could not be displayed even when the exe is compiled in the debug mode which in most cases indicates the memory overrun of some var declared on stack and calling return when the actual return address is overwritten with garbage
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #23
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    There is a way to get stack tracing in release mode when an exception is thrown, however, it requires a bit of work and some assembly code. There are examples of this in books specifically about Microsoft's debugger in Visual Studio. The MSVS debugger is powerful but is usually shipped as a lame duck most of the time. Most of the tricks you can do with it require external code or assembly and/or fairly decent knowledge of its internal workings.

    The VS debugger can attach to the process, however, you will only get an assembly output when you do a break all. It is not good at parsing the assembly and giving you good information about what is going on. However there are 3rd party debuggers that are good for this. One that comes to mind is OllyDebug. You can attach to your process, analyze the source, find all intermodular calls, all referenced text strings, and it even recognizes which calls are Win32 calls. It can break down the calls by parameter and show you what each parameter is. Very handy. Perhaps this will show you what is going on.
    Last edited by VirtualAce; 01-30-2009 at 04:37 PM.

  9. #24
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    I read in a Symantec security analysis of Vista that Vista has a technique to randomly allocate memory for programs. This way hacker or malware can not guess the place of data.
    For example when you do
    Code:
    int *password = new int;
    *password = 23124;
    ...
    delete password;
    The value of password is stiil there. It is called ghost, if remember correctly. You can read that if you know its address and have the permission to access that address (for example when the original program is terminated or has released the memory. A few time it made a hardtime debugging my code, because it worked correctly in debug mode but not in release mode. It was actually reading ghost value in debug mode. Vista may prevent this to happen.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #25
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    Quote Originally Posted by siavoshkc View Post
    The value of password is stiil there. It is called ghost, if remember correctly. You can read that if you know its address and have the permission to access that address (for example when the original program is terminated or has released the memory. A few time it made a hardtime debugging my code, because it worked correctly in debug mode but not in release mode. It was actually reading ghost value in debug mode. Vista may prevent this to happen.
    It's not really much to do with vista: it's more related to memory management in your runtime library that comes with your compiler.

    For performance reasons, when your program deallocates memory (or any other resource) the runtime library may choose not to request the operating system to physically deallocate it. That is the reason for "ghosting". However, a subsequent memory allocation might use that memory. The "might" is the reason that dereferening a deallocated pointer yields undefined behaviour (anything can happen, but might not).

    The operating system may play similar games (eg when the runtime library asks the OS to deallocate memory, the OS may choose not to get the hardware to deallocate and the next call to the OS for memory may retrieve the same address). That's the same thing as the runtime library does, albeit a second order effect. But, as a rough rule, operating systems do not protect a program from accesses of memory in that program's address space.

    Some OS's (eg OpenBSD) do randomise such things, but the attention is more often on overwriting the affected memory than with moving it around.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a string array to a function using pointers
    By asofaihp in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 11:31 AM
  2. Passing Pointers by reference
    By Bladactania in forum C Programming
    Replies: 10
    Last Post: 02-13-2009, 09:14 AM
  3. Passing pointers by reference
    By JOCAAN in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 10:02 PM
  4. Passing pointers to two-dimensional arrays of structs
    By dr.neil.stewart in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 10:25 AM
  5. problems passing pointers
    By doormat in forum C Programming
    Replies: 9
    Last Post: 04-11-2004, 04:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21