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

Home Posts Topics Members FAQ

Reallocate an array (pointers)

Hi,
I have no idea why my reallocation function (myadd) is breaking up my
array. I'm trying to solve it and it took me few hours already. Maybe
you can look at my code and explain me what is wrong. (I still have
problems with pointers).
I left my code as it is.
Thank you!

#include "stdafx.h" /* contains stdio.h */
#include <stdlib.h>
#include <string.h>
void *alloca(int n);
char *readr(void);
void myadd(char **A);
void myfree(void *A);

/* Main program */
int main(void){
/* Dynamically allocated array of pointers :*/
char **names;
int i,n=2;

names=(char **)alloca(n);
for(i=0;i<n;i++){
names[i]=readr();
printf("txt: %s\n", names[i]);
}

myadd(names);
myfree(names);
return 0;
}
/* Functions */
void myfree(void *AA){ /* Free all allocated memory */
int i;
void **A=(void **)AA;
for(i=0; A[i]; i++) /* Finish freeing when A[i]=NULL */
free(A[i]);
free(AA);
}

void *alloca(int n){
void **A;
/* Avoid n==0:*/
if(n==0) n=1;
if((A=(void **)calloc(n+1, sizeof(*A)))==NULL) return NULL;
A[n]=NULL;
return A;
}

char *readr(void){
char *buf;
int i;
char c,BB[256]={'\0'};
fflush(stdin);
/* Read 255 characters max. Last char is '\0': */
for(i=0; ((c=getchar())!='\n')&&i<255; i++)
BB[i]=c;
fflush(stdin);
/* i+1: enable myfree() to free all memory: */
if((buf=(char *)calloc(i+1,sizeof(char)))==NULL){
printf("Oops, can't allocate mem.!\n");
}
else{
strcpy(buf,BB);
return buf;
}
}
/* Should reallocate "names" and add one more entry to it :*/
void myadd(char **A){
int n;
/* Count rows of array :*/
for(n=0;A[n];n++);
/* It is supposed to reallocate "names" but it seems to break it */
if((A=(char **)realloc(A, (n+2)*sizeof(*A)))==NULL)
printf("Oops, can't reallocate mem.!\n");
A[n]=readr();
A[n+1]=NULL;
}
May 28 '07 #1
11 4214
Piotrek wrote:
>
I have no idea why my reallocation function (myadd) is breaking up
my array. I'm trying to solve it and it took me few hours already.
Maybe you can look at my code and explain me what is wrong. (I
still have problems with pointers).
I left my code as it is.

#include "stdafx.h" /* contains stdio.h */
Then use stdio.h. You never listed stdafx.h.
#include <stdlib.h>
#include <string.h>

void *alloca(int n);
char *readr(void);
void myadd(char **A);
void myfree(void *A);

/* Main program */
int main(void){
/* Dynamically allocated array of pointers :*/
char **names;
int i,n=2;

names=(char **)alloca(n);
Alloca returns void*. You don't need (or want) the cast.
for(i=0;i<n;i++){
Try including a few spaces between items. "for (i = 0; i < n; i++)"
names[i]=readr();
printf("txt: %s\n", names[i]);
}

myadd(names);
myfree(names);
return 0;
}

/* Functions */
Better to precede main with these, thus dispensing with the
prototypes. This technique reduces the possibility of silly
errors.
void myfree(void *AA){ /* Free all allocated memory */
int i;
void **A=(void **)AA;
Once again, no cast. You are doing something funny (and
unnecessary) with these pointers.
for(i=0; A[i]; i++) /* Finish freeing when A[i]=NULL */
free(A[i]);
free(AA);
}

void *alloca(int n){
void **A;
/* Avoid n==0:*/
if(n==0) n=1;
if((A=(void **)calloc(n+1, sizeof(*A)))==NULL) return NULL;
No cast.
A[n]=NULL;
return A;
}
I got tired of reading by now.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 29 '07 #2
I compiled and runed ur code with dev-c++,and added a slice of my test
code,but I don't think ur array is broken up.
what did u mean?
int main(void){
/* Dynamically allocated array of pointers :*/
char **names;
int i,n=2;

names=(char **)alloca(n);
if(names == NULL)
{
printf("error,names is null!\n");
exit(1);
}
for(i=0;i<n;i++){
names[i]=readr();
printf("txt: %s\n", names[i]);
}

myadd(names);
/*add my test code*/
i = 0;
while(names[i]){
/*names[i]=readr();*/
printf("txt: %s\n", names[i++]);
}
myfree(names);
return 0;
}

"Piotrek" <no*****@noreply.com??????:f3**********@news.task. gda.pl...
Hi,
I have no idea why my reallocation function (myadd) is breaking up my
array. I'm trying to solve it and it took me few hours already. Maybe you
can look at my code and explain me what is wrong. (I still have problems
with pointers).
I left my code as it is.
Thank you!

#include "stdafx.h" /* contains stdio.h */
#include <stdlib.h>
#include <string.h>
void *alloca(int n);
char *readr(void);
void myadd(char **A);
void myfree(void *A);

/* Main program */
int main(void){
/* Dynamically allocated array of pointers :*/
char **names;
int i,n=2;

names=(char **)alloca(n);
for(i=0;i<n;i++){
names[i]=readr();
printf("txt: %s\n", names[i]);
}

myadd(names);
myfree(names);
return 0;
}
/* Functions */
void myfree(void *AA){ /* Free all allocated memory */
int i;
void **A=(void **)AA;
for(i=0; A[i]; i++) /* Finish freeing when A[i]=NULL */
free(A[i]);
free(AA);
}

void *alloca(int n){
void **A;
/* Avoid n==0:*/
if(n==0) n=1;
if((A=(void **)calloc(n+1, sizeof(*A)))==NULL) return NULL;
A[n]=NULL;
return A;
}

char *readr(void){
char *buf;
int i;
char c,BB[256]={'\0'};
fflush(stdin);
/* Read 255 characters max. Last char is '\0': */
for(i=0; ((c=getchar())!='\n')&&i<255; i++)
BB[i]=c;
fflush(stdin);
/* i+1: enable myfree() to free all memory: */
if((buf=(char *)calloc(i+1,sizeof(char)))==NULL){
printf("Oops, can't allocate mem.!\n");
}
else{
strcpy(buf,BB);
return buf;
}
}
/* Should reallocate "names" and add one more entry to it :*/
void myadd(char **A){
int n;
/* Count rows of array :*/
for(n=0;A[n];n++);
/* It is supposed to reallocate "names" but it seems to break it */
if((A=(char **)realloc(A, (n+2)*sizeof(*A)))==NULL)
printf("Oops, can't reallocate mem.!\n");
A[n]=readr();
A[n+1]=NULL;
}

May 29 '07 #3
On Mon, 28 May 2007 22:32:34 +0200, Piotrek <no*****@noreply.com>
wrote:
>Hi,
I have no idea why my reallocation function (myadd) is breaking up my
array. I'm trying to solve it and it took me few hours already. Maybe
you can look at my code and explain me what is wrong. (I still have
problems with pointers).
Your code has numerous errors and invokes undefined behavior.
>I left my code as it is.
Thank you!

#include "stdafx.h" /* contains stdio.h */
#include <stdlib.h>
#include <string.h>
void *alloca(int n);
char *readr(void);
void myadd(char **A);
void myfree(void *A);

/* Main program */
int main(void){
/* Dynamically allocated array of pointers :*/
char **names;
int i,n=2;

names=(char **)alloca(n);
You need to check that alloca succeeded.
for(i=0;i<n;i++){
names[i]=readr();
You need to check that readr succeeded.
printf("txt: %s\n", names[i]);
}

myadd(names);
myfree(names);
return 0;
}
/* Functions */
void myfree(void *AA){ /* Free all allocated memory */
int i;
void **A=(void **)AA;
for(i=0; A[i]; i++) /* Finish freeing when A[i]=NULL */
free(A[i]);
free(AA);
}

void *alloca(int n){
void **A;
/* Avoid n==0:*/
if(n==0) n=1;
if((A=(void **)calloc(n+1, sizeof(*A)))==NULL) return NULL;
This is a problem waiting to happen. You are allocating space for n+1
objects of type void*. In this instance, you will use the space to
store values of type char*. The standard guarantees that void* and
char* have the same size (and representation). However, if you ever
use this function to allocate space for different types of pointers,
you have no idea if you will allocate enough space. It is entirely
possible for sizeof (int*) to be greater than sizeof (void*).

Why are you using calloc? Do you think initializing a pointer to all
bits 0 has some benefit?
A[n]=NULL;
return A;
}

char *readr(void){
char *buf;
int i;
char c,BB[256]={'\0'};
fflush(stdin);
This invokes undefined behavior. fflush is defined only for output
streams.
>/* Read 255 characters max. Last char is '\0': */
for(i=0; ((c=getchar())!='\n')&&i<255; i++)
getchar returns an int. c is a char. It is possible for getchar to
return a value that will not fit in a char. If so, this would invoke
undesirable (either undefined or implementation defined) behavior.
BB[i]=c;
fflush(stdin);
/* i+1: enable myfree() to free all memory: */
if((buf=(char *)calloc(i+1,sizeof(char)))==NULL){
printf("Oops, can't allocate mem.!\n");
}
else{
strcpy(buf,BB);
return buf;
}
If calloc failed, you do not return anything. This will invoke
undefined behavior. Did you compiler not complain about reaching the
end of the function without returning a value?
>}
/* Should reallocate "names" and add one more entry to it :*/
void myadd(char **A){
int n;
This is an automatic variable that is not initialized. Therefore its
value is indeterminate.
>/* Count rows of array :*/
for(n=0;A[n];n++);
Any attempt to evaluate the indeterminate value in n invokes undefined
behavior.
>/* It is supposed to reallocate "names" but it seems to break it */
if((A=(char **)realloc(A, (n+2)*sizeof(*A)))==NULL)
n contains garbage. It could be negative. n+2 has no meaning.
printf("Oops, can't reallocate mem.!\n");
A[n]=readr();
When realloc fails, you print the error message but then procede to
use A as if it contained a valid pointer. This also invokes undefined
behavior.
A[n+1]=NULL;
Since C passes by value, when you return from this function, the value
of A is discarded. The value of names in the calling function is
unchanged. If the realloc succeeded and allocated a new block of
memory (instead or reusing the existing block), that value will no
longer point to the allocated area. In fact, that value becomes
indeterminate and any use of it in the calling function invokes
undefined behavior.
>}

Remove del for email
May 29 '07 #4
CBFalconer wrote:
>
Piotrek wrote:
/* Functions */

Better to precede main with these, thus dispensing with the
prototypes. This technique reduces the possibility of silly
errors.
Which silly errors are you refering to?

--
pete
May 29 '07 #5
Barry Schwarz wrote:
>
On Mon, 28 May 2007 22:32:34 +0200, Piotrek <no*****@noreply.com>
wrote:
/* Count rows of array :*/
for(n=0;A[n];n++);

Any attempt to evaluate the indeterminate value in n invokes undefined
behavior.
He assigned a value of zero to n, in the above shown code.
Since C passes by value, when you return from this function, the value
of A is discarded. The value of names in the calling function is
unchanged. If the realloc succeeded and allocated a new block of
memory (instead or reusing the existing block), that value will no
longer point to the allocated area. In fact, that value becomes
indeterminate and any use of it in the calling function invokes
undefined behavior.
That's the ticket!

void myadd(char ***A)
{
size_t n;
void *p;

for (n = 0; (*A)[n] != NULL; ++n) {
;
}
p = realloc(*A, (n + 2) * sizeof(**A));
if (p != NULL) {
*A = p;
(*A)[n + 1] = NULL;
(*A)[n] = readr();
} else {
puts("p == NULL");
exit(EXIT_FAILURE);
}
}

--
pete
May 29 '07 #6
Piotrek wrote:
>
Hi,
I have no idea why my reallocation function (myadd) is breaking up my
array. I'm trying to solve it and it took me few hours already. Maybe
you can look at my code and explain me what is wrong. (I still have
problems with pointers).
I left my code as it is.
Thank you!
I agree with everything that Barry Schwarz
said in his reply to your post,
except the part about indeterminate n;
I think he just didn't see the assignment to n.
/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STRINGS 2

char **alloca(size_t n);
char *readr(void);
void myadd(char ***A);
void myfree(char **A);

int main(void)
{
char **names;
size_t i, n = STRINGS;

names = alloca(n);
if (names == NULL) {
puts("names == NULL");
exit(EXIT_FAILURE);
}
for (i = 0; i != n; ++i) {
names[i] = readr();
printf("txt: %s\n", names[i]);
}
myadd(&names);
puts("\n");
for (i = 0; names[i] != NULL; ++i) {
printf("txt: %s\n", names[i]);
}
myfree(names);
puts("\nfreed");
return 0;
}

void myfree(char **A)
{
size_t i;

for (i = 0; A[i] != NULL; ++i) {
free(A[i]);
}
free(A);
}

char **alloca(size_t n)
{
char **A;

if ((A = malloc((n + 1) * sizeof *A)) != NULL) {
A[n] = NULL;
}
return A;
}

char *readr(void)
{
char *buf;
size_t i;
int c;
char BB[256] = {'\0'};

for (i = 0; ((c = getchar()) != '\n') && i != 255; ++i) {
if (c == EOF) {
puts("c == EOF");
exit(EXIT_FAILURE);
}
BB[i] = (char)c;
}
if ((buf = malloc(i + 1)) != NULL) {
strcpy(buf, BB);
} else {
puts("buf == NULL");
exit(EXIT_FAILURE);
}
return buf;
}

void myadd(char ***A)
{
size_t n;
void *p;

for (n = 0; (*A)[n] != NULL; ++n) {
;
}
p = realloc(*A, (n + 2) * sizeof(**A));
if (p != NULL) {
*A = p;
(*A)[n + 1] = NULL;
(*A)[n] = readr();
} else {
puts("p == NULL");
exit(EXIT_FAILURE);
}
}

/* END new.c */
--
pete
May 29 '07 #7
Piotrek wrote:
Hi,
I have no idea why my reallocation function (myadd) is breaking up my
array. I'm trying to solve it and it took me few hours already. Maybe
you can look at my code and explain me what is wrong. (I still have
problems with pointers).
I left my code as it is.
Thank you!

#include "stdafx.h" /* contains stdio.h */
#include <stdlib.h>
#include <string.h>
void *alloca(int n);
Just to add one minor point to the discussion. alloca() is a common
extension on many systems. It is also a built in function in gcc, so
unless you compile your program with -ansi or -std=c[89]9, your program
will fail in mysterious ways.

It's probably a good idea to rename alloca() to something else.

HTH
Bjørn

[snip]
--
Looking for an embeddable web server?
http://www.metasystems.no/products/h...der/index.html
May 29 '07 #8
On Tue, 29 May 2007 02:12:03 GMT, pete <pf*****@mindspring.comwrote:
>Barry Schwarz wrote:
>>
On Mon, 28 May 2007 22:32:34 +0200, Piotrek <no*****@noreply.com>
wrote:
>/* Count rows of array :*/
for(n=0;A[n];n++);

Any attempt to evaluate the indeterminate value in n invokes undefined
behavior.

He assigned a value of zero to n, in the above shown code.
Yes, I missed that. I guess horizontal white space is becoming more
than a luxury.
Remove del for email
May 29 '07 #9
eric liu wrote:
>
I compiled and runed ur code with dev-c++,and added a slice of my
test code,but I don't think ur array is broken up.
what did u mean?
ur is not a legal language on c.l.c, it is an ancient Abysinian
city. U hasn't posted here for some time. This is not a
chat-room.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 29 '07 #10
pete wrote:
CBFalconer wrote:
>Piotrek wrote:
>>/* Functions */

Better to precede main with these, thus dispensing with the
prototypes. This technique reduces the possibility of silly
errors.

Which silly errors are you refering to?
Anything caused by the prototype and declaration headers being
unequal. It also means you only have to look one way to find the
function declaration.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 29 '07 #11
On Tue, 29 May 2007 16:09:59 -0400, CBFalconer <cb********@yahoo.com>
wrote:
eric liu wrote:

I compiled and runed ur code with dev-c++,and added a slice of my
test code,but I don't think ur array is broken up.
what did u mean?

ur is not a legal language on c.l.c, it is an ancient Abysinian
city. U hasn't posted here for some time. This is not a
chat-room.
Ur was in Mesopotamia (now Iraq) not Abyssinia (now ~Ethiopia).
Otherwise concur.

Plus, runes are a source form. If you are going to rune a program,
which we don't recommend as it (a) isn't defined by the C standard and
(b) doesn't work well in Usenet postings, do it _before_ compiling.

:-!
- formerly david.thompson1 || achar(64) || worldnet.att.net
Jul 1 '07 #12

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

Similar topics

20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
19
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
2
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
18
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
4
by: erktek | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char* argv) { int i; int a; int *p = NULL;
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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: 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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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 ...

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.