Thread: Pointer Help?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    Pointer Help?

    Is there any way to know how many bytes I allocated? Example:

    Code:
    char *mystring = (char *) malloc ( 100 * sizeof(char));
    mystring = "1234512345";
    Is there any way to get the "100" somehow?

    sizeof(mystring) returns 4
    strlen(mystring) returns the total number of bytes up to a null termination (10)

    How do I get "100"?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    There is no standard function to obtain the allocated size. Most compilers on windows generally have a funcion named _msize from <malloc.h> that retrieves the size of the dynamic buffer. There are alternatives on other operating systems, but these are not portable.

    Furthermore, just because you pass 100 to malloc doesn't mean that the CRT will necessarily allocate 100 bytes. In many cases the CRT will extend the size you passed (Usually to the closest power of 2)

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Also note the above is a memory leak. You should use strcpy or some other appropriate derivative to add "1234512345" to the buffer you allocated (mystring), and at some point free() it.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by tabstop View Post
    You don't.

    Hmm, ok. I guess I need to find another way to do this but I am having a lot of trouble.

    I have two strings, string1 and string_total.

    I am receiving data on string1 and then concatenating into string_total every time.

    How do I know when I should realloc string_total?

    Code:
    while(something)
    {
    	string1 = read_from_file();
    	
    	if ( string_total is getting full ) // This is the condition I need help with
    	{
    		string_total = (char *) realloc ( string_total, new_total_size );
    	}
    	
    	strncat( string_totall, string1, bytes_read );
    }

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    if(strlen(string_total) + 1 > SOMETHING)
    //Do stuff

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Paul22000 View Post
    Hmm, ok. I guess I need to find another way to do this but I am having a lot of trouble.

    I have two strings, string1 and string_total.

    I am receiving data on string1 and then concatenating into string_total every time.

    How do I know when I should realloc string_total?

    Code:
    while(something)
    {
    	string1 = read_from_file();
    	
    	if ( string_total is getting full ) // This is the condition I need help with
    	{
    		string_total = (char *) realloc ( string_total, new_total_size );
    	}
    	
    	strncat( string_totall, string1, bytes_read );
    }
    In C, it's your job to manage memory. You must always know the size of the memory you have allocated, and how much of it you have used. You should not fear using variables to keep count with -- for sure, you'll need to know how many bytes you get back from read_from_file.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Paul22000 View Post
    Code:
    if ( string_total is getting full ) // This is the condition I need help with
    There is no straightforward way to do so other than as stated, using a counter to keep track of the memory.
    The length of string1 is known completely after it has been read from the input file so in the first pass check if
    Code:
    100 >= strlen(string1)+1
    and in subsequent passes check if
    Code:
    (100*counter - strlen(string_total)) >= strlen(string1)+1
    If condition is true do the catenation otherwise realloc() string_total by a chunk of 100 bytes before doing the catenation.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Paul22000 View Post
    How do I know when I should realloc string_total?
    You can get exact numbers using printf and sizeof(), but these may only hold true for your particular system, so you would want to include something like
    Code:
    int x=sizeof(myimportantmeasurement);
    for convenience
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Thanks guys. I made the counter as suggested. Just a global variable that holds the initial size I set (the "100") and checks against it after every read, and increment if necessary. Thanks again.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Paul22000 View Post
    Thanks guys. I made the counter as suggested. Just a global variable that holds the initial size I set (the "100") and checks against it after every read, and increment if necessary. Thanks again.
    better avoid globals

    you could make a struct with 2 members - pointer and allocated size for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM