473,883 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with memcpy and pointers/arrays confusion - again

Hi,

I'm relatively new with C-programming and even though I've read about
pointers and arrays many times, it's a topic that is a little confusing
to me - at least at this moment:

----
1) What's the difference between these 3 statements:

(i) memcpy(&b, &KoefD, n); // this works somewhere in my code

(ii) memcpy(&b[0], &KoefD[0], n); // but this doesn't

(iii) memcpy(b, KoefD, n); // what's the meaning of this then?
N.B: b is defined such that:

double *b = (double *) malloc(sizeof(d ouble) * (n+1));

and

KoefD is an argument to a sub-function that is called from within
main(). It's prototype (in main-function) is something like:

void sub_function(in t n, const double *KoefD);

I understand that this "&" gives the physical address of the pointer
itself... I would assume that was the same thing as &b[0] (the first
element) - but it doesn't seem like it is....
2) Why does this work:

free(indx); // free 1D array

But this doesn't?

free(b);

---------

indx is:
unsigned long *indx = (unsigned long *) malloc( (n+1)*sizeof(un signed
long) );

b is:
double *b = (double *) malloc(sizeof(d ouble) * (n+1));
I hope I don't have to cut down my program in small pieces to show that
these questions are actually some problems I struggle with now and it
might be I misunderstood something, but any comments would be greatly
appreciated...

If I have to, I'll cut my program down so you can just copy/paste, but
it would be much easier if I could figure out the error myself.
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Mar 9 '06
39 19673
Jordan Abel <ra*******@gmai l.com> writes:
#if __STDC_VERSION_ _ == 199901l
Why == instead of >=?
#define prntZ "z"
#elif SIZE_MAX <= UINT_MAX && sizeof(size_t) <= sizeof(int)
#define prntZ ""
#elif SIZE_MAX == ULONG_MAX && sizeof(size_t) == sizeof(long)
#define prntZ "l"
#else
#error size_t too big
#endif

--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Mar 20 '06 #31
On 2006-03-20, Ben Pfaff <bl*@cs.stanfor d.edu> wrote:
Jordan Abel <ra*******@gmai l.com> writes:
#if __STDC_VERSION_ _ == 199901l
Why == instead of >=?


typo.
#define prntZ "z"
#elif SIZE_MAX <= UINT_MAX && sizeof(size_t) <= sizeof(int)
#define prntZ ""
#elif SIZE_MAX == ULONG_MAX && sizeof(size_t) == sizeof(long)
unfortunately, it appears that sizeof doesn't work in a preprocessor
directive - the question is, can I trust the maxes?
#define prntZ "l"
#else
#error size_t too big
#endif

Mar 20 '06 #32
Jordan Abel <ra*******@gmai l.com> writes:
On 2006-03-20, Ben Pfaff <bl*@cs.stanfor d.edu> wrote:
Jordan Abel <ra*******@gmai l.com> writes:
#define prntZ "z"
#elif SIZE_MAX <= UINT_MAX && sizeof(size_t) <= sizeof(int)
#define prntZ ""
#elif SIZE_MAX == ULONG_MAX && sizeof(size_t) == sizeof(long)


unfortunately, it appears that sizeof doesn't work in a preprocessor
directive - the question is, can I trust the maxes?


You can't trust the ranges or the sizes, actually, by my reading.
`long' and `int' could have the same size, range, and
representation, and it would still yield undefined behavior to
print one of them with the other format specifier. This is
because the description of the fprintf function states explicitly
what type arguments must be; it doesn't make any allowance for
equivalent representations .

However, I'd guess that using ranges would work on "normal"
implementations .
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Mar 20 '06 #33
Dave Thompson <da************ *@worldnet.att. net> writes:
[...]
sizeof, and sizeof(typename ), doesn't standardly work in preprocessor
directives, although there are rumors some compilers support it.


The definitive word on this can be found in an article posted to
comp.std.c in 1998.

<http://groups.google.c om/group/comp.std.c/msg/4852afc61a060d8 9?hl=en&>

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 20 '06 #34
Keith Thompson opined:
Dave Thompson <da************ *@worldnet.att. net> writes:
[...]
sizeof, and sizeof(typename ), doesn't standardly work in
preprocessor directives, although there are rumors some compilers
support it.


The definitive word on this can be found in an article posted to
comp.std.c in 1998.

<http://groups.google.c om/group/comp.std.c/msg/4852afc61a060d8 9?hl=en&>

Ah, just the kind of quote to brighten a grey English morning!

Thanks.

--
BR, Vladimir

Predestination was doomed from the start.

Mar 20 '06 #35
Keith Thompson <ks***@mib.or g> wrote:
Martin Jørgensen <un*********@sp am.jay.net> writes:
[...]
I think I know why I did that cast. From
http://en.wikipedia.org/wiki/Malloc :

int *ptr = (int *)malloc(sizeof (int) * 10);

But that cast is wrong, you say? Then somebody should update what's on
wikipedia...


Looks like someone has:

#include <stdlib.h>

/* Allocate space for an array with 10 elements of type int */
/* C++ needs a cast, C strongly discourages it */
int *ptr = malloc(sizeof *ptr * 10);


Anyone taking bets on how long before it's changed back?

Richard
Mar 20 '06 #36
Richard Bos wrote:
Keith Thompson <ks***@mib.or g> wrote:
Martin Jørgensen <un*********@sp am.jay.net> writes:
[...]
I think I know why I did that cast. From
http://en.wikipedia.org/wiki/Malloc :

int *ptr = (int *)malloc(sizeof (int) * 10);

But that cast is wrong, you say? Then somebody should update what's on
wikipedia...

Looks like someone has:

#include <stdlib.h>

/* Allocate space for an array with 10 elements of type int */
/* C++ needs a cast, C strongly discourages it */
int *ptr = malloc(sizeof *ptr * 10);


Anyone taking bets on how long before it's changed back?


Well, when I looked yesterday the comment had been removed, but the code
was still the same. I changed the exit(1) to exit(EXIT_FAILU RE) for them.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Mar 20 '06 #37
On 2006-03-20, Ben Pfaff <bl*@cs.stanfor d.edu> wrote:
Jordan Abel <ra*******@gmai l.com> writes:
On 2006-03-20, Ben Pfaff <bl*@cs.stanfor d.edu> wrote:
Jordan Abel <ra*******@gmai l.com> writes:
#define prntZ "z"
#elif SIZE_MAX <= UINT_MAX && sizeof(size_t) <= sizeof(int)
#define prntZ ""
#elif SIZE_MAX == ULONG_MAX && sizeof(size_t) == sizeof(long)
unfortunately, it appears that sizeof doesn't work in a preprocessor
directive - the question is, can I trust the maxes?


You can't trust the ranges or the sizes, actually, by my reading.
`long' and `int' could have the same size, range, and
representation, and it would still yield undefined behavior to
print one of them with the other format specifier.


As a matter of practice, though, if they both have the same range, and
size_t has the same range as both, size_t is more likely to be unsigned
int than unsigned long. [and, of course, if size_t is NARROWER than int,
you can use int, because it will be promoted]
This is
because the description of the fprintf function states explicitly
what type arguments must be; it doesn't make any allowance for
equivalent representations .

However, I'd guess that using ranges would work on "normal"
implementations .

Mar 20 '06 #38
Jordan Abel <ra*******@gmai l.com> writes:
As a matter of practice, though, if they both have the same range, and
size_t has the same range as both, size_t is more likely to be unsigned
int than unsigned long. [...]


I've seen both `unsigned int' and `unsigned long' as size_t when
GCC is configured in different ways. I wouldn't want to depend
on either.
--
Just another C hacker.
Mar 20 '06 #39
Jordan Abel <ra*******@gmai l.com> writes:
As a matter of practice, though, if they both have the same range, and
size_t has the same range as both, size_t is more likely to be unsigned
int than unsigned long. [...]


I've seen both `unsigned int' and `unsigned long' as size_t when
GCC is configured in different ways on systems where those types
are identical, so I wouldn't want to assume that either one is
size_t.
--
Just another C hacker.
Mar 21 '06 #40

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

Similar topics

10
18725
by: spoc | last post by:
I have been using memcpy to copy one class to another of the same type. There are reasons why I had to do this bug am getting some odd crashes and maybe I'm doing something dodgy copying classes like this? The classes contain no dynamicaly allocated data, just standard types and arrays. Here is an example of what I'm doing, I know in isolation it may seem odd. I am presuming this will copy all data ok but not really sure of the workings and...
3
2367
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
5
3761
by: manya | last post by:
Ok, it's been a while since I've done the whole memcpy stuff with C++ and I'm having a hard time remembering everything. I hope, however, that you can help me with my problem. I memcpy a struct into a buffer to put it into a database (BerkeleyDB, to be specific) - what do I do to memcpy the thing back? Code Example: //struct I want to work with
28
2136
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
3
2483
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I am trying to calculate, with each sort, how many times during the sort the array elements are compared and swapped.
8
3695
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
24
3158
by: diego.arias.vel | last post by:
Hi all I have certain problem when I'm doing this: void copy(char *filename) { char *log_file; log_file=filename; strcat(log_file,"-log.txt");
2
4463
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
11
3071
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; }
0
11121
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10407
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
9564
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
7114
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
5793
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
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4606
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
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.