Valgrind - facing some problem

This is a discussion on Valgrind - facing some problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I have the following program pointertst.cpp --------- Code: #include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; int main() { char *c; ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    Valgrind - facing some problem

    Hi,
    I have the following program
    pointertst.cpp
    ---------
    Code:
    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    int main()
    {
      char *c;
      int *ip;
      c=(char*)malloc(100);
      ip=(int*)c;
      free(ip);
      cout<<"IN POINTER TEST";
      return 0;
    }
    I have used g++ -o hello.out pointertst.cpp

    Now the problem is when I use the following command it gives output as "valgrind: hello.out: command not found"

    command is:
    valgrind --tool=memcheck --leak-check=yes hello.out

    I'm using Linux Mint(Gloria) and valgrind is installed.

    Could anyone please help?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Presuming your program really is called "hello.out", and is located in the current directory, you need to specify it as "./hello.out" instead of "hello.out":

    Code:
    $ valgrind ./hello.out
    EDIT: BTW, if you expect Valgrind to produce some kind of warning for this code, it won't. There is no memory leak or out-of-bounds reference happening here. There is a dangling pointer, but Valgrind does not check for those.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Quote Originally Posted by brewbuck View Post
    Presuming your program really is called "hello.out", and is located in the current directory, you need to specify it as "./hello.out" instead of "hello.out":

    Code:
    $ valgrind ./hello.out
    EDIT: BTW, if you expect Valgrind to produce some kind of warning for this code, it won't. There is no memory leak or out-of-bounds reference happening here. There is a dangling pointer, but Valgrind does not check for those.
    Thank you for such quick response.
    You have mentioned about dangling pointer. Is it 'c' pointer in the above program you are referring to? If it is then I hope c=NULL will solve the problem?

    Thanks again!!

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Jones_C View Post
    Thank you for such quick response.
    You have mentioned about dangling pointer. Is it 'c' pointer in the above program you are referring to? If it is then I hope c=NULL will solve the problem?

    Thanks again!!
    Dangling pointers are no problems at all, just things you have to consider. Yes, c = NULL; after free would "fix" this.
    I personally don't do this unless NULL is simply a possible value of the pointer that can happen, or where there would be a possibility that I would free/delete the same pointer twice if I don't set it to NULL.

    Usually I believe you should just leave a dangling pointer as long as you're smart enough not to use the pointer after freeing it. With the exceptions I mentioned, that is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. facing problem with Buffer
    By enggabhinandan in forum C Programming
    Replies: 5
    Last Post: 11-22-2006, 05:26 AM
  3. facing problem in understanding this....
    By enggabhinandan in forum C Programming
    Replies: 10
    Last Post: 10-25-2006, 05:30 AM
  4. Replies: 2
    Last Post: 06-21-2006, 04:23 AM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

Tags for this Thread


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