Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
159 user(s) are online (134 user(s) are browsing Forums)

Members: 0
Guests: 159

more...

Support us!

Headlines

 
  Register To Post  

(1) 2 »
Resize an array in C
Quite a regular
Quite a regular


See User information
Hi All,

I've made a look over the net without really finding an useable method on how to resize an arry during program running.

Imagine I have an array defined with this :

char WindowTitle[] = "MyArray";

and I want during execution for example, to change it by a new string

char NewTitle[] = "TitleEnteredByUser";

and I want to put NewTitle[] -> WindowTitle[]

Does someone have an idea ?

Thank you
Regards,
AmiDARK.

All we have to decide is what to do with the time that is given to us.
Go to top
Re: Resize an array in C
Supreme Council
Supreme Council


See User information
@freddix

I would suggest you do it properly, and allocate and free memory for your string buffers.

If you really have to resize arrays, check the realloc() function.

Simon

Go to top
Re: Resize an array in C
Quite a regular
Quite a regular


See User information
@Rigo
Does strcpy do the correct job ?
if I want to put 2nd string inside 1st one ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: Resize an array in C
Home away from home
Home away from home


See User information
@freddix
What you are trying to do doesn't really make sense. You need to allocate new memory (for a new string), put whatever you want in the new string, and then put the string's pointer in place of the old string.

BTW, strcpy will NOT resize the target sting. If you try, it will write past the end of the string, and most likely corrupt something (crash if you are lucky, corrupt data if you are not).

If you were using E instead of C for coding, then I'd write some code as an example... (Personally I don't feel that C is a good language to learn this kind of stuff, due to the horrid way it handles pointers vs non-pointers.)

Author of the PortablE programming language.
Go to top
Re: Resize an array in C
Home away from home
Home away from home


See User information
@freddix

Like ChrisH said, you have to reallocate the memory yourself. In C++ there's a string class that does it all for you, but in C it's up to you. No, strcpy won't reallocate memory.

Here are a few examples:

// Static strings example (fixed strings)
const char winTitle1[] = "Title 1";
const char windowTitle = winTitle;

// ...

const char winTitle2[] = "Title 2";

// Changing window title to winTitle2
windowTitle = winTitle2;



// Dynamic strings example

const char winTitle1[] = "Title 1";

char *windowTitle = (char*)malloc(strlen(winTitle1) + 1); // +1 for the NULL terminator
strcpy(windowTitle, winTitle1);

// Now replace the string with a new one
const char winTitle2[] = "A new title";
free(windowTitle); // Deallocating the old string
windowTitle = (char*)malloc(strlen(winTitle2) + 1);
strcpy(windowTitle, winTitle2);


Which should you use? Well, if there are only a few things that the window title could be, the first example is appropriate. However, if you will be dynamically changing the window title (e.g., multiple strings are combined, or a filename is used, etc.), then dynamically allocating memory is the best option.

WARNING: The above example does no error checking. You should always check if a memory allocation fails.

Hans

Join Kea Campus' Amiga Corner and support Amiga content creation
https://keasigmadelta.com/ - see more of my work
Go to top
Re: Resize an array in C
Just popping in
Just popping in


See User information
@freddix

There's an article I read in an old issue of (the now defunct) Dr. Dobb's Journal that addresses a similar problem: run-time array allocatiob in C. It uses single/nested pointers in a way that creates 1/2D arrays which can be allocated on-the-fly, and can be used anywhere normal 1D and 2D arrays are used. The code doesn't include array extension, but they can be de-allocated and re-allocated during run-time, and the code could probably be modified to permit array extension.

It's too much code to in-line here; I'll send it if you are interested.

Regards,
JCC

Go to top
Re: Resize an array in C
Just popping in
Just popping in


See User information
s@freddix

I've had the same problem recently, trying to convert my palmos databook database to lipidclock reminder format. Luckily, I just do:

char *string;
if((string=malloc(strlen("string to enter"))!=0)
{
sprintf(string, "%s", "string to enter");
}
else
{
printf("Memory allocation error!\a\n");
}


To swap different string, I guess you need to use the (previously mentioned) realloc() command, eg..
char *string1 = "first string";
char *string2 = "second string";
char *changestring;

if((changestring=malloc(strlen(string1))!=0)
{
sprintf(changestring, "%s", string1);
};
if((changestring=realloc(strlen(string2))!=0)
{
sprintf(changestring, "%s", string2);
};

Also, I prefer to use sprintf() instead of strcpy, the additional formatting options are very useful. (personal preference though). Sprintf uses the same args as printf, the pointer before the format string is where you put the array name)

I've made extensive use of a couple of websites that have been quite life-savers for looking up stuff, too.
cplusplus.com, though you have to be careful with this one if you're using straight C, and not C++. Plus, the language can be kinda thick at times, though it has some good example code.

C Programming Reference is one I found the other day, but it looks like beginner-level example code. Also DOS/X86 based afaik.

C programming.com seems useful for beginner level stuff, too.

I've been getting used to the memory allocation probs myself, having trashed my system memory to the point of requiring a cold reset. (I'm using BlizKick to set up a custom 3.9+goodies kickstart, which isn't "locked", resulting in a restart and a lovely red screen when it gets trashed. Using RTG means I need to flip the VGA switch to even see that...)

EDIT: If you go to those links via Ibrowse, you may want to disable javascript to speed things up a bit. (I have them in the URL prefs w/ Javascript disabled)

Resized Image
Go to top
Re: Resize an array in C
Quite a regular
Quite a regular


See User information
@JCC:
Yes, i'd like to see it :)

@kvasir;
I'll check these articles.

Thank you to each of you.
in fact, I think I will use another approach.
I've made a command to Create/Delete string :
void CreateDeleteStringchar ZePointerint NewSize ){
  
// We'll free the word defined at * ZePointer
  
if ( NewSize == ){
    if ( 
ZePointer != ){
      
freeZePointer );
      
ZePointer 0;
     }
   }
  
// we allocate NewSize bytes to fit the need of the word to create
  
if ( NewSize != ){
    
ZePointer malloc( ( NewSize sizeofchar ) ) + );
   }
 }


Used with NewSize of 0 will delete the string from memory.
Used with a newsize, it will alloc memory for the string.
And then I'll do memory copy with 2 strings pointers when needed :p

What do you think about this idea ?

Regards,
AmiDARK


Edited by freddix on 2009/9/15 23:55:30
All we have to decide is what to do with the time that is given to us.
Go to top
Re: Resize an array in C
Just can't stay away
Just can't stay away


See User information
@kvasir

Your example code doesn't allocate space for the null-terminator character (malloc size should be strlen()+1) so it will always write one byte past the memory allocation.

Go to top
Re: Resize an array in C
Just popping in
Just popping in


See User information
@salass00

Ooops! Did I mention I've requently trashed my system memory?

Resized Image
Go to top
Re: Resize an array in C
Quite a regular
Quite a regular


See User information
@salass00:
You're right ;)
I've forgotten this "detail" ;)
*FIXED*

Previous post modified too :p
:p

All we have to decide is what to do with the time that is given to us.
Go to top
Re: Resize an array in C
Home away from home
Home away from home


See User information
@freddix

Quote:

freddix wrote:
@salass00:
You're right ;)
I've forgotten this "detail" ;)
*FIXED*

Previous post modified too :p
:p


And I even put a comment in my example explaining the importance of strlen() + 1.

Join Kea Campus' Amiga Corner and support Amiga content creation
https://keasigmadelta.com/ - see more of my work
Go to top
Re: Resize an array in C
Home away from home
Home away from home


See User information
@freddix

Quote:

freddix wrote:

Thank you to each of you.
in fact, I think I will use another approach.
I've made a command to Create/Delete string :
void CreateDeleteStringchar ZePointerint NewSize ){
  
// We'll free the word defined at * ZePointer
  
if ( NewSize == ){
    if ( 
ZePointer != ){
      
freeZePointer );
      
ZePointer 0;
     }
   }
  
// we allocate NewSize bytes to fit the need of the word to create
  
if ( NewSize != ){
    
ZePointer malloc( ( NewSize sizeofchar ) ) + );
   }
 }


Used with NewSize of 0 will delete the string from memory.
Used with a newsize, it will alloc memory for the string.
And then I'll do memory copy with 2 strings pointers when needed :p

What do you think about this idea ?

Regards,
AmiDARK


This code won't work because you're writing a new pointer to ZePointer, but that's a local pointer; the calling code's pointer will remain unchanged. Also, you could have a memory leak if NewSize != 0 and you reallocate ZePointer.

Try something like this:
void ReAllocStringchar **ZePointerint NewSize ){
  
// We'll free the word defined at * ZePointer
  
if (*ZePointer)
  {
      
free( *ZePointer );
      *
ZePointer 0;
  }
  
// we allocate NewSize bytes to fit the need of the word to create
  
if ( NewSize != ){
    *
ZePointer malloc( ( NewSize sizeofchar ) ) + );
   }
 }


The code above has a pointer to the original pointer to the string, so we update that pointer, rather than a local variable.

I recommend that you create functions that do a bit more, such as copyString(), concatenateString() and also have allocString()/freeString(). The best thing to do would be to create a string Abstract Data Type (ADT). Something like:
typedef struct String_s
{
    
char *string;
    
int allocLength;
String;

Stringstring_alloc(int length);
void string_free(String *string);
Stringstring_resize(String *stringint newSize);
Stringstring_copy(...);
Stringstring_cat(...);
charstring_getContents(...);
const 
charstring_getContentsConst(...);
...

By ... I mean that I'm too lazy to write everything out.

This way you can easily hide all of the nasty details of manipulating strings in one source file, and won't make mistakes such as trying to reallocate a string that wasn't allocated with malloc() in the first place.

Hans

EDIT: Just found a bad bug in the example that I gave


Edited by Hans on 2009/9/16 0:58:35
Join Kea Campus' Amiga Corner and support Amiga content creation
https://keasigmadelta.com/ - see more of my work
Go to top
Re: Resize an array in C
Quite a regular
Quite a regular


See User information
@Hans
Thank Hans for your last post.

I've modified my CreateDeleteString to fit me need with your advices.

You're right that some other functions to manipulate strings will be developed for copy/move/delete/resize strings.

thanks :)

All we have to decide is what to do with the time that is given to us.
Go to top
Re: Resize an array in C
Home away from home
Home away from home


See User information
@freddix

Oops, I forgot to dereference ZePointer in the if statement. I had:

if(ZePointer)

instead of:

if(*ZePointer)

I've fixed it above, but anyone who read it before then should take note.

Hans

Join Kea Campus' Amiga Corner and support Amiga content creation
https://keasigmadelta.com/ - see more of my work
Go to top
Re: Resize an array in C
Quite a regular
Quite a regular


See User information
@all

One could make a C++ class with proper resize functions and assertions for access beyond the allocated size.
And add to the mix proper equivalents for string functions .
And also make it a template class to unbind the array from specific data type.

I bet STL has something that does that.

Jack

Resized Image
"the expression, 'atonal music,' is most unfortunate--it is on a par with calling flying 'the art of not falling,' or swimming 'the art of not drowning.'. A. Schoenberg
Go to top
Re: Resize an array in C
Not too shy to talk
Not too shy to talk


See User information
@freddix

Quote:

freddix wrote:
Imagine I have an array defined with this :

char WindowTitle[] = "MyArray";

and I want during execution for example, to change it by a new string

char NewTitle[] = "TitleEnteredByUser";

and I want to put NewTitle[] -> WindowTitle[]



In order to keep it as simple as possible I would do something like this:



char WindowTitle[256] = "MyTitle";
.
.
.
strcpy (WindowTitle,"TitleEnteredByUser");



Just make sure that the user cannot enter more than 255 characters.

Bye,
Thomas

Go to top
Re: Resize an array in C
Just popping in
Just popping in


See User information
@thomas

That's why strlcpy() was invented. Using non-size-checking functions and just "assuming" that the allocated size is sufficient should be punished with something really ugly. They may trash memory and you don't even notice it immediately and hunting such bugs is a real PITA.

Go to top
Re: Resize an array in C
Home away from home
Home away from home


See User information
@tboeckel
Or in AmigaE (and PortablE) you use special "e-strings", which know their size, so it is impossible to trash memory when copying or appending to them...

Author of the PortablE programming language.
Go to top
Re: Resize an array in C
Home away from home
Home away from home


See User information
@Jack

Quote:

Jack wrote:
@all

One could make a C++ class with proper resize functions and assertions for access beyond the allocated size.
And add to the mix proper equivalents for string functions .
And also make it a template class to unbind the array from specific data type.

I bet STL has something that does that.

Jack


STL has a string class, so it's already done.

Hans

Join Kea Campus' Amiga Corner and support Amiga content creation
https://keasigmadelta.com/ - see more of my work
Go to top

  Register To Post
(1) 2 »

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project