473,473 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Learned something new today - thoughts?

Hi,

char **ipaddress;

ipaddress = malloc(N * sizeof(char*));

for (i=0; ;i++) {
ipaddress[i] = fxn_that_returns_pointer_to_char;
}

Here, I learned that I don't have to specify N. I can simply do:

ipaddress = malloc(sizeof(char*));

And everything works fine. I guess you just give it the first address
of pointer-to-char and that's all you need to do? I've always thought
that you have to specify how many pointer-to-chars as a lot of
examples do. Any drawback to not specifying N? In my case, I don't
know what N is. FYI, fxn_that_returns_pointer_to_char takes care of
allocating memory for chars.

Thanks!

-j.
Nov 13 '05 #1
9 1510
James Lee wrote:
char **ipaddress;

ipaddress = malloc(N * sizeof(char*));

for (i=0; ;i++) {
ipaddress[i] = fxn_that_returns_pointer_to_char;
}

Here, I learned that I don't have to specify N. I can simply do:
That is an infinate loop (so no matter what N is, you will most likely get a
SEGFAULT at some point)...
ipaddress = malloc(sizeof(char*));

And everything works fine. I guess you just give it the first address
of pointer-to-char and that's all you need to do? I've always thought
that you have to specify how many pointer-to-chars as a lot of
examples do. Any drawback to not specifying N? In my case, I don't
know what N is. FYI, fxn_that_returns_pointer_to_char takes care of
allocating memory for chars.


But in the case of omitting N nothing takes care of allocating memory for
the char* (at least for more than one). So although it might work in some
cases, it is likely not to work in others, because you will be writing to
memory that's "not yours". Possibly, if you did another malloc, writing to
ipaddress[i], where i is greater than 0 could, will overwrite data in the
newly allocated block.

--
Martijn
http://www.sereneconcepts.nl
Nov 13 '05 #2
In article <14**************************@posting.google.com >, James Lee wrote:
Hi,

char **ipaddress;

ipaddress = malloc(N * sizeof(char*));

for (i=0; ;i++) {
I suppose you left out the loop condition by mistake. It ought
to say "i < N".

ipaddress[i] = fxn_that_returns_pointer_to_char;
}

Here, I learned that I don't have to specify N. I can simply do:

ipaddress = malloc(sizeof(char*));


That gives you enough space to store one pointer to char in
ipaddress[0], but you're not allowed to use ipaddress[n], for n
not equal to 0, in any way whatsoever.

It might work on your machine right now, and tomorrow, but it's
sure not a good thing to go outside the allocated space. Bad
indeed.
--
Andreas Kähäri
Nov 13 '05 #3
"James Lee" <cs***@yahoo.com> wrote in message
news:14**************************@posting.google.c om...
Hi,

char **ipaddress;

ipaddress = malloc(N * sizeof(char*));
You should check for success/failure here.

for (i=0; ;i++) {
ipaddress[i] = fxn_that_returns_pointer_to_char;
}
You have an infinite loop (no termination condition
specified). This will eventually blow up. You have no way to
stop the loop, so it will eventually write outside
your array (when 'i' becomes equal to 'N'). Undefined behavior.


Here, I learned that I don't have to specify N.
No, you did not learn that. How you came to that
conclusion is beyond me.
I can simply do:

ipaddress = malloc(sizeof(char*));
That allocates storage for a single pointer.
ipaddress[0] is the only element in your array.

And everything
seems like it
works fine. I guess you just give it the first address
of pointer-to-char and that's all you need to do?
No. If you want to store an object, memory must
be provided. malloc(sizeof(char*)) only gives
enough memory for a *single* pointer.
I've always thought
that you have to specify how many pointer-to-chars as a lot of
examples do.
Yes, if you want to allocate memory for 'N' objects,
you must multiply N by the object's type's size for
the argument to 'malloc()' (except in case of allocating
type 'char' objects, because sizeof(char) is one by
definition.)

char *p = malloc(1000); /* room for 1000 characters */
int *pi = malloc(1000 * sizeof *pi); /* room for 1000 integers */
Any drawback to not specifying N?
Yes, undefined behavior.
In my case, I don't
know what N is.
Then who does? You can supply any nonnegative
integral value. This might be from within your
code, or perhaps from the user.
FYI, fxn_that_returns_pointer_to_char takes care of
allocating memory for chars.


But you've only allocated enough memory for a single
pointer to point to your multiple pointers to chars.

BTW if 'fxn...' allocates the storage, who deallocates it?
This is important.

A pointer is an object, just like any other type,
and must have storage allotted for it.

Do not use a compiler's behavior to determine language
correctness.

What C text(s) are you reading?

-Mike


Nov 13 '05 #4
On 25 Sep 2003 14:59:09 -0700
cs***@yahoo.com (James Lee) wrote:
Hi,

char **ipaddress;

ipaddress = malloc(N * sizeof(char*));
ipaddress = malloc(N * sizeof *ipaddress);
would be better as it means you don't have to change it if the type of
ipaddress is changed.
for (i=0; ;i++) {
ipaddress[i] = fxn_that_returns_pointer_to_char;
}

Here, I learned that I don't have to specify N. I can simply do:

ipaddress = malloc(sizeof(char*));

And everything works fine. I guess you just give it the first address
of pointer-to-char and that's all you need to do? I've always thought
that you have to specify how many pointer-to-chars as a lot of
examples do. Any drawback to not specifying N? In my case, I don't
know what N is. FYI, fxn_that_returns_pointer_to_char takes care of
allocating memory for chars.


The downside of specifying less memory than you require is that you
might be trampling over anything such as data used in maintaining the
heap or other data you own. It could also cause you computer to burst in
to explode with enough force to destroy all buildings withing 30000km.

If your program does not know who much memory will be required when it
reaches the malloc then you can use realloc later to increase the size
(remembering it might move) later in the program.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #5
cs***@yahoo.com (James Lee) writes:
Hi,

char **ipaddress;

ipaddress = malloc(N * sizeof(char*));

for (i=0; ;i++) {
ipaddress[i] = fxn_that_returns_pointer_to_char;
}

Here, I learned that I don't have to specify N. I can simply do:

ipaddress = malloc(sizeof(char*));

And everything works fine.


Huh. Only if N is one. Of course, you're actually initializing
infinite char*s, so N is infinity. You're screwed.

-Micah
Nov 13 '05 #6
cs***@yahoo.com (James Lee) wrote in message news:<14**************************@posting.google. com>...
Hi,

char **ipaddress;

ipaddress = malloc(N * sizeof(char*));

for (i=0; ;i++) {
ipaddress[i] = fxn_that_returns_pointer_to_char;
}

Here, I learned that I don't have to specify N. I can simply do:

ipaddress = malloc(sizeof(char*));

And everything works fine. I guess you just give it the first address
of pointer-to-char and that's all you need to do? I've always thought
that you have to specify how many pointer-to-chars as a lot of
examples do. Any drawback to not specifying N? In my case, I don't
know what N is. FYI, fxn_that_returns_pointer_to_char takes care of
allocating memory for chars.

Thanks!

-j.


Thanks everyone for your response. Man, I didn't think you'll be so
critical of the snippet I posted. :-) Obviously, I was only trying to
get the point across. I do not have infinite loop anywhere and yes I
free everything I allocate (or I try to at least).

I do not know how many sizeof(char*)'s to allocate b/c I never know
how many machines are going to respond to UDP broadcast message that I
am sending out. I'd like to keep a list of ip addresses (non-sorted).
I guess I can keep a linked-list for this but if I were to do it this
way, would realloc be the solution to my problem?

I really appreciate all your opinions/feedback and help. Thanks.

-j.
Nov 13 '05 #7

"James Lee" <cs***@yahoo.com> wrote in message
news:14**************************@posting.google.c om...
cs***@yahoo.com (James Lee) wrote in message news:<14**************************@posting.google. com>...
Hi,

char **ipaddress;

ipaddress = malloc(N * sizeof(char*));

for (i=0; ;i++) {
ipaddress[i] = fxn_that_returns_pointer_to_char;
}

Here, I learned that I don't have to specify N. I can simply do:

ipaddress = malloc(sizeof(char*));

And everything works fine. I guess you just give it the first address
of pointer-to-char and that's all you need to do? I've always thought
that you have to specify how many pointer-to-chars as a lot of
examples do. Any drawback to not specifying N? In my case, I don't
know what N is. FYI, fxn_that_returns_pointer_to_char takes care of
allocating memory for chars.

Thanks!

-j.


Thanks everyone for your response. Man, I didn't think you'll be so
critical of the snippet I posted. :-)


That's what we do here. :-)
Seriously, though, criticism is a good thing.
Obviously, I was only trying to
get the point across.
Not very well, imo. :-)
I do not have infinite loop anywhere
You do in the code you posted. Better to post
'real' code than try to 'describe' with
incomplete fragments. Compilable code is
best, then we can test it.
and yes I
free everything I allocate (or I try to at least).
No need to only 'try'. Just Do It.

I do not know how many sizeof(char*)'s to allocate b/c I never know
how many machines are going to respond to UDP broadcast message that I
am sending out.
You can use 'realloc()' to increase the size of already
allocated storage.
I'd like to keep a list of ip addresses (non-sorted).
I guess I can keep a linked-list for this
Yes, you could.
but if I were to do it this
way, would realloc be the solution to my problem?


It would be one solution, yes.

-Mike
Nov 13 '05 #8
James Lee wrote:
[snip]
Thanks everyone for your response. Man, I didn't think you'll be so
critical of the snippet I posted. :-) Obviously, I was only trying to
get the point across. I do not have infinite loop anywhere and yes I
free everything I allocate (or I try to at least).

I do not know how many sizeof(char*)'s to allocate b/c I never know
how many machines are going to respond to UDP broadcast message that I
am sending out.
Unfortunately, you're either going to /have/ to know, or you are going to have
to reallocate your allocated memory with each overflow.

One suggestion: since you're sending a UDP broadcast message and gathering
responses, you do have an upper limit to the number of responses. Assuming that
you are'nt using a multicast group, then you are limited to the number of
interfaces on your subnet minus two. You could just allocate /this/ many
entries, and fill the ones you receive.

The other way would be to allocate a block, and when you get to an overflow
condition, realloc() the block larger.
I'd like to keep a list of ip addresses (non-sorted).
I guess I can keep a linked-list for this but if I were to do it this
way, would realloc be the solution to my problem?

I really appreciate all your opinions/feedback and help. Thanks.

-j.

--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.

Nov 13 '05 #9
Lew Pitcher wrote:
Unfortunately, you're either going to /have/ to know, or you are
going to have to reallocate your allocated memory with each overflow.
[snipped]
The other way would be to allocate a block, and when you get to an
overflow condition, realloc() the block larger.


This is what I would do - I've always learned to optimize for the most
common results. I think it is save to assume that class C or smaller
networks are most common and thus to be expected - so start with a buffer
size 256 (which is actually two too many) and double this buffer each time
it is overflown, eventually covering B and A networks as well.

--
Martijn
http://www.sereneconcepts.nl
Nov 13 '05 #10

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

Similar topics

39
by: Marco Aschwanden | last post by:
Hi I don't have to talk about the beauty of Python and its clear and readable syntax... but there are a few things that striked me while learning Python. I have collected those thoughts. I am...
44
by: seberino | last post by:
Tuples are defined with regards to parentheses ()'s as everyone knows. This causes confusion for 1 item tuples since (5) can be interpreted as a tuple OR as the number 5 in a mathematical...
0
by: Marco Snoek | last post by:
Hi, I just can't get it... I've made an agenda/scheduler.. Now, to discover if a recurring event has moved (so it should be displayed at the current date) i must (?) use an or function. 1)...
2
by: tshad | last post by:
I am trying to check the date of my field to check if it is greater that today, but I am getting an error. I am using : <asp:CompareValidator runat="server"...
4
by: Chris | last post by:
Hello, I am attempting to build a MS SQL query that will return data from "today"; today being current day 8:00AM-10:00PM today. My goal is to return the data from a table that is written to...
2
by: peter | last post by:
I've been trying to teach myself Tkinter programming over the last few months (in a strictly amateur way), and have made a number of requests for help in this newsgroup and elsewhere. I've now...
3
by: veg_all | last post by:
How do you replace an occurence until a certain point: You can see my example code below and the output: $string1 = 'Hello world today'; $string2 = 'Hello blahblah'; $string1 = preg_replace...
6
by: Scott Abel | last post by:
Decibel, the underground hard rock and extreme heavy metal magazine, had the same problems as many other content-heavy organizations: inefficient and disconnected processes, outdated tools, and the...
2
by: Just_a_fan | last post by:
As they say in South Park... "Well, I learned something today." This is valid in both VB6 and 9 versions: Select Case TrySelect Case 1, 3 Debug.Print "1" Case 2 Debug.Print "2" Case 3
0
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...
0
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,...
0
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...
0
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,...
1
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...
0
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...
0
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...
0
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 ...
0
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...

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.