About goto

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

44 Replies - 987 Views - Last Post: 26 November 2018 - 11:20 AM Rate Topic: -----

#1 Nwb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 11-November 18

About goto

Posted 24 November 2018 - 06:11 AM

[TL;DR START]
Okay it's about goto. How exactly does it work! (don't post yet.. read on for the actual question ;p)

Now the question:
You know how new and delete, two statements used for dynamic allocation of memory, have made programming so much easier?

I was thinking, if we could emulate something similar to this for allocating arrays, dynamically, without using new and delete. I know I know it doesn't make sense to not use new but it's just a thought.

What do I mean by allocating dynamic arrays? (we're talking about STATIC arrays)
1) Making an array whose size is determined at compile time (wait don't say it's impossible yet..)
2) Destroying that array.
3) Also, replacing a given array with another array with the same name with +1 size and deleting the old array.
(Basically incrementing the size of a static array to make room for new elements)


So I was thinking if we can use goto (which every programmer despises apparently) to do something similar to that!

Wow Nwb, are you up late again? No. Well, actually, maybe? But, no, Wait. Let me explain with logic.

[TL;DR END]


This is the frame of the logic:


-> Takes input from user of size of arary.
-> Uses a tree and conditionals to declare array of that size.

Example (suppose we support 4 indexes, of course we will have a larger tree than this):
[b]label_for_creating_array:[/b]

If (input_size > 2)
  if (input_size == 3)
    -> DECLARE ARRAY[3] 
   else
     -> DECLARE ARRAY[4]
else
  if (input size == 1)
  -> DECLARE ARRAY[1]
 else
  -> DECLARE ARRAY[2]

[b]goto main_function;[/b]



This wouldn't work if the goto statement local'd the array definition to the scope.


To destroy an array I was thinking about making use of block.

Here's the structure again:

Main program() {


{
create_array; // go to create array label
(come here after creating array) // after create_array, control comes here
executable statements;
}
// array destroyed here
create_array; // go to create array label
}




Would that be a good way to dynamically allocate assuming we cannot use new and delete? Is there a better approach? Have I missed something?

This post has been edited by Nwb: 24 November 2018 - 06:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: About goto

#2 Nwb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 11-November 18

Re: About goto

Posted 24 November 2018 - 06:20 AM

This is not all stupid. 15 & 16 y.os (students..) in India (I can't speak for all of India it's only my school syllabus) use Turbo C (because our textbooks have clrscr(); and getch();) for executing in our school computers.

So if I could use something like this to dynamically generate something depending on what the user inputs (impossible to do with just static arrays, like for example a maze where using a 2D array makes it easy to keep track of the maze), this would be a GREAT way to do it. Basically if you had to participate in inter-school competitions you could and would blow the minds of the judges.

So if you're like me then, you've got yourself a good way to blow the minds of your host.. *starts evil laughter*..
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14581
  • View blog
  • Posts: 58,445
  • Joined: 12-June 08

Re: About goto

Posted 24 November 2018 - 06:36 AM

I am not certain where the mind blowing occurs or why goto would be needed. Malloc exists as an alternative to new and then there are vector containers as a whole better option.
Was This Post Helpful? 2
  • +
  • -

#4 Nwb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 11-November 18

Re: About goto

Posted 24 November 2018 - 06:42 AM

View Postmodi123_1, on 24 November 2018 - 06:36 AM, said:

I am not certain where the mind blowing occurs or why goto would be needed. Malloc exists as an alternative to new and then there are vector containers as a whole better option.


That's why I said "assume that you cannot use new". Turbo C doesn't support Malloc or new, that's the whole point of trying to do this ;)/>

Mostly I wanted to know if this is a VALID approach, think of it like an exercise to try and think of different ways a concept could potentially be used.

edit: Actually I'm not sure of Turbo C supports malloc, so thanks.

This post has been edited by Nwb: 24 November 2018 - 06:48 AM

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14581
  • View blog
  • Posts: 58,445
  • Joined: 12-June 08

Re: About goto

Posted 24 November 2018 - 06:48 AM

Well.. no. It is not terribly clear, but the running understanding would be to have a function that would create a new array of some given size, copy the contents, and return that back to the calling point. No need for goto.
Was This Post Helpful? 1
  • +
  • -

#6 Nwb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 11-November 18

Re: About goto

Posted 24 November 2018 - 06:49 AM

View Postmodi123_1, on 24 November 2018 - 06:48 AM, said:

Well.. no. It is not terribly clear, but the running understanding would be to have a function that would create a new array of some given size, copy the contents, and return that back to the calling point. No need for goto.


How do you get rid of the previous array though? *scratches head*

You have to go out of the local block in order for anything to get destroyed by the compiler right? But then I want to continue executing the program in the main function.. so goto is the only thing I thought was possible?

How do you allocate memory to the pointer? Or how do you add an index to the pointer without being able to allocate memory?

I think what you were trying to say is use a 2D array, pass through function, and then assign something to one of its index. But how is this possible, if unless you had to declare the array with 1000 indexes, which should be overkill? What if the array is larger than 1000 elements or just contains 2 elements throughout the program's execution?


By the way for assigning the new values to the newly incremented array, I was thinking about using a global array. Is there a better way?
^^ Because the old array gets deleted before the new array can be called..

This post has been edited by Nwb: 24 November 2018 - 07:02 AM

Was This Post Helpful? 0
  • +
  • -

#7 Nwb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 11-November 18

Re: About goto

Posted 24 November 2018 - 07:05 AM

I can't edit my previous post, but, I don't know why I said 2D array.. I meant 1D array itself.. so sorry if that confuses.
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2681
  • View blog
  • Posts: 4,281
  • Joined: 21-June 11

Re: About goto

Posted 24 November 2018 - 08:28 AM

You can't do what you're trying to do. If you declare a variable inside an if, it will only be in scope inside that if. Even if you have multiple variable definitions with the same name, it will always be decided statically which definition a given use of that name refers to. It is impossible to make this dynamic. Neither goto nor any other language construct will help you to get around that fact.

So if you want to define your own means of dynamic allocation without using malloc or any other built-in methods of memory management, you'll have to go a different way. You could define a function my_malloc that simply returns memory from a global (or better static) array of some maximum size and uses global/static index to keep track of how much memory of that array has already been returned. That would work pretty well if you're okay with never freeing the allocated memory. Once you want to free memory, it gets more complicated. If you want to go that way, you should look into algorithms for dynamic memory allocation. Free lists would probably be the simplest way to go there.

That said, you can use malloc in Turbo C perfectly fine, so you should probably just do that, unless you want to implement your own version for learning purposes only (which I'd fully support, but it sounded more like you were trying to implement your own because you thought you had to, which you don't).
Was This Post Helpful? 4
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,530
  • Joined: 05-May 12

Re: About goto

Posted 24 November 2018 - 09:35 AM

View PostNwb, on 24 November 2018 - 08:42 AM, said:

That's why I said "assume that you cannot use new". Turbo C doesn't support Malloc or new, that's the whole point of trying to do this ;)/>/>

:

:edit: Actually I'm not sure of Turbo C supports malloc, so thanks.


Turbo C supports malloc() because that's part of the language. In Turbo C++, it supports both the C malloc() as well as the C++ new.
Was This Post Helpful? 1
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,530
  • Joined: 05-May 12

Re: About goto

Posted 24 November 2018 - 09:42 AM

If you don't want to use malloc() or new, but want to guarantee that you have memory that you can use for data storage, and you can safely assume that you are running in a real DOS machine (including a DOS machine's memory model), then take a look at using the video memory that is not being used while you are in console mode.

Also, if you know that your machine is at IBM XT level or higher and you have more than 680K, if I recall correctly, the memory addresses where the ROM lives is swapped with RAM to give you an extra 64K.

But in general, if you need something mind blowing, consider using Mode X graphics (instead of the BGI) and driving the speaker directly (instead of sound()) so that you can get the effect of polyphonic sound.
Was This Post Helpful? 1
  • +
  • -

#11 Nwb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 11-November 18

Re: About goto

Posted 24 November 2018 - 06:37 PM

Thanks a lot guys. I'll ask my about new and malloc.

I totally forgot that the arrays would be local to the if statement. Can I declare a variable in the if of statement which is local to the main function using scope operator :: or something?
Was This Post Helpful? 0
  • +
  • -

#12 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2681
  • View blog
  • Posts: 4,281
  • Joined: 21-June 11

Re: About goto

Posted 24 November 2018 - 08:00 PM

No and even if you could, it would be illegal to have multiple declarations and/or definitions of the same variable in the same scope, but with different types (remember that an array's size is part of its type). That goes back to what I was saying earlier about everything being determined statically. It is simply impossible that you write x on line 42 without the compiler knowing whether this refers to the x defined on line 23 or the one on line 13.

Also note that :: can only access the scopes of namespaces and structs/classes, not local scopes, and only to access existing members or define ones that have already been declared, not to declare new members. Plus it's a C++ operator that doesn't exist in C.
Was This Post Helpful? 1
  • +
  • -

#13 Nwb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 11-November 18

Re: About goto

Posted 24 November 2018 - 10:32 PM

Okay thanks to everybody that replied. Now I am justtt curious.

What would be the best way to rely on conditionals to declare an array?

I could only come up with something like this:

{
Int a[1]
If (size<2)
  GOTO
} 
//variable destroyed here

{
Int a[2]
If (size<2)
  GOTO
} 
//variable destroyed here

And so on...




in this case too, could we use something other than goto?
Was This Post Helpful? 0
  • +
  • -

#14 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2681
  • View blog
  • Posts: 4,281
  • Joined: 21-June 11

Re: About goto

Posted 24 November 2018 - 10:52 PM

Please explain what you're trying to do there and what purpose the goto is supposed to serve. The code you wrote doesn't make any sense to me.
Was This Post Helpful? 1
  • +
  • -

#15 Nwb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 11-November 18

Re: About goto

Posted 24 November 2018 - 10:55 PM

So would combining this above snippet and the original post make it so that I can do what was originally intended that is dynamic allocation assuming we cannot use new and malloc?

BTW if I could choose between a 2d pointer and a Static 2d array, which would I pick and why? Assuming the array is static and is used Only the stirring values.

Oh and pardon me I meant turbo c++when I said turbo c
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3