Thread: Checking null vector pointers

  1. #1
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203

    Checking null vector pointers

    If I were to declare a vector of complex classes e.g.
    Code:
    std::vector<MyClass*> vec;
    , resize it (or not. either way it returns the same segmentation fault), and then check it by saying
    Code:
    MyClass*& a = vec[0];
    
    if (a != (MyClass*)NULL) { etc; }
    . Why would checking for null return a segfault? I've never had this problem before with vectors or just dynamically declared pointers. I tried resizing it, and initializing all the members to NULL.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Did you check that the vector is not empty but actually has an element vec[0] ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    I did not check whether the vector was empty but I did make sure it wasn't by resizing it. What are you getting at?
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you don't need to cast NULL to your pointer type. if you're using G++ >=4.6 or visual studio 2010, you can use the nullptr keyword instead of NULL. it is part of the new standard that was just approved this year. I also think it's unnecessary to make a reference to a pointer, unless you're planning to modify the pointer inside the vector.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Rodaxoleaux View Post
    I did not check whether the vector was empty but I did make sure it wasn't by resizing it. What are you getting at?
    Probably that about the only way it can cause a seg fault is if you've essentially created a null-reference, one of those things that logically does not exist, but at the low level bad code can end up effectively putting you there. You'd only get that by dereferencing NULL, which should only happen if the container was empty I expect.
    Otherwise, you're probably not getting a segfault precisely where you think you are.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Code:
    #include <vector>
    
    class MyClass
    {
    public: 
    	int some_int;
    };
    
    int main()
    {
    	std::vector<MyClass*> vec;
    
    	vec.resize( 100, NULL );
    
    	MyClass*& a = vec[0];
     
    	if (a != NULL) 
    	{ 
    		//etc; 
    	}
    
    	system( "pause" );
    
    	return 0;
    }
    This does not segfault on my system. If it does on yours... I don't know Otherwise, check the rest of your code, maybe you aren't resizing or you are clearing it somewhere else.

    Please note that "MyClass*&" is quite useless... a simple pointer would most likely be enough.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Cool points for nvoigt and iMalc. True. It being empty does create segfaults. I didn't know that. @Elkvis: C++0x. Oh, the greatness that is sure to produce.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector checking
    By cuo741 in forum C++ Programming
    Replies: 14
    Last Post: 09-13-2010, 01:24 PM
  2. checking for null
    By rEtard in forum C Programming
    Replies: 7
    Last Post: 02-25-2005, 01:15 PM
  3. FILE pointer null checking problem
    By techrolla in forum C Programming
    Replies: 12
    Last Post: 01-06-2004, 05:38 PM
  4. null pointers
    By ygfperson in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2002, 07:49 PM
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 11:13 PM