473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

DLL & pointers problem

Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer[i] = Buf[i]; // Error
}
delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...

The problem is, that presented code doesn't work correctly.
When I run my application I've got an exception like: 'Access violation at
address 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
Can you give me some hints? I have no idea what's wrong. If presented code
is located in one application (without dll) everything seems to be ok. How
can I resolve it?

Thank you
Pawel
Jul 22 '05 #1
8 1363

"Benek" <or*******@cbpmgn.barg.cy> wrote in message
news:ca**********@news.onet.pl...
Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer[i] = Buf[i]; // Error
}
delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...

The problem is, that presented code doesn't work correctly.
When I run my application I've got an exception like: 'Access violation at
address 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
Can you give me some hints? I have no idea what's wrong. If presented code
is located in one application (without dll) everything seems to be ok. How
can I resolve it?


Well I don't know. DLLs are not part of the C++ language, maybe you should
ask on a Windows programming group like
news:comp.os.ms-windows.programmer.win32. However one thing to try would be
to change this

void get_list(BYTE *&Buffer)

to this

void get_list(BYTE *Buffer)

There is no reason at all to use a reference in the code you quoted.

john
Jul 22 '05 #2

"Benek" <or*******@cbpmgn.barg.cy> wrote in message
news:ca**********@news.onet.pl...
Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer[i] = Buf[i]; // Error
}
delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...


I don't see any apparent problem in the code you have shown. DLLs etc are
offtopic here. But I do remember I had a similar problem some time back and it
had something to do with making it a multithreaded dll...just a wild guess
though. Right place to ask this is some MS newsgroup.

Best wishes,
Sharad
Jul 22 '05 #3
On Fri, 18 Jun 2004 16:26:34 +0200, "Benek" <or*******@cbpmgn.barg.cy>
wrote:
Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer[i] = Buf[i]; // Error
}
delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...

The problem is, that presented code doesn't work correctly.
When I run my application I've got an exception like: 'Access violation at
address 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
Can you give me some hints? I have no idea what's wrong. If presented code
is located in one application (without dll) everything seems to be ok. How
can I resolve it?

Thank you
Pawel


Technically, this has nothing to do with the C++ language itself, but
more with memory management / linker issues which are specific to the
MS-Windows platform. Therefore, it is off-topic in this newsgroup.

Usually this kind of problem arises when allocating memory in one
module (i.e., the DLL) and deleting it in another (your EXE).
Otherwise, there would be no need to declare the argument for
get_list() as reference to a pointer.

Are you sure this is the same code that is causing your problem??
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #4

"Bob Hairgrove" <wouldnt_you_like@to_know.com> schreef in bericht
news:t1********************************@4ax.com...
On Fri, 18 Jun 2004 16:26:34 +0200, "Benek" <or*******@cbpmgn.barg.cy>
wrote:
Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer[i] = Buf[i]; // Error
}
delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...

The problem is, that presented code doesn't work correctly.
When I run my application I've got an exception like: 'Access violation ataddress 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
Can you give me some hints? I have no idea what's wrong. If presented codeis located in one application (without dll) everything seems to be ok. Howcan I resolve it?

Thank you
Pawel


Technically, this has nothing to do with the C++ language itself, but
more with memory management / linker issues which are specific to the
MS-Windows platform. Therefore, it is off-topic in this newsgroup.

Usually this kind of problem arises when allocating memory in one
module (i.e., the DLL) and deleting it in another (your EXE).
Otherwise, there would be no need to declare the argument for
get_list() as reference to a pointer.

Are you sure this is the same code that is causing your problem??
--
Bob Hairgrove
No**********@Home.com

Of course this is off-topic but if you export a function with DLL shouldn't
the first line be a macro???
I'm not sure if this is only when you use MFC or always the case.
Jul 22 '05 #5
On Fri, 18 Jun 2004 19:27:25 +0200, "Kapt. Boogschutter"
<so*****@nobody.com> wrote:

"Bob Hairgrove" <wouldnt_you_like@to_know.com> schreef in bericht
news:t1********************************@4ax.com.. .
On Fri, 18 Jun 2004 16:26:34 +0200, "Benek" <or*******@cbpmgn.barg.cy>
wrote:
>Hello
>I writing dll and application in C++, and I have a following problem:
>
>In dll I have function:
>void get_list(BYTE *&Buffer)
>{
> BYTE* Buf = new BYTE[5];
> for(int i = 0; i < 5; ++i) {
> Buffer[i] = Buf[i]; // Error
> }
> delete[] Buf;
>}
>
>Application use this function (from dll) like in following example:
> ...
> List = new BYTE[5];
> get_list(List);
> delete[] List;
> ...
>
>The problem is, that presented code doesn't work correctly.
>When I run my application I've got an exception like: 'Access violationat >address 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
>Can you give me some hints? I have no idea what's wrong. If presentedcode >is located in one application (without dll) everything seems to be ok.How >can I resolve it?
>
>Thank you
>Pawel
>


Technically, this has nothing to do with the C++ language itself, but
more with memory management / linker issues which are specific to the
MS-Windows platform. Therefore, it is off-topic in this newsgroup.

Usually this kind of problem arises when allocating memory in one
module (i.e., the DLL) and deleting it in another (your EXE).
Otherwise, there would be no need to declare the argument for
get_list() as reference to a pointer.

Are you sure this is the same code that is causing your problem??
--
Bob Hairgrove
No**********@Home.com

Of course this is off-topic but if you export a function with DLL shouldn't
the first line be a macro???
I'm not sure if this is only when you use MFC or always the case.


Not necessarily. One usually uses the "extern" keyword (which is not a
macro), or 'extern "C"' if the function is to have C linkage. Perhaps
this is what you were thinking of?
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #6

"Bob Hairgrove" <wouldnt_you_like@to_know.com> schreef in bericht
news:i1********************************@4ax.com...
On Fri, 18 Jun 2004 19:27:25 +0200, "Kapt. Boogschutter"
<so*****@nobody.com> wrote:

"Bob Hairgrove" <wouldnt_you_like@to_know.com> schreef in bericht
news:t1********************************@4ax.com.. .
On Fri, 18 Jun 2004 16:26:34 +0200, "Benek" <or*******@cbpmgn.barg.cy>
wrote:

>Hello
>I writing dll and application in C++, and I have a following problem:
>
>In dll I have function:
>void get_list(BYTE *&Buffer)
>{
> BYTE* Buf = new BYTE[5];
> for(int i = 0; i < 5; ++i) {
> Buffer[i] = Buf[i]; // Error
> }
> delete[] Buf;
>}
>
>Application use this function (from dll) like in following example:
> ...
> List = new BYTE[5];
> get_list(List);
> delete[] List;
> ...
>
>The problem is, that presented code doesn't work correctly.
>When I run my application I've got an exception like: 'Access
violationat
>address 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
>Can you give me some hints? I have no idea what's wrong. If presented

code
>is located in one application (without dll) everything seems to be ok.

How
>can I resolve it?
>
>Thank you
>Pawel
>

Technically, this has nothing to do with the C++ language itself, but
more with memory management / linker issues which are specific to the
MS-Windows platform. Therefore, it is off-topic in this newsgroup.

Usually this kind of problem arises when allocating memory in one
module (i.e., the DLL) and deleting it in another (your EXE).
Otherwise, there would be no need to declare the argument for
get_list() as reference to a pointer.

Are you sure this is the same code that is causing your problem??
--
Bob Hairgrove
No**********@Home.com

Of course this is off-topic but if you export a function with DLL shouldn'tthe first line be a macro???
I'm not sure if this is only when you use MFC or always the case.


Not necessarily. One usually uses the "extern" keyword (which is not a
macro), or 'extern "C"' if the function is to have C linkage. Perhaps
this is what you were thinking of?
--
Bob Hairgrove
No**********@Home.com


That was not what I was thinking of,
I looked it up what I was looking for, its:
AFX_MANAGE_STATE(AfxGetStaticModuleState());
But it's only for MFC Extension DLLs .
Jul 22 '05 #7
"Kapt. Boogschutter" <so*****@nobody.com> wrote in message news:<ca**********@news3.tilbu1.nb.home.nl>...
"Bob Hairgrove" <wouldnt_you_like@to_know.com> schreef in bericht
news:t1********************************@4ax.com...
On Fri, 18 Jun 2004 16:26:34 +0200, "Benek" <or*******@cbpmgn.barg.cy>
wrote:
Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer[i] = Buf[i]; // Error
} delete[] Buf;}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...

The problem is, that presented code doesn't work correctly.
When I run my application I've got an exception like: 'Access violation ataddress 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
Can you give me some hints? I have no idea what's wrong. If presented codeis located in one application (without dll) everything seems to be ok. Howcan I resolve it?

Thank you
Pawel


Technically, this has nothing to do with the C++ language itself, but
more with memory management / linker issues which are specific to the
MS-Windows platform. Therefore, it is off-topic in this newsgroup.

Usually this kind of problem arises when allocating memory in one
module (i.e., the DLL) and deleting it in another (your EXE).
Otherwise, there would be no need to declare the argument for
get_list() as reference to a pointer.

Are you sure this is the same code that is causing your problem??
--
Bob Hairgrove
No**********@Home.com

Of course this is off-topic but if you export a function with DLL shouldn't
the first line be a macro???
I'm not sure if this is only when you use MFC or always the case.


Try to use the same CRT library (C Runtime Library)...
Your application and your DLL should use the Single-threaded DLL or
the Multi-threaded DLL CRT library, not the simple Single-threaded or
Multi-threaded.
Jul 22 '05 #8
ak
On Fri, 18 Jun 2004 16:53:58 +0200, Bob Hairgrove <wouldnt_you_like@to_know.com>
wrote:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer[i] = Buf[i]; // Error
}
delete[] Buf;
}


what is it you want to achieve with the function? Buf is not initialized
so assigning random values to Buffer is what you are doing above.

why pass BYTE *&Buffer? you should specify as a function arg how many
max bytes it should write - for safety reasons:

void get_list( BYTE *Buffer, int MaxBufferLen )
Jul 22 '05 #9

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

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
14
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over...
80
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: ...
72
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
28
by: Martin Jørgensen | last post by:
Hi, I have a "funny" question, which I think is pretty "healthy" to examine... This program is being investigated: - - - - - - - #include <iostream> using namespace std; #define DAYS 7
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
42
by: coder_lol | last post by:
Thanks everyone again for contributing to helping me clear C++ confusions. I did some serious reading on copy constructors and assignments and I think I've got a good handle on the memory stuff. ...
14
by: jalqadir | last post by:
The constructor in MyClass instantiates many objects pointers through 'new', I would like to implement a way to make sure that the object has been allocated in memory by catch(ing) the bad_alloc...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.