473,572 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is possible to build some pointer array?

Hi, all:

I want to record some memory pointer returned from malloc, is possible
the code like below?

int memo_index[10];
int i,j;
char *tmp;

for (i=0;i<10;i++){
tmp = malloc(10);
memo_index[i] = tmp;
}

what I want to realize is "use the memo_index [10] to record each
usable memory pointer, then I just use
memo_index[i] to refer to each stuff installed in the allocated memory
/

Is it possible or some else way?

Thanks for any comments.

bin

Nov 15 '05 #1
48 2137
yezi wrote:
I want to record some memory pointer returned from malloc, is possible
the code like below?

int memo_index[10];
you want to record the address of a variable in memo_index so declare
it as a pointer.
int *memo_index[10].
int i,j;
char *tmp;

for (i=0;i<10;i++){
tmp = malloc(10);
memo_index[i] = tmp;
}


malloc will return a void pointer so type cast it and assign it to
memo_index.
for (i=0;i<10;i++)
memo_index[i]=(int *)malloc(10);
type casting depends on the way you want to use pointer.

Nov 15 '05 #2
ru********@redi ffmail.com writes:
[...]
malloc will return a void pointer so type cast it and assign it to
memo_index.
for (i=0;i<10;i++)
memo_index[i]=(int *)malloc(10);
type casting depends on the way you want to use pointer.


No, no, no.

malloc() returns a result of type void*, which will be implicitly
converted to any pointer-to-object type. Casting the result is
unnecessary, and can mask certain errors such as forgetting to
"#include <stdlib.h>" or using a C++ compiler.

--
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.
Nov 15 '05 #3

ru********@redi ffmail.com wrote:
yezi wrote:
I want to record some memory pointer returned from malloc, is possible
the code like below?

int memo_index[10];


you want to record the address of a variable in memo_index so declare
it as a pointer.
int *memo_index[10].
int i,j;
char *tmp;

for (i=0;i<10;i++){
tmp = malloc(10);
memo_index[i] = tmp;
}


malloc will return a void pointer so type cast it and assign it to
memo_index.
for (i=0;i<10;i++)
memo_index[i]=(int *)malloc(10);
type casting depends on the way you want to use pointer.


since tmp is a char, it should really be:

char *memo_index[10];

That is, an array of 10 char pointers. Although pointers are all the
same size, don't point to different typed data from different typed
pointers unless you really need to.

Nov 15 '05 #4
On 8 Nov 2005 21:11:41 -0800, "sl*******@yaho o.com"
<sl*******@gmai l.com> wrote in comp.lang.c:

ru********@redi ffmail.com wrote:
yezi wrote:
I want to record some memory pointer returned from malloc, is possible
the code like below?

int memo_index[10];


you want to record the address of a variable in memo_index so declare
it as a pointer.
int *memo_index[10].
int i,j;
char *tmp;

for (i=0;i<10;i++){
tmp = malloc(10);
memo_index[i] = tmp;
}


malloc will return a void pointer so type cast it and assign it to
memo_index.
for (i=0;i<10;i++)
memo_index[i]=(int *)malloc(10);
type casting depends on the way you want to use pointer.


since tmp is a char, it should really be:

char *memo_index[10];

That is, an array of 10 char pointers. Although pointers are all the
same size, don't point to different typed data from different typed
pointers unless you really need to.


There is no guarantee or requirement at all in C that all pointers are
the same size. The only guarantees are:

1. Pointer to void and pointer to any of the character types have the
same size and representation. These pointers can store the full
representation of a pointer to any other object type.

2. All pointers to structures and unions have the same size
representation.

3. All pointers to functions, regardless of the return type or
argument list, have the same size.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #5
"sl*******@yaho o.com" <sl*******@gmai l.com> writes:
ru********@redi ffmail.com wrote: [...]
malloc will return a void pointer so type cast it and assign it to
memo_index.
for (i=0;i<10;i++)
memo_index[i]=(int *)malloc(10);
type casting depends on the way you want to use pointer.


(I've already commented on this.)
since tmp is a char, it should really be:

char *memo_index[10];

That is, an array of 10 char pointers. Although pointers are all the
same size, don't point to different typed data from different typed
pointers unless you really need to.


You can't assume that all pointers are the same size; there are
systems on which they aren't.

If you want a generic pointer, use void*. (char* is guaranteed to
have the same representation and alignment requirements, but that's
for historical reasons.)

--
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.
Nov 15 '05 #6

Jack Klein wrote:
There is no guarantee or requirement at all in C that all pointers are
the same size. The only guarantees are:

1. Pointer to void and pointer to any of the character types have the
same size and representation. These pointers can store the full
representation of a pointer to any other object type.

2. All pointers to structures and unions have the same size
representation.

3. All pointers to functions, regardless of the return type or
argument list, have the same size.


Hmm, a lot of code I've seen make the assumption that 1==2. Especially
TCP/IP libraries in Linux, Windows, BSD etc. that casts a char* into
structs to extract data from a packet.

If 1!=2, then you're saying that a void pointer should not ever point
to a struct. But this is how polymorphism is usually emulated in C.
Why, even malloc( ) returns a void pointer which can be used to point
to an area of memory to store a struct.

Besides, aren't pointers just addresses into memory? Why would a CPU
differentiate between structured and unstructured bytes? I can
understand the difference of function pointers since it points to an
area of executing code, not data memory, and in some systems these are
different address spaces.

Nov 15 '05 #7
Keith Thompson wrote:
malloc will return a void pointer so type cast it and assign it to
memo_index.
for (i=0;i<10;i++)
memo_index[i]=(int *)malloc(10);
type casting depends on the way you want to use pointer.


No, no, no.

malloc() returns a result of type void*, which will be implicitly
converted to any pointer-to-object type. Casting the result is
unnecessary, and can mask certain errors such as forgetting to
"#include <stdlib.h>" or using a C++ compiler.

Then how we should assign the memo_index[i] .
Is it like
memo_index[i]=malloc(10);
then i am getting this warning
warning: assignment makes pointer from integer without a cast

Nov 15 '05 #8
ru********@redi ffmail.com writes:
Keith Thompson wrote:
> malloc will return a void pointer so type cast it and assign it to
> memo_index.
> for (i=0;i<10;i++)
> memo_index[i]=(int *)malloc(10);
> type casting depends on the way you want to use pointer.


No, no, no.

malloc() returns a result of type void*, which will be implicitly
converted to any pointer-to-object type. Casting the result is
unnecessary, and can mask certain errors such as forgetting to
"#include <stdlib.h>" or using a C++ compiler.

Then how we should assign the memo_index[i] .
Is it like
memo_index[i]=malloc(10);
then i am getting this warning
warning: assignment makes pointer from integer without a cast


See above. You need to add
#include <stdlib.h>
so the compiler can see the declaration of malloc(). Without that the
compiler assumes (incorrectly) that malloc() returns an int.

Using a cast tells the compiler "Trust me, I know what I'm doing". If
you don't actually know what you're doing, the compiler will get its
revenge.

--
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.
Nov 15 '05 #9
ru********@redi ffmail.com wrote:
Keith Thompson wrote:
malloc() returns a result of type void*, which will be implicitly
converted to any pointer-to-object type. Casting the result is
unnecessary, and can mask certain errors such as forgetting to
"#include <stdlib.h>" or using a C++ compiler.

Then how we should assign the memo_index[i] .
Is it like
memo_index[i]=malloc(10);
then i am getting this warning
warning: assignment makes pointer from integer without a cast


Then either you have not #included <stdlib.h>, or you are not
programming in C but in an inferior clone.

Richard
Nov 15 '05 #10

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

Similar topics

3
5838
by: Kaz Kylheku | last post by:
Given some class C with array T x, is it possible to get a pointer-to-data-member to one of the elements? &C::x gives us a pointer-to-member-array: T (C::*). But I just want to get a T C::* pointing to a selected array element, so that I can later use an instance c of that class to pick out that array element: c->*ptr. This syntax,...
7
6678
by: B. Wood | last post by:
I have a small program (see below) that demonstrates an error I am getting when I build the program on a linux platform. I first initialize an unsigned char array (lines 16). I then initialize my array of my typedeffed structure (dataStruct) (lines 18-24). I use my initialized unsigned character array to initialize the unsigned character...
1
1773
by: mrhicks | last post by:
Hello all, I need some advice/help on a particular problem I am having. I have a basic struct called "indv_rpt_rply" that holds information for a particular device in our system which I will call INDV. The struct looks like // Some info used for the struct typedef unsigned char uint8; /* 8 bits */
8
1460
by: Richard Edwards | last post by:
If I have the following declaration: static char *list = {0}; and I execute this code: list = malloc(10); am I asking for problems?
3
2403
by: John | last post by:
Hi, I need to build a BAUDOT lookup table for an encoder. I see to use a char as the index 'A' or 'B' and get back an array of booleans (five elements long). Baudot = {00011} Baudot = {11001} Can someone give me a pointer on how to build this as a static lookup
3
1768
by: Wild Wind | last post by:
Hello, I made a post relating to this issue a while back, but I haven't received any answer, so here I am again. I am writing a mixed C++ dll which uses the following declaration: typedef System::Byte ByteArray __gc;
19
1645
by: Clint Olsen | last post by:
I was just thinking about the virtues of C vs. C++ wrt. ADT/generic programming. The biggest complaint about writing container libraries for ADTs is that void * offers no type safety. Does it really have to be this way? Couldn't you for instance track an object's accesses with void pointers and ensure they are used consistently across...
0
7733
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7640
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8000
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...
0
6337
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...
1
5524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3685
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...
0
3673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2138
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
0
975
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...

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.