Thread: Structural Equivalence Array

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    Structural Equivalence Array

    Does anyone know how to test the structural equivalence of an array in C?

    I'm unsure if I should be tested the size of the array. Or the 1st element of the array (b/c the name is equivalent to a pointer to the first element).

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's the "structural equivalence of an array"? You cannot know, given just a memory address, if it is part of an allocated array, or just a single variable, or if you can even dereference that point in memory.


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

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by quzah View Post
    What's the "structural equivalence of an array"? You cannot know, given just a memory address, if it is part of an allocated array, or just a single variable, or if you can even dereference that point in memory.

    Quzah.
    this is what lead me to the possibility of using the sizeof(). Very confusing question to say the least..

    homework - Are two int arrays of different size "type equivalent" in C? - Stack Overflow

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Would strcmp help at all?

    http://linux.die.net/man/3/strcmp
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by Babkockdood View Post
    nope.. I was looking through some other commands like typeof() etc.. because of the structure of arrays it's very difficult to make a comparison. I've been searching for a hint for the past day or so before taking it to the forums. ughh..

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You still aren't telling us in plain English what it is you're trying to do.


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

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by quzah View Post
    You still aren't telling us in plain English what it is you're trying to do.


    Quzah.
    "That array constructor in C does not construct a new type and structural equivalence is applied to arrays. How can your write code to test this? i.e. how can you test that C uses structural equivalence between arrays?"

    verbatim.

    from my understanding arr[0-9] is structurally equivalent to arr[1-10]. so maybe I should just be checking the type of used within the array and the count of primitive types in the array.
    Last edited by DJPlayer; 02-26-2011 at 03:02 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I still don't know what it is you are trying to prove. You're trying to prove what? That two different sized arrays of the same element type are the same? How can you expect anyone to be able to help you if you can't put in simple clear terms what it is you think you're trying to prove? One more time:

    What is "structural equivalence"? - In your own words.


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

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by DJPlayer View Post
    "That array constructor in C does not construct a new type and structural equivalence is applied to arrays. How can your write code to test this? i.e. how can you test that C uses structural equivalence between arrays?"

    verbatim.
    You can't... because it doesn't.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by quzah View Post
    I still don't know what it is you are trying to prove. You're trying to prove what? That two different sized arrays of the same element type are the same? How can you expect anyone to be able to help you if you can't put in simple clear terms what it is you think you're trying to prove? One more time:

    What is "structural equivalence"? - In your own words.
    structural equivalence is basically the same structure (vs type name). I'm assuming with arrays we include that to be the same primitive data stored as well as it's amount of memory allocation.

    visually this is not a problem but to write code to test it.. ehh..

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by CommonTater View Post
    You can't... because it doesn't.
    Just a thought.. but notice I cannot use the = operator on array variables but I can use it on primitive data types that use name equivalence. Difference being == , vs. =. So possibly a way to demonstrate structure vs. name equivalence? Just a thought.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by DJPlayer View Post
    Just a thought.. but notice I cannot use the = operator on array variables but I can use it on primitive data types that use name equivalence. Difference being == , vs. =. So possibly a way to demonstrate structure vs. name equivalence? Just a thought.
    The single equals sign is an assignment a = b; You can assign any element of an array using indexing such as a[17] = b; but you cannot assign whole arrays.

    The double equals sign is a comparison if (a == b) you can test individual array elements using this such as if (a[17] == b) but you cannot test whole arrays.

    Why not? Because an array's name is a pointer. It's just a pointer to the first element of the array. What you end up doing is assigning one array's pointer to the other's address... leaking memory in the process.

    As a C programmer you are not working with managed code, there are no constructors or destructors, there's no garbage collector... it's 100% entirely up to you to keep track of what is what and how big it is, and to write code that does not leak memory, violate array boundaries, try to assign 200 bytes to an integer and so on... That's the power of this language and it's biggest weakness.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by DJPlayer View Post
    structural equivalence is basically the same structure (vs type name). I'm assuming with arrays we include that to be the same primitive data stored as well as it's amount of memory allocation.

    visually this is not a problem but to write code to test it.. ehh..
    An array in C does not know how much memory it occupies... that is, it does not know how big it is. It's up to you to keep track of that.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by DJPlayer View Post
    structural equivalence is basically the same structure (vs type name)
    This still doesn't explain what "structural equivalence" is. What's a "structure"? Obviously not a C struct, so what? Do you mean "identically the same in memory"? No, of course it wouldn't be the same - why would you expect an array that is size X to be the same thing as an array that is size Y?

    An array isn't a type specifier. It's a quantity specifier.

    Quzah.
    Last edited by quzah; 02-26-2011 at 05:32 AM.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by CommonTater View Post
    The single equals sign is an assignment a = b; You can assign any element of an array using indexing such as a[17] = b; but you cannot assign whole arrays.

    The double equals sign is a comparison if (a == b) you can test individual array elements using this such as if (a[17] == b) but you cannot test whole arrays.

    Why not? Because an array's name is a pointer. It's just a pointer to the first element of the array. What you end up doing is assigning one array's pointer to the other's address... leaking memory in the process.
    I didn't realize there was a memory leak involved in the mere use of the pointer in C. But essentially your statement of how C looks at arrays was going to be my attempt. The fact that you have a pointer to the first element obviously says something about it's structure. The funny thing about structure is that the bounds are not part of it. Which is why I stated arr1[0-9] is structurally equivalent to arr2[1-10]. We have to arrays of integers both w/ 10 elements both with integers.. now this is vs. name equivalence where C looks at the name to make a determination.. used in primitive types.

    essentially I'm trying to find a way to say these two arrays are structurally equivalent.

    Code:
     int x[] = { [0 ... 9] = 1 };
     int y[] = { [1 ... 10] = 1 };
    Last edited by DJPlayer; 02-26-2011 at 11:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM