Memory and pointers

This is a discussion on Memory and pointers within the C Programming forums, part of the General Programming Boards category; Hi I am a noob in C programming and trying to learn. I came across some code that needs some ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Memory and pointers

    Hi

    I am a noob in C programming and trying to learn. I came across some code that needs some explanation from someone who can C.

    The program should malloc a big memory block and I should then use this memory block to manage objects with pointers.

    Code:
    struct list_el {
    	int id;
    
    	struct list_el *prev;
    	struct list_el *next;
    };
    
    typedef struct list_el item;
    
    int main(void)
    {
    	void *myMemory = malloc(11111111111);
    	struct list_el* item = (struct list_el*)myMemory;
    
    	//Instead of using malloc when creating an object of this struct, 	  can I do this:
    
    	int pos = add(pos) + item;
    
    }
    
    int add(int pos)
    {
    	item *curr;
    	curr = pos + sizeof(struct list_el);
    	curr->id = 1;
    
    	return curr;
    }
    I have no clear understanding of what
    Code:
    struct list_el* item = (struct list_el*)myMemory;
    do. Is my understanding of pointers and memory in this example wrong?

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I will never suggest anyone to go with this paradigm to understand the pointer and the memory. This is totally one big mess. After all I will be surprised if this code worked.

    Code:
    int pos = add(pos) + item;
    Pos not initialised an sending a garbage value to func add and which is obviously going to return and unknown memory address. Which may be perhaps beyond your allocated memory access.

    The amount memry your are allocating too big big than a long int. You should get a compiler warning on that?

    Your not checking the return value of malloc? Its more likely the malloc will fail to allocate such a huge memory unless or otherwise.

    Code:
    struct list_el* item = (struct list_el*)myMemory;
    I really dont think there is a need for this. You could still do the pointer offseting through myMemory. Unless other can find the need of it?

    Harish
    Last edited by ssharish2005; 10-11-2010 at 07:25 AM.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssharish2005 View Post
    I will never suggest anyone to go with this paradigm to understand the pointer and the memory. This is totally one big mess. After all I will be surprised if this code worked.
    I'll say!

    That's 11, 1s he's got there... 11gb of ram.

    In multitasking environments (which is every computer, nowadays) you use memory only as needed and release it as soon as you're done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem - malloc, pointers and vector
    By DrSnuggles in forum C++ Programming
    Replies: 18
    Last Post: 08-14-2009, 11:28 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  3. Newbie question: pointers, other program's memory
    By xxxxme in forum C++ Programming
    Replies: 23
    Last Post: 11-25-2006, 01:00 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM

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