Hiding identifiers of the header from other parts of the program

This is a discussion on Hiding identifiers of the header from other parts of the program within the C Programming forums, part of the General Programming Boards category; Hello, I have written two functions NewMalloc() and NewFree() which are similar to malloc() and free(). They have a global ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    Hiding identifiers of the header from other parts of the program

    Hello,
    I have written two functions NewMalloc() and NewFree() which are similar to malloc() and free(). They have a global variable of 50000 byte array and its where the memory is allocated dynamically. Everything is written in C , (not C++)

    my problem is that i have givent this array, the name(identifier) "NEWMALLOC_mem" which will not allow me to use this word as an identifier in my programs that use these two functions.
    (for now, the two functions, macros and the global variable are written in a file called newmalloc.c which is to be linked with my programs).

    in C++ this can be solved by using Namespace... but i want this on C..

    please help me...

    Thanks..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Deamonpog
    my problem is that i have givent this array, the name(identifier) "NEWMALLOC_mem" which will not allow me to use this word as an identifier in my programs that use these two functions.
    (for now, the two functions, macros and the global variable are written in a file called newmalloc.c which is to be linked with my programs).
    What exactly is the name conflict? The solution is simply to change one or both of the names, e.g., by using a prefix.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So don't put that variable name in the header. There's no reason to, anyway; just put it in the .c file only and everybody will be happy.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,293
    There is no namespace equivalent in C. The best you can probably do is prefixing the name with something, like foo_NEWMALLOC_mem instead of the C++ foo::NEWMALLOC_mem.

    I think the more important question is why would you want to use NEWMALLOC_mem as an identifier in the rest of your program? One, it sounds like you're using a global here, which is fine since you need access to the memory throughout the duration of the program, but it should probably be a static global, accessible only in newmalloc.c. Two, any pointer to memory "allocated" with NewMalloc should only get it's address from NewMalloc, and should never address NEWMALLOC_mem directly.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    In your .c file, you have

    Code:
    static char NEWMALLOC_mem[50000];
    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.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    well, i just want this to be perfect.. the whole thing works very good.. and only thing is this.. i used this "NEWMALLOC_" as the prefix as u can see... so thats how the array name ended up as "NEWMALLOC_mem"..

    i just want to know if there is another way to hide this..

    namespace in C++ do this in a good way... that we have to put "::" to call something inside a namespace..
    i was wondering if there is a way i can do it with C.
    (or at least you can tell me how its done inside of c++...)

    Thank you all for quick replies....

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Deamonpog View Post
    well, i just want this to be perfect.. the whole thing works very good.. and only thing is this.. i used this "NEWMALLOC_" as the prefix as u can see... so thats how the array name ended up as "NEWMALLOC_mem"..

    i just want to know if there is another way to hide this..

    namespace in C++ do this in a good way... that we have to put "::" to call something inside a namespace..
    i was wondering if there is a way i can do it with C.
    (or at least you can tell me how its done inside of c++...)

    Thank you all for quick replies....
    As we say, if it's not in the header file, then as far as anybody else is concerned it doesn't exist. What more could you possibly ask for?

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    This is what my code looks... i ddnt copy pastet the codes inside the functions because they are too long(about 300 lines)... i will put them if u want it....
    i dont need the word static rite?

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <errno.h>
    
    #ifndef NEWMALLOC_H
    #define NEWMALLOC_H
    
    /*-- PREPROCESSOR SUBSTITIUTIONS --*/
    // Core Basics
    #define MEMSIZE 50000	// Size of the Memory.(maximum is 65534, better to be an Even)
    #define MAS 8		// Minimum Allocation Size Permited in Bytes.
    #define MALLOCDATA 8	// Size of the data section of the Memory in Bytes.
    
    // Pointers in Data Section
    #define FREEHEAD 0		// Head Pointer
    #define SIZE 1			// Local Variable for storing the size
    #define PREVIOUS 2		// Previous Pointer
    #define CURRENT 3		// Current Pointer
    
    // nested(2nd level) directives
    #define ARRAYSIZE (MEMSIZE/2)
    #define MAXALLOWEDSIZE (MEMSIZE - MALLOCDATA)
    #define ENDOFNEXT MEMSIZE	// End of Next links
    
    
    /*-- MEMORY DEFINITION AND DATA SECTION INITIALIZATION --*/
    /*
    ----------------------------------------------------------------------------------
    	initialize to,
    [FREEHEAD][SIZE][PREVIOUS][CURRENT][B_Next][B_Prev][B_Free][].....[]
    
    FREEHEAD = MALLOCDATA+4
    SIZE = 0
    PREVIOUS = 0
    CURRENT = 0
    B_Next = ENDOFNEXT				// end of Next links
    B_Prev = 0						// end of Prev links
    B_Free = 0						// end of Free links
    ----------------------------------------------------------------------------------
    */
    unsigned short NEWMALLOC_mem[ARRAYSIZE] = { (MALLOCDATA+4), 0, 0, 0, ENDOFNEXT, 0, 0};
    
    
    
    void * NewMalloc(size_t size)
    {
           // long code
    }
    
    void NewFree(void * location)
    {
    	// long code
    }
    
    #endif

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Deamonpog
    well, i just want this to be perfect.. the whole thing works very good.. and only thing is this.. i used this "NEWMALLOC_" as the prefix as u can see... so thats how the array name ended up as "NEWMALLOC_mem"..

    i just want to know if there is another way to hide this..

    namespace in C++ do this in a good way... that we have to put "::" to call something inside a namespace..
    i was wondering if there is a way i can do it with C.
    (or at least you can tell me how its done inside of c++...)
    Bah, I misread you. I think tabstop gave you the first step of the solution and Salem completed it.

    Basically, one possible problem is that you are defining the array in the header file, thus it is defined in many translation units (i.e., each combination of that header and a source file in which the header is included), which is a Bad Thing. You should define the array in exactly one source file, i.e., the one in which you implement NewMalloc() and NewFree().

    But you don't even need to refer to NEWMALLOC_mem in the header at all. Yet, your next worry is that if somehow the name NEWMALLOC_mem is used somewhere else, perhaps accidentally or coincidentally, there will be a conflict. The solution is to declare the array static, as per Salem's example. This way, another use of the NEWMALLOC_mem name would not conflict. Since you know about C++ namespaces, note that this technique is akin to the use of an unnamed namespace.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Deamonpog View Post
    This is what my code looks... i ddnt copy pastet the codes inside the functions because they are too long(about 300 lines)... i will put them if u want it....
    i dont need the word static rite?

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <errno.h>
    
    #ifndef NEWMALLOC_H
    #define NEWMALLOC_H
    
    /*-- PREPROCESSOR SUBSTITIUTIONS --*/
    // Core Basics
    #define MEMSIZE 50000	// Size of the Memory.(maximum is 65534, better to be an Even)
    #define MAS 8		// Minimum Allocation Size Permited in Bytes.
    #define MALLOCDATA 8	// Size of the data section of the Memory in Bytes.
    
    // Pointers in Data Section
    #define FREEHEAD 0		// Head Pointer
    #define SIZE 1			// Local Variable for storing the size
    #define PREVIOUS 2		// Previous Pointer
    #define CURRENT 3		// Current Pointer
    
    // nested(2nd level) directives
    #define ARRAYSIZE (MEMSIZE/2)
    #define MAXALLOWEDSIZE (MEMSIZE - MALLOCDATA)
    #define ENDOFNEXT MEMSIZE	// End of Next links
    
    
    /*-- MEMORY DEFINITION AND DATA SECTION INITIALIZATION --*/
    /*
    ----------------------------------------------------------------------------------
    	initialize to,
    [FREEHEAD][SIZE][PREVIOUS][CURRENT][B_Next][B_Prev][B_Free][].....[]
    
    FREEHEAD = MALLOCDATA+4
    SIZE = 0
    PREVIOUS = 0
    CURRENT = 0
    B_Next = ENDOFNEXT				// end of Next links
    B_Prev = 0						// end of Prev links
    B_Free = 0						// end of Free links
    ----------------------------------------------------------------------------------
    */
    unsigned short NEWMALLOC_mem[ARRAYSIZE] = { (MALLOCDATA+4), 0, 0, 0, ENDOFNEXT, 0, 0};
    
    
    
    void * NewMalloc(size_t size)
    {
           // long code
    }
    
    void NewFree(void * location)
    {
    	// long code
    }
    
    #endif
    Half a jiff -- is this your header file (since you have an include guard at the top)? That's not at all what a header file should look like. A header file for this should look roughly like
    Code:
    #ifndef NEWMALLOC_H
    #define NEWMALLOC_H
    
    void * NewMalloc(size_t size);
    void NewFree(void * location);
    #endif
    ...and that's it. Prototypes for the function is pretty much all you want for a header. All that code should go in a .c file, which is compiled separately from the program that uses it (if you're being fancy about it) or at the same time as your program (if you're being less fancy).

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Quote Originally Posted by tabstop View Post
    As we say, if it's not in the header file, then as far as anybody else is concerned it doesn't exist. What more could you possibly ask for?
    sorry, i dont understand what you say.... maybe its my weak english.. pardon me... can u explain....

    what i said was that i want to use the same name( NEWMALLOC_mem) inside a program for another variable, but the program will also use this NewMalloc function. so it will make an error rite?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Deamonpog
    what i said was that i want to use the same name( NEWMALLOC_mem) inside a program for another variable, but the program will also use this NewMalloc function. so it will make an error rite?
    It depends. For example, if you declare another variable named NEWMALLOC_mem as a local variable in the main function, it would actually be fine. The problem comes if you declare another global variable named NEWMALLOC_mem. This is mostly a theoretical problem since you are already using a prefix to the name, and global variables should be rare anyway, but the solution, as demonstrated by Salem in post #5 and reiterated by me in post #9, is to declare this global array as static.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Quote Originally Posted by laserlight View Post
    It depends. For example, if you declare another variable named NEWMALLOC_mem as a local variable in the main function, it would actually be fine. The problem comes if you declare another global variable named NEWMALLOC_mem. This is mostly a theoretical problem since you are already using a prefix to the name, and global variables should be rare anyway, but the solution, as demonstrated by Salem in post #5 and reiterated by me in post #9, is to declare this global array as static.
    Ok, so will using static keyword solve the problem completely? i dont know about it very much.. so now i will look at it...

    thanks everybody for helping me.. thank you very much..

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Deamonpog
    Ok, so will using static keyword solve the problem completely?
    Yes, but you also have to move the definition of the array to the source file. If you leave it in the header, then each source file that includes the header will have its own version of the array, which is not what you want.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Quote Originally Posted by laserlight View Post
    Yes, but you also have to move the definition of the array to the source file. If you leave it in the header, then each source file that includes the header will have its own version of the array, which is not what you want.
    oh... thank you very much for that information too...
    also i tried it... yah... it works great... thanks....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 11:12 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Clocking a program, and parts of it
    By Boksha in forum C Programming
    Replies: 4
    Last Post: 03-18-2002, 05:02 PM

Tags for this Thread


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