Thread: How to measure the amount of bytes that a pointer points to

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    30

    How to measure the amount of bytes that a pointer points to

    Hi,

    Can someone show me how I can measure the amount of bytes that *ptr points to in the following piece of code? I don't know how to do that.

    Thanks for your help.

    Code:
    #include <memory.h>
    
    
    int main(int argc, char *argv[])
    {
    
    	char *ptr;
    	
    	ptr = malloc(3);
    
    	*ptr = "ab";
    	
    
    	return 0;
    
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can't do it. And your code is further wrong, because *ptr is char, not char*, so trying to assign const char* to char won't work.
    Use strcpy.
    You need to manually keep track of your allocation.
    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. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    I would prefer:

    Code:
    #include <stdlib.h>
    
    
    int main(int argc, char *argv[])
    {
    
    	char *ptr;
    	
    	//ptr = malloc(3*sizeof(char));
    
            // doing this ptr points to a constant string in the heap and you have
            // a memory leak of three bytes. Malloc is useless in that case.
            // ab string is already allocated in the memory
    	ptr = "ab";
    	
    
    	return 0;
    
    }

    malloc or *ptr = "ab" should be removed from your code.

    EDIT: Second here.
    Last edited by myle; 05-25-2008 at 01:22 PM.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    Ok, thanks for your help guys

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In the second case, it should be const char*, because a string literal is const.
    http://cpwiki.sourceforge.net/Common...kes_and_errors
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. mallocing with outside pointers
    By mart_man00 in forum C Programming
    Replies: 11
    Last Post: 07-04-2003, 02:17 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Pointer points to struct pointer
    By gogo in forum C Programming
    Replies: 4
    Last Post: 11-27-2001, 12:14 PM