What's the difference between these two ??

Posted on 2004-10-05
Medium Priority
Last Modified: 2010-04-01
Hi, what is the difference between these two codefragments ??
I don't see any difference...
**** THIS WORKS ****
int16* dst=p1->buffer;  //buffer consists allso out of int16's
for (int i = 0; i < strlen(aa); i++)
      val = *aa;
      SSVAL(dst, 0, val);
      if (val == 0)
**** END ****


for (int i = 0; i < strlen(aa); i++)
      val = *aa;
      SSVAL(p1->buffer, 0, val);
      if (val == 0)

Question by:pimpernel
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 46

Expert Comment

by:Kent Olsen
ID: 12226086

From your comment and the code fragments, it appears that p1 is defined as:

typedef struct
  int16 buffer[SomeLength];
} p1_type;

p1_type *p1;

In your first example you assign the address of p1->buffer to dst and then increment dst each time through your loop.

In your second example you increment p1->buffer[0] each time through the loop.


Author Comment

ID: 12226155
Thanks. But how should I increment it then correct (like in the first example...) ??

LVL 46

Accepted Solution

Kent Olsen earned 400 total points
ID: 12226262

You need a temporary variable that you can increment, like in your first example.

The second fragment is actually an error because you're incrementing your data instead of a pointer.  Even if p1->buffer is defined as an 'int16 *' you don't want to do what the second fragment does because you're now incrementing the pointer to your buffer.  In all likelihood this is the base pointer so this coding style causes you to lose the base pointer to your buffer.

Stick with something close to example 1.  It works because it's correct.

Good Luck,
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Assisted Solution

guntherothk earned 100 total points
ID: 12227342
I'm not sure this loop does what you think it does. As you increment i in the loop, you are also incrementing aa, so strlen(aa) is decreasing each time through the loop. C++ evaluates the expression fully each time through. I'm betting you only read half the things you want to read. Unless this is the behavior you want, you need to assign strlen(aa) to a variable outside the loop

    int slen = strlen(aa);
    for (int i = 0; i < slen; i++) ...

or you can be fancy and put both into the for loop initializer

    for (int slen = strlen(aa), i = 0; i < slen; i++) ...

Author Comment

ID: 12228106
OK, Kdo, I'll keep the first sollution...
Thanks guntherothk for the tip. I'll put the strlen out of the "for".

LVL 39

Expert Comment

ID: 12229591
Yesterday i answered to that question


It was using a ToUnic function that had identical code fragments than your code, but hadn't the bugs kdo and gunther already pointed out. Obviously you are trying to change working code, what isn't recommended even if you would exactly know what you are doing.

As you made no response to the comments of kdo and gunther, i'll explain again the errors you made:

With p1->(buffer++) you are changing the pointer value of the data member 'buffer' in the struct _UNICODE_STRING. The instance 'p1' of  that struct has an invalid buffer member then (it points to the terminating zero and *not* to the begin of the previously allocated storage).

The main difference to the old working code is, that it uses a local pointer variable, thus leaving the value of p1->buffer intact.

The second error was similar: Instead of using a local variable 'len' as upper bound of the for loop, you are using strlen function. However, the argument of strlen is the char pointer 'aa' that get changed in the loop when that statement executes: aa++; . So, strlen(aa) decrements and i++ increments, e. g. with a string length of 10 only 5 bytes were copied to the UNICODE string.

Regards, Alex


Author Comment

ID: 12331500
sorry for my very late response, but I don't check my mail very often...
Thanks for your extra explanation.
I allreasy understood the remark of Kdo, but indeed didn't entirely understood guntherothk's remark (since the first code with the strlen also worked).

Now it's entirely clear (I think), and since my strings allways have a fixed length I just replaced the strlen part with th length.

Greetz, pimpernel

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question