Thread: Arrays and pointers to allocated memory

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Arrays and pointers to allocated memory

    If you declare

    int x[500][1000];

    does x[i] give you the pointer to the first element in the i:th-array? Or is x[i] the whole i:th array itself?



    Also, a declaration

    char string[100];

    will give the expression sizeof(string) the value 100. But a declaration

    char *string;
    string = malloc(100);

    will give the same expression the value 4 cause then it's a ponter? Is there any way to se how large the allocated memory is? I mean, when you use free(string), it must be known somewere how big the allocated area is, right? And then you could be able to check how large area you allocated?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TriKri
    If you declare

    int x[500][1000];

    does x[i] give you the pointer to the first element in the i:th-array? Or is x[i] the whole i:th array itself?
    Test it yourself. Print the address of x[0]. Now print the address of x[1]. Do a bit of math, get your answer.

    Quote Originally Posted by TriKri
    Is there any way to se how large the allocated memory is?
    No.


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

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I see another recommendation to visit this page:
    http://www.torek.net/torek/c/pa.html

    because
    does x[i] give you the pointer to the first element in the i:th-array? Or is x[i] the whole i:th array itself?
    the first element is an array.

    Quote Originally Posted by TriKri
    Is there any way to se how large the allocated memory is? I mean, when you use free(string), it must be known somewere how big the allocated area is, right? And then you could be able to check how large area you allocated?
    Just remember how much you ask for.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Quote Originally Posted by TriKri
    If you declare

    int x[500][1000];

    does x[i] give you the pointer to the first element in the i:th-array? Or is x[i] the whole i:th array itself?
    The "whole array" is acctually a pointer to a block of memory. The address of the first element is the same address of this whole array. The answer is yes if you think this way. If not, then make some tests and take your conclusions.


    Quote Originally Posted by TriKri
    Also, a declaration

    char string[100];

    will give the expression sizeof(string) the value 100. But a declaration

    char *string;
    string = malloc(100);

    will give the same expression the value 4 cause then it's a ponter? Is there any way to se how large the allocated memory is? I mean, when you use free(string), it must be known somewere how big the allocated area is, right? And then you could be able to check how large area you allocated?
    Yes, but it depends on the operational system.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Mortissus
    The "whole array" is acctually a pointer to a block of memory.
    No it isn't. Not any more than a "whole int" is "acctually a pointer to a block of memory". Array names, though similar in most ways, are not pointers.


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

  6. #6
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Thanx for all the answers. Now I feel I understand the difference between arrays and pointers a little bit better.

    But what about writing
    Code:
    int myarray[];
    myarray = malloc(100 * sizeof(int));
    Does myarray really become an array or does myarray become a pointer? Now I am lost.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    int myarray[];
    is not a valid declaration.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by TriKri
    But what about writing
    Code:
    int myarray[];
    myarray = malloc(100 * sizeof(int));
    Does myarray really become an array or does myarray become a pointer? Now I am lost.
    myarray starts as a pointer, and it ends as a pointer to some memory. The memory can be addressed as an array just like other pointers.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is not a valid declaration.
    It was, deep in the mists of time.
    http://www.cs.bell-labs.com/who/dmr/primevalC.html
    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.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Is there any way to se how large the allocated memory is? I mean, when you use free(string), it must be known somewere how big the allocated area is, right? And then you could be able to check how large area you allocated?
    Good point. When malloc() (or new in C++) allocates memory it writes a header for it that keeps some metadata like the size of it. There should be a way of reading this header for example with a function lile getmeminf(), why not?

    And about arrays and pointers: http://www.cplusplus.com/doc/general/Arrptr.html
    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

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > There should be a way of reading this header for example with a function lile getmeminf(), why not?
    There might be, but there is no PORTABLE way to find out, short of writing your own wrapper around malloc and free to keep track of the size.

    So have a look through your compiler specific documentation, and decide for yourself whether the loss of portability is worth it.
    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.

  12. #12
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    If such thing exists it should be found in <new> header, but I didn't find any function to do such thing.
    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

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If such a thing exists (which it doesn't) in <new> (which it doesn't), it would be C++, not C.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    For C it should be found in stdlib.h among other related functions that are:
    Code:
    void *  calloc(__in size_t _NumOfElements, __in size_t _SizeOfElements);
    void   free(__inout_opt void * _Memory);
    void * malloc(__in size_t _Size);
    void * realloc(__in_opt void * _Memory, __in size_t _NewSize);
    It is strange to me that there is no such function. It should be standard in C/C++ and should take the pointer as argument and return its size at least. I don't know what information can be found in the header of allocated memory.
    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

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well since my code usually looks like
    Code:
    size_t howmany = 123;
    int *myArr = malloc ( sizeof *myArr * howmany );
    Why would I need a library function to tell me how many bytes have been allocated when I already have a variable telling me how many bytes have been allocated?

    And if you do pass that to another function with say
    foo ( myArr );
    Why is it so hard not to change it to
    foo ( myArr, howmany );
    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.

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, 04:07 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM