int pointer to memory address

This is a discussion on int pointer to memory address within the C Programming forums, part of the General Programming Boards category; Hi, Just want to know if int *Group_ID is acceptable to refer to memory address, I test and it works. ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    23

    int pointer to memory address

    Hi,

    Just want to know if int *Group_ID is acceptable to refer to memory address, I test and it works.
    Just a quick scrap of code without any mem management, ...

    Code:
    typedef struct Object{
    
          int *Group_ID;
          int somenumber;
    
    }Object;
    
    typedef struct Group{
    
         //some Data
         int *Object_ID; //this would actually be a list referencing objects
         int somenumber;
    
    }Group;
    
    
    function test_group(void *pt){
    
           Group *grp;
           grp = (Group *)pt;
           printf("%d\n",grp->somenumber);
    
    }
    
    
    //edited here:
    function test_object(void *pt){
    
           Object *obj;
           obj = (Object *)pt;
           printf("%d\n",obj->somenumber);
    
    }
    
    
    function testgroup(void){
    
           Object someobj;
           Group *g, GRP;        
           //....
    
           g = &GRP;
           g->somenumber = 77;
           g->Object_ID = &someobj;
    
           someobj->Group_ID = &GRP;
           someobj->somenumber = 88;
    
    
           test_group(someobj->Group_ID);
           test_object(g->Object_ID);
           
    
    }
    Last edited by vurentjie; 03-23-2009 at 12:12 PM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    why do you store pointer to struct in the pointer to int?

    or declare teh pointer with correct type using forward declaration, or use void*
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    23

    reply

    I do realize I can use forward declaration and actual type - which is probably the best idea, but I wanted to know if it would be acceptable.

    I think I should have casted the address,

    Code:
           someobj->Group_ID = (int *)&GRP;
           g->Object_ID =  (int *)&someobj;
    Anyway perhaps my question is not useful.

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by vurentjie View Post
    I do realize I can use forward declaration and actual type - which is probably the best idea, but I wanted to know if it would be acceptable.

    I think I should have casted the address,

    Code:
           someobj->Group_ID = (int *)&GRP;
           g->Object_ID =  (int *)&someobj;
    Anyway perhaps my question is not useful.
    A pointer is always as many bytes as your architecture - int, char, void and whatever pointers are always 4 bytes on a 32-bit architecture. Same applies on 64-bit, but they're 8 bytes.

    It's basically the same thing, but it makes your code that much harder to read. Why do you want to do this?
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    why not to use void* ?
    standard garantees that you can cast any pointer to void* and back and get the same pointer. Nothing is said about casting to int* and back
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    23

    reply

    Why do you want to do this?
    I don't !!...anymore. I will use specific type or void depending on how many different types I need to assign to an id.


    I am experimenting with data structures, and more specifically want to create objects that are independent of groups but can be referenced by groups. So that objects can belong to more than one group but can also be dealt with as 'all objects' irrespective of which group they belong to.

    This is probably not that interesting out of context or for learned programmers. As I say, I am experimenting with c programming data structures, and was wondering about the int pointer to a memory address.

    So what type is a memory address?? For interest's sake...

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by vurentjie View Post
    So what type is a memory address?? For interest's sake...
    A memory address is an integer. C and C++ distinguish between integers and pointers, but essentially they are just numbers.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    23

    re

    thanks,

    i spent a good deal of today reading and reading on c and memory,

    today i realized that my understanding of c-syntax and coding is reasonably ok, but i have been lacking in the general understanding of memory management,

    i spent a good deal more reading up on memory alignment and padding today, so i thought i would add this here....for whatever reason, probably because before today i would easily have done something silly like this.

    from the book Memory as a Programming Concept in C and C++
    by Frantisek Franek

    Code:
    char i;
       int* p = (int*) &i;
       ...
       *p = 1234567892;
       ...

    No compiler will complain; everything seems fine. Yet clearly we are storing a binary code for the integer value 1234567892 that takes 32 bits (01001001100101100000001011010100) at the address of the variable i that has the size of 8 bits

    There are several possible outcomes.

    If the whole part X of the memory in Figure 3.1 belongs to the running program (process), then:

    if X does not contain any data important for the rest of the execution of the program, then the program runs fine and there is no apparent problem;

    if X does contain important data that are overridden by the 100101100000001011010100 tail of the binary code but by pure chance this does not change anything (as the data stored therein just happened to be the same), then the program runs fine and there is no apparent problem;

    if X does contain important data that are overridden and thus changed, then

    incorrect results may be produced or

    the program may crash with all kinds of possible error messages.

    If all or part of X belongs to some other process, then the program is terminated by the operating system for a memory access violation (the infamous UNIX segmentation fault error).




    i think my first post till now i have a much better idea of how a c program needs to be structured for effeciency, although now i need to implement what i have been learning

    anyway thanks for the helpful forum ...later

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by IceDane View Post
    A pointer is always as many bytes as your architecture - int, char, void and whatever pointers are always 4 bytes on a 32-bit architecture. Same applies on 64-bit, but they're 8 bytes.
    Pedanticism:
    The standard says no such thing - it has some guarantees about pointers being compatible (particularly that any pointer can be cast to void * and back to it's original form). There are however architectures where a char * is different from an int * - the reason being that the machine uses "word addressing", so a pointer points at a machine-word (e.g. a 32-bit aligned address). To get to individual bytes within that word, you have to use a separate register to say "extract byte X from address A" - so a char * would need to store the X and the A component. void * would also do that, to guarantee that the void * can store and restore a char *.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by vurentjie View Post
    thanks,

    i spent a good deal of today reading and reading on c and memory,

    today i realized that my understanding of c-syntax and coding is reasonably ok, but i have been lacking in the general understanding of memory management,

    i spent a good deal more reading up on memory alignment and padding today, so i thought i would add this here....for whatever reason, probably because before today i would easily have done something silly like this.

    from the book Memory as a Programming Concept in C and C++
    by Frantisek Franek


    i think my first post till now i have a much better idea of how a c program needs to be structured for effeciency, although now i need to implement what i have been learning

    anyway thanks for the helpful forum ...later
    Some compilers may warn about truncation in that example.
    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.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    23

    another question

    I thought I'd put this here, as another follow on,

    I am trying to understand something else about how memory will be organized in examples such as the following...

    Code:
    struct OfSize8{OfSize8 *next };
    
    struct OfSize16{  OfSize16 *next};
    If each of these lists are initialized I assume that each list node in the respective list will be set contiguously in memory - also meaning that each list will be allocated a block of memory starting at a different offset.

    If during program runtime OfSize16 is dynamically allocated space for X amount of nodes, but later none of these nodes are in use. This memory block has not been freed (but might have been set to zero or some such) and is still available for re-use. Re-using this memory for the same type of Object seems relatively easy.

    What would need to happen if a different type of object of the same size wanted to use this memory block? Would it be correct to assume that it would merely involve running through the memory from the original head and allocating it to the new list of objects, and removing pointers from the old list.

    Would it be possible to take this memory block which is a multiple of 16 (and therefore of 8) and re-use it on the list OfSize8? What does this entail?

    Any brief explanations would be appreciated. And any corrections to my understanding...

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    You can do that. The reason is that to the computer - it's just bits, or bytes. It doesn't care what's stored in there.
    This is the same way malloc works - it just allocates a raw number of bytes needed for your data and you treat it like a certain type.
    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: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM
  3. 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
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

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