Memory Address

This is a discussion on Memory Address within the C Programming forums, part of the General Programming Boards category; Hello, I have a simple question. I've been writing a function that shows me the current address of a variables ...

  1. #1
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Memory Address

    Hello,

    I have a simple question. I've been writing a function that shows me the current address of a variables memory. So far there are multiple addresses when pointing to each of the pointers.

    My code is below, and was wondering if this is the correct route to finding the address of the variable:

    Code:
    void memoryAddress(const char *format, ...) {
    	int cmd, **arg = (int **)&format + 1;
    
    	while (*format) {
    		if (format[0] != '%') {
    			format++;
    			continue;
    		}
    
    		cmd = format[1];
    		format += 2;
    
    		switch( cmd ) {
    		case 'i':
    		case 'd':
    		case 'u':
    		case 'c':
    		case 'f':
    		case 'g':
    		case 's':
    			printf("0x%p\n", (void *)arg);	// address of data
    			break;
    		}
    		arg++;
    	}
    }
    
    int main() {
    	float myFloat = 0.0f;
    	char str[] = {"Hello"};
    
    	memoryAddress("%s %g", str, &myFloat);
    
    	return 0;
    }
    I do know if you get control of **arg you can write information to it. Like [*(float *)*arg] would allow you to write a floating point to your memory address.

    I just want to confirm this is all correct. Though if I display [*arg] or [arg] I get other address results. Does anyone know the difference between the three results? [**arg] [*arg] [arg]


    Edit: No this is not homework, im just curious about the different memory address results


    Thank you for your time,
    - Stack Overflow
    Last edited by Stack Overflow; 05-25-2004 at 12:16 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Why the printf style format string? If all you want to do is print the addresses of each argument then really all you need to do is use, say, an integer denoting the number of arguments as the first and then inside the function, have va_arg return a pointer to void:
    Code:
    #include <stdarg.h>
    #include <stdio.h>
    
    void
    memoryAddress(
      int n,
      ...
      )
    {
      va_list args;
    
      va_start(args, n);
      while (n--) {
        printf("0x%p\n", va_arg(args, void *));
      }
      va_end(args);
    }
    
    int
    main()
    {
      float f = 0.0f;
      char  s[] = "Hello";
    
      memoryAddress(2, s, &f);
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    That makes sense,

    Thanks alot your way is much simpler and well written.

    I'll have to keep the va args in mind. I guess I just got stuck with writing a similar function [sscanf()] then with the same mind set while writing this function.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    ^^ I see how you code works but, why such an aquard indention style prelude? Nice code though

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >why such an aquard indention style prelude?
    I assume you mean the function headers since K&R formatting with a two space indent is far from awkward. I'll break it down for you so that you can see my reasoning behind it:
    Code:
    /* 1 */ void
    /* 2 */ memoryAddress(
    /* 3 */   int n,
    /* 4 */   ...
    /* 5 */   )
    /* 6 */ {
    The return type is on its own line (1) so that the function name begins at column 1 on the next line (2). This is so that I can avoid complicated or tedious searches for the function definition. This is as opposed to calls to the function that would always have at least a two space indention (thus not be at column 1) and the function declaration which is more conventional:
    Code:
    void memoryAddress(int n, ...);
    Still on (2), I maintain the K&R style by beginning the argument list on the same line as the function name. There's no big reason for this other than I prefer it over:
    Code:
    function_name
      (
      args
      )
    Each argument (3) and (4) has its own line so that I can more easily comment them in a way that looks nice. This setup also avoids really long argument lists and line wrapping. A standardized short method is much better IMO than an ad hoc line break. Arguments are indented two spaces for readability purposes. The closing paren of the argument list is also on its own line for the same reason as the opening paren being on the same line as the function name. I prefer the look over this:
    Code:
    function_name(
      args)
    The opening brace of the function body (6) is always on column 1 just as the closing brace is for ease of navigation in vi (which I use occasionally ).

    You may be wondering why the new style, but I'll shock everyone and say that this was always my style, I just never used it here. I won't go into detail for personal reasons, no offense.
    My best code is written with the delete key.

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Okay. I use the { on the same line as a function like you well here is mine
    Code:
    int func(argc){
            stuff indented by a tab on vim and 8 spaces here
    } /*on its own line*/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 12:41 PM
  2. Pointer to specific memory address
    By elnerdo in forum C++ Programming
    Replies: 10
    Last Post: 05-19-2006, 08:35 AM
  3. how to get memory address applied in other program?
    By wu7up in forum C Programming
    Replies: 1
    Last Post: 03-01-2003, 01:44 AM
  4. Memory address?
    By Munkey01 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 09:04 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM

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