Arrays and pointers to allocated memory

This is a discussion on Arrays and pointers to allocated memory within the C Programming forums, part of the General Programming Boards category; When memory is allocated during runtime it is easier to get the size of it by a function, rather than ...

  1. #16
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    When memory is allocated during runtime it is easier to get the size of it by a function, rather than seeking in the code. Sometimes it can be hard to find the howmany value.
    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

  2. #17
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    What on earth are you talking about?
    Why is it so hard to pass around the size value along with the pointer, which is so much more flexible since it allows you to use real arrays as well as dynamic memory (just look at fgets() for a prime example).

    Code:
    void foo ( char *ptr ) {
      // There is NOTHING you can do here to tell whether ptr is
      // a global variable, a local variable, or a malloc'ed variable
      // or tell exactly how many bytes are being pointed at.
    }
    
    char global[10];
    int main ( ) {
      char local[20];
      char *dynamic = malloc(30);
      foo( global );
      foo( local );
      foo( dynamic );
      foo( &dynamic[20] );  // is in dynamic memory, just not the start of it
    }
    Assuming that your magic function could take care of the last two cases, the first two would be much harder to solve. Such a function would have to return an error status of some sort, then what are you supposed to do - bail out?

    If it really bugs you that much, create a struct which contains the pointer and the size, and pass that around instead. Or move to C++ and use the additional creature comforts offered by std::vector
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #18
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    I don't have any problem. But there should be a function to read the header generated by malloc() even if you can write programs without it easily, thats all.
    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

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Write and track your own. There is no need to pollute the language with more junk because people are too lazy or stupid to pay attention to what they're doing. They did enough of that already in C99.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    I know what you say. But this is not like for example dynamic arrays. This is something that should being puted into language when it was created. I feel it should be standard in C an C++ but I don't insist on it.
    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

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

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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