Solved

What's the difference between these two ??

Posted on 2004-10-05
7
245 Views
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);
      dst++;
      aa++;
      if (val == 0)
            break;
      }
**** END ****

**** THIS DOESN'T WORKS ****

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

0
Comment
Question by:pimpernel
7 Comments
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

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.


Kent
0
 

Author Comment

by:pimpernel
Comment Utility
Thanks. But how should I increment it then correct (like in the first example...) ??

0
 
LVL 45

Accepted Solution

by:
Kdo earned 100 total points
Comment Utility

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,
Kent
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 2

Assisted Solution

by:guntherothk
guntherothk earned 25 total points
Comment Utility
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++) ...
0
 

Author Comment

by:pimpernel
Comment Utility
OK, Kdo, I'll keep the first sollution...
Thanks guntherothk for the tip. I'll put the strlen out of the "for".

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
Yesterday i answered to that question

     http:Q_21154939.html

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








0
 

Author Comment

by:pimpernel
Comment Utility
Hello,
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
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now