473,659 Members | 2,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer Increment

Dear All,

Which is correct ?

Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);

If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?

Thanks in advance
Ranjeet

Nov 15 '05 #1
15 2674
ra***********@g mail.com wrote:
Which is correct ?
Neither, as usual.
Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);
This is not the right way to call malloc().
If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?


No. It will point to the 10th int.

Richard
Nov 15 '05 #2

ra***********@g mail.com wrote:
Dear All,

Which is correct ?

Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);

If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?
It will point to 10th integer location. int *PtrMemoryBlock = (int *)malloc(100); Assume PtrMemoryBlock = 0. PtrMemoryBlock = PtrMemoryBlock + 10; After the above operation PtrMemoryBlock will be 40.
10 * 4 = 40th byte location.
4 is the size of the integer.
10 is the increment value
Thanks in advance
Ranjeet


Nov 15 '05 #3
ranjeet.gu...@g mail.com write:
will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?

neither ,
it points to memory (PtrMemoryBlock just points to) loaction of Nth
byte,
and, N = 10 * sizeof(int).
pointer "PtrMemoryBlock " is declared to point to int type, so while
doing an addition of 10 to the pointer , 10 * sizeof(int) is added in
fact.

Nov 15 '05 #4

Robben wrote:
ra***********@g mail.com wrote:
Dear All,

Which is correct ?

Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);

If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?
It will point to 10th integer location.
int *PtrMemoryBlock = (int *)malloc(100);

Assume PtrMemoryBlock = 0.
PtrMemoryBlock = PtrMemoryBlock + 10;

After the above operation PtrMemoryBlock will be 40.
10 * 4 = 40th byte location.
4 is the size of the integer.
10 is the increment value


Here is the code

int main ()
{
int *MemoryBlockPtr = malloc(100);
printf("%p\n", MemoryBlockPtr) ;
MemoryBlockPtr = MemoryBlockPtr + 10;
printf("%p\n", MemoryBlockPtr) ;
return 0;
}

Thanks for your explanation as I was also wondering about the above
40, to figure out mathematically, I was able to conclude the above
conclusion, (Which you to stated)

Now as you have said and seems to me that you are confirm, then,
please let me know why multiplication is done with the size ?

Now If the normal pointer of the array is incremented then there
is no such multiplication.

Anyway Looks to me that I am missing some thing ? Please clarify.

Can we fix and point to the 10th int what is the correction factor,
Is this Universially Accepted (Correction factor is there) ????

Regards
Ranjeet


Thanks in advance
Ranjeet


Nov 15 '05 #5

ra***********@g mail.com wrote:
Robben wrote:
ra***********@g mail.com wrote:
Dear All,

Which is correct ?

Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);

If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?
It will point to 10th integer location.
int *PtrMemoryBlock = (int *)malloc(100);

Assume PtrMemoryBlock = 0.
PtrMemoryBlock = PtrMemoryBlock + 10;

After the above operation PtrMemoryBlock will be 40.
10 * 4 = 40th byte location.
4 is the size of the integer.
10 is the increment value


Here is the code


#include <stdio.h>
#include <stdlib.h> int main ()
{
int *MemoryBlockPtr = malloc(100);
If you are trying to allocate room for say 100 integers, say so:
int *MemoryBlockPtr = malloc( 100 * sizeof *MemoryBlockPtr ); printf("%p\n", MemoryBlockPtr) ;
%p requires you to pass a pointer to void, so:
printf("%p\n", (void *)MemoryBlockPt r);
MemoryBlockPtr = MemoryBlockPtr + 10;
printf("%p\n", MemoryBlockPtr) ;
printf("%p\n", (void *)MemoryBlockPt r);
return 0;
}

Thanks for your explanation as I was also wondering about the above
40, to figure out mathematically, I was able to conclude the above
conclusion, (Which you to stated)

Now as you have said and seems to me that you are confirm, then,
please let me know why multiplication is done with the size ?
For a type T,
T array[HUGE];
T *pt = array;
pt = pt + int_offset;
is read by the compiler as
pt = pt + int_offset * sizeof *pt;
Now If the normal pointer of the array is incremented then there
is no such multiplication.
What is a normal pointer? Anyway Looks to me that I am missing some thing ? Please clarify.

Can we fix and point to the 10th int what is the correction factor,
Is this Universially Accepted (Correction factor is there) ????

Regards
Ranjeet


Thanks in advance
Ranjeet


Nov 15 '05 #6
ra***********@g mail.com wrote:
Robben wrote:
ra***********@g mail.com wrote:
Dear All,

Which is correct ?

Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);

If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?
It will point to 10th integer location.
int *PtrMemoryBlock = (int *)malloc(100);

Assume PtrMemoryBlock = 0.
PtrMemoryBlock = PtrMemoryBlock + 10;

After the above operation PtrMemoryBlock will be 40.
10 * 4 = 40th byte location.
4 is the size of the integer.
10 is the increment value


Here is the code

int main ()
{
int *MemoryBlockPtr = malloc(100);
printf("%p\n", MemoryBlockPtr) ;
MemoryBlockPtr = MemoryBlockPtr + 10;
printf("%p\n", MemoryBlockPtr) ;
return 0;
}

Thanks for your explanation as I was also wondering about the above
40, to figure out mathematically, I was able to conclude the above
conclusion, (Which you to stated)

Now as you have said and seems to me that you are confirm, then,
please let me know why multiplication is done with the size ?

Now If the normal pointer of the array is incremented then there
is no such multiplication.

It will be the same... as array also holds location the pointer
location.
Try out!!
It completely depends on what array is holding
For char/unsigned char there will not be any multiplication as it char
size is 1.
for short it will be multiplied by 2 and so on..

Please look into Sumans comments as there is something wrong in the
sample code provided
If you are trying to allocate room for say 100 integers, say so:
int *MemoryBlockPtr = malloc( 100 * sizeof *MemoryBlockPtr );

Anyway Looks to me that I am missing some thing ? Please clarify.

Can we fix and point to the 10th int what is the correction factor,
Is this Universially Accepted (Correction factor is there) ????

Regards
Ranjeet


Thanks in advance
Ranjeet


Nov 15 '05 #7
It will point to
(initial_addres s)+(num_times_t o_increment)*(s ize_of_data_typ e)

Nov 15 '05 #8

ke******@hotmai l.com wrote:
ranjeet.gu...@g mail.com write:
will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?

neither ,
it points to memory (PtrMemoryBlock just points to) loaction of Nth
byte,
and, N = 10 * sizeof(int).
pointer "PtrMemoryBlock " is declared to point to int type, so while
doing an addition of 10 to the pointer , 10 * sizeof(int) is added in
fact.


Thanks to all for your quick Response, Well I cleared doubts
while going thourgh the threads, Was bit confused, but I am clear :-)
Well in one slot, I asked some REALLY Stupid Question :-)

Reagrds
Ranjeet

Nov 15 '05 #9
ah*********@gma il.com wrote:
It will point to
(initial_addres s)+(num_times_t o_increment)*(s ize_of_data_typ e)


What will? I suggest you read the posts that are made almost daily to
this group or read CBFalconer's sig which is on all his posts and has
instructions. Had you actually read a few days posts as you are meant to
with ALL groups before posting you would have seen these instructions.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2339
by: Asfand Yar Qazi | last post by:
Hello. Partly for learning purposes, I have written a smart pointer class. Could you please tell me what's wrong with it? (I know there's something wrong with it, but just not what!) Note that, as well as reference counting the stored pointer, it also has the ability to be declared for incomplete types (I think). I.e.: struct Incomplete;
22
3281
by: lokman | last post by:
Hi, In the following code, can someone tell me the difference between *p++ and p++ ? I can see both achieve the same result. Thanks a lot !
8
2146
by: BigMan | last post by:
I wonder if the C++ standard says what should happen if I increment a pointer to a one-past-the-end value. I think it says that it is perfectly legal to increment a pointer to the last element element in an array - I get a pointer to one-past-the-end value. It is guaranteed that such a value exists for any array, so I always get a valid pointer to something (I do not care what it is). So, my question is what if I go further and increment...
6
1686
by: Bo Sun | last post by:
hi: please take a look at the following code: int p={111, 222, 333, 444}, *q = p, p1, p2, p3; p1 = *q++; p2 = *(q++); p3 = *(++q);
3
1900
by: Denis Palmeiro | last post by:
Hi, I've been reading K&R, and I'm a bit confused. In the book, they have the following function: void writelines(char *lineptr, int nlines ) { while( nlines-- > 0 ) printf("%s\n", *lineptr++); } Now when I initialize:
2
1446
by: a | last post by:
Hi, func(int* p){ p = p+1; } main(){ int* ptr; //assume ptr is pointing to a valid allocated array func(ptr); //Will ptr equal the original address value //or equal the original address + sizeof(int)??
33
5049
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
2
2277
by: braton | last post by:
Hello I'm wondering about the following thing: Let's define variable and set a pointer to points to it: int a = 0; int * p_a = &a; Now, according to C99, clause 6.5.6.7 I can treat pointer to "a"
3
7406
by: tfelb | last post by:
Hi group! I have here five different declarations but I have some problems to understand this concept. I know there are more examples if I would use parentheses but I think the following ones are common. What I've learned.. int myArray = { 0, 1, 2 }; int *ptr = myArray;
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.