Jake Evans
Jake Evans

Reputation: 1048

C - Pointers and Memory

I am a little confused, say in this example code;

ptrMem = createSharedMemory();  

ptrTemp = ptrMem;

ptrMem points to a shared memory location created by SHMGET

What is the point in having a ptrTemp pointing to it as well and how do I utilize both?

I want to loop through the memory and assign random numbers, e.g.;

ptrTemp = ptrMem;       
for(nCount = 0; nCount < 24; nCount++)
        {   
            x = rand() % 10000;
            *ptrTemp++ = x;
        }   

The situation is having a client and a server which communicated through shared memory. There needs to be a character or something similar which indicates to the other process that they can do their processing. So for example, after the above loop has finished; *ptrMem='*'.

Upvotes: 2

Views: 246

Answers (4)

Dave Rager
Dave Rager

Reputation: 8160

It's a matter of taste.

If you want to iterate through the memory by incrementing a pointer, then you need to keep a pointer to the beginning so it can be properly freed.

You could also use an index to iterate through the memory instead and not use the temporary pointer.

for(nCount = 0; nCount < 24; nCount++)
{   
    x = rand() % 10000;
    ptrMem[nCount] = x;
} 

Most instances I've seen (or done myself) when using a pointer to iterate through a memory buffer rather than just using an index is when the amount to advance the pointer isn't consistent. For instance, if your buffer always has the same data type in each element the compiler can know how much to increment the pointer (or calculate an index offset) for you.

struct mystruct* ptrMem;
ptrMem[0]; // first element
ptrMem[1]; // second element

struct mystruct* ptrTemp = ptrMem;
*ptrTemp; // first element
ptrTemp++; // increments ptrTemp by sizeof(struct mystruct) bytes
*ptrTemp; // second element

In this case it's a matter of taste which one you wish to use.

If for instance the compiler can't know the structure of the data contained in the buffer, such as processing a midi file where the commands have variable lengths, it can be easier (more readable) to iterate a pointer one byte at a time. Still, the compiler knows the size of a byte so you could use an index here as well.

Upvotes: 0

Mike
Mike

Reputation: 49473

  • What is the point in having a ptrTemp pointing to it as well and how do I utilize both?

It's the same idea as a "head" pointer in a linked list.

ptrTemp = ptrMem;     // here both pointers point to the start
                      //  of your shared memory location

As you run through you loop you perform this again and again:

*ptrTemp++ = x;

Now ptrTemp doesn't point to the start of your shared memory location anymore. In this specific example, you know that you looped 24 times (did 24 increments to your pointer) so you could use pointer arithmetic and "back off" the address, but why bother? For a few extra bytes you can just use a new pointer and always have a handy reference to the start of the buffer which will most likely need to be passed/referenced.

Keep in mind the name ptrTemp has "Temp" in the name, implying it's for some sort of temporary storage.


EDIT: The * operator is to dereference a pointer (or declare it as such). So in your code:

ptrTemp = ptrMem; // Assigning one pointer to another, no need for the * here since
                  // they are both pointers.

*ptrTemp++ = x; // here you are assigning a value to the memory pointed to by ptrTemp
                // to assign a value you need to dereference, then we increment ptrTemp
  • if I wanted to print the memory out, I'd have to do; ptrTemp = ptrMem, right?

Not really, that would just set the temp pointer back to the start of your shared memory buffer. If you wanted to print the contents of the memory out you could simply do:

for(nCount = 0; nCount < 24; nCount++)
    printf("%p: %d\n", ptrMem+nCount, *(ptrMem+nCount));

this doesn't change what ptrMem is pointing out (because we never overwrite it) we want to know what memory address (ptrMem+nCount) has what value (the dereferenced version of that) so we print a "%p" (pointer format string) and a "%d" (int format string)

Upvotes: 1

Davide Berra
Davide Berra

Reputation: 6568

I'd use the prtTemp if i'd need to change his value to access different memory areas of the shared memory.

In case i'd need to pointer to the first byte of the shared memory area i can use the untouched ptrMem

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409442

It's because in the loop you change the pointer ptrTemp. After doing ptrTemp++ the pointer no longer points to the original memory.

Upvotes: 2

Related Questions