Author Topic: Aparent error in Code Blocks  (Read 4541 times)

Offline venom4u31

  • Multiple posting newcomer
  • *
  • Posts: 19
Aparent error in Code Blocks
« on: March 05, 2012, 07:52:57 pm »
I was writing a program in code blocks and I have stumbled upon this error... I ran the program execution just after the initialization of s1 and s2 and although s2 is called just like s1 in the watch window, it has a totally different value than it should have.
s1 and s2 are not global or declared in any other function of this project. In this example j=0, d=1;


Can you please tell me any possible reason for which s1 and v->v+j+d have different values but identical addresses? I can't think of any. There you have a descriptive image (I can assure you it's not edited):


Offline stahta01

  • Lives here!
  • ****
  • Posts: 6996
    • My Best Post
Re: Aparent error in Code Blocks
« Reply #1 on: March 05, 2012, 08:11:34 pm »
Use the command line debugger.
Verify the problem either exists or does not exist using the command line debugger.

POST Information regarding your setup.
CB Version
OS Name and version
Compiler version
Debugger version

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline venom4u31

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Aparent error in Code Blocks
« Reply #2 on: March 05, 2012, 08:25:21 pm »
The Code Blocks Version is 10.05
The OS is Windows 7 32 bits
The compiler is GNU GCC but I don't know how to find its version.
The debugger name and version is GNU gdb 6.8

I don't know how to verify if a problem exists using the command line debugger, I know about the existence of the problem when I am seeing that two pointers point to the same address and their value greatly differs.

Also, I noticed that there are two commas after *((int*)v->v+j+d) = 23,, and v->v+j+d = (void *) 0x22fea9,,. Could that mean anything?
« Last Edit: March 05, 2012, 08:27:21 pm by venom4u31 »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6996
    • My Best Post
Re: Aparent error in Code Blocks
« Reply #3 on: March 06, 2012, 12:07:34 am »
Can someone else help this poster; I really do not used debug enough to help them.

The thinks stated in debug threads are:

Turn on debug logging (I have no idea how to do.)
Use the debug nightly build. http://forums.codeblocks.org/index.php/board,20.0.html
Verify the problem exists on the command line as stated in past.
Upgrade/downgrade the gdb version (no idea which version work best.)

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5242
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Aparent error in Code Blocks
« Reply #4 on: March 06, 2012, 01:04:07 am »
@OP
Please post your code snippet so that I can test it.

In fact, I'm not fully understand what's OP's problem, what is the watch expression, and what's the exact value it should be? Please list them.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline venom4u31

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Aparent error in Code Blocks
« Reply #5 on: March 06, 2012, 08:45:35 am »
@OP
Please post your code snippet so that I can test it.

In fact, I'm not fully understand what's OP's problem, what is the watch expression, and what's the exact value it should be? Please list them.


To have the code working there are several files that need to be working. If you want I can paste the basic structure of v to make you understand what it is about.
Code: [Select]
//v is a TMult element that represents a structure that can hold any type of element


typedef int(*TFComp)(const void *, const void *);
typedef struct
{ size_t d;        /* element size */
  TFComp fid;      /* function that checks the equality of two elements */
  TFComp ord;      /* ordering function */
  void *v, *s, *t; /* value vector adresses: v for beginning, s for end of used memory and t for available memory */
} TMult;



Regarding values, I think it's pretty clear. The vector v->v is listed (it's void with int values in it), the initialization of s1 is clear: it's v->v+j (j=0 and d=1, j, d are int numbers). And s2=v->v+j+d which is supposed to be s1+1. Now, s1 is correctly v->v, as it says there, its value is 1. However s2, although it holds the exact same memory address as v->v+j+d, instead of having the value 23 in it, it has a huge number stored.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5242
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Aparent error in Code Blocks
« Reply #6 on: March 06, 2012, 08:54:06 am »
Ok, you can do:

1, download the debugger branch nightly build version
2, enable the full debug log output
3, past all the debugger's debug log text when you watch those variables.

Mean while, I will test your sample code too.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5242
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Aparent error in Code Blocks
« Reply #7 on: March 06, 2012, 09:03:02 am »
So, what's your problem? Here is my test code and see the screen shot below:

code:
Code: [Select]
#include <stdio.h>

//v is a TMult element that represents a structure that can hold any type of element

typedef int(*TFComp)(const void *, const void *);


typedef struct
{ size_t d;        /* element size */
  TFComp fid;      /* function that checks the equality of two elements */
  TFComp ord;      /* ordering function */
  void *v, *s, *t; /* value vector adresses: v for beginning, s for end of used memory and t for available memory */
} TMult;


TMult a;

int main()
{
    TMult *p = &a;
    p->v = (void*)0x1;
    p->s = (void*)0x2;
    return 0;
}


screen shot:



I can't see any thing wrong.

EDIT: would you mind to extend my example to show us the errors?
« Last Edit: March 06, 2012, 09:04:39 am by ollydbg »
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7253
    • Jens' unofficial debian-repository for the Code::Blocks - IDE
Re: Aparent error in Code Blocks
« Reply #8 on: March 06, 2012, 09:36:09 am »
Please create a simple working example, that shows the issue, not just a stub.

From what I see, the issue most likely occurs because of incorrect referencing/dereferncing of pointers, probably caused by a lack of basic programming knowledge.

Be aware, that this forum/website, does not teach programming and is not meant to find programming errors (if they are not inside C::B's own code).
Such questions violate our forum rules, and every user has accepted these rules with registering here.

So your topic might get locked !

Offline venom4u31

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Aparent error in Code Blocks
« Reply #9 on: March 06, 2012, 09:41:52 am »
Please create a simple working example, that shows the issue, not just a stub.

From what I see, the issue most likely occurs because of incorrect referencing/dereferncing of pointers, probably caused by a lack of basic programming knowledge.

Be aware, that this forum/website, does not teach programming and is not meant to find programming errors (if they are not inside C::B's own code).
Such questions violate our forum rules, and every user has accepted these rules with registering here.

So your topic might get locked !


I'm sorry, I didn't want or expect to have the code solved or a valid solution provided. I just wanted to know a possible reason for which such a thing might happen. Since I didn't find any logical reason for two pointers initialized the same way to have completely different values, I thought that might have happened because a code::blocks error.